NFS: struct nfs_open_dir_context: convert rpc_cred pointer to cred.
[sfrench/cifs-2.6.git] / fs / nfs / nfs4proc.c
1 /*
2  *  fs/nfs/nfs4proc.c
3  *
4  *  Client-side procedure declarations for NFSv4.
5  *
6  *  Copyright (c) 2002 The Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  Kendrick Smith <kmsmith@umich.edu>
10  *  Andy Adamson   <andros@umich.edu>
11  *
12  *  Redistribution and use in source and binary forms, with or without
13  *  modification, are permitted provided that the following conditions
14  *  are met:
15  *
16  *  1. Redistributions of source code must retain the above copyright
17  *     notice, this list of conditions and the following disclaimer.
18  *  2. Redistributions in binary form must reproduce the above copyright
19  *     notice, this list of conditions and the following disclaimer in the
20  *     documentation and/or other materials provided with the distribution.
21  *  3. Neither the name of the University nor the names of its
22  *     contributors may be used to endorse or promote products derived
23  *     from this software without specific prior written permission.
24  *
25  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 #include <linux/mm.h>
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
57 #include <linux/iversion.h>
58
59 #include "nfs4_fs.h"
60 #include "delegation.h"
61 #include "internal.h"
62 #include "iostat.h"
63 #include "callback.h"
64 #include "pnfs.h"
65 #include "netns.h"
66 #include "nfs4idmap.h"
67 #include "nfs4session.h"
68 #include "fscache.h"
69
70 #include "nfs4trace.h"
71
72 #define NFSDBG_FACILITY         NFSDBG_PROC
73
74 #define NFS4_BITMASK_SZ         3
75
76 #define NFS4_POLL_RETRY_MIN     (HZ/10)
77 #define NFS4_POLL_RETRY_MAX     (15*HZ)
78
79 /* file attributes which can be mapped to nfs attributes */
80 #define NFS4_VALID_ATTRS (ATTR_MODE \
81         | ATTR_UID \
82         | ATTR_GID \
83         | ATTR_SIZE \
84         | ATTR_ATIME \
85         | ATTR_MTIME \
86         | ATTR_CTIME \
87         | ATTR_ATIME_SET \
88         | ATTR_MTIME_SET)
89
90 struct nfs4_opendata;
91 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
92 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
93 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
94 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label, struct inode *inode);
95 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
96 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
97                             struct nfs_fattr *fattr, struct iattr *sattr,
98                             struct nfs_open_context *ctx, struct nfs4_label *ilabel,
99                             struct nfs4_label *olabel);
100 #ifdef CONFIG_NFS_V4_1
101 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
102                 struct rpc_cred *cred,
103                 struct nfs4_slot *slot,
104                 bool is_privileged);
105 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
106                 struct rpc_cred *);
107 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
108                 struct rpc_cred *, bool);
109 #endif
110
111 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
112 static inline struct nfs4_label *
113 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
114         struct iattr *sattr, struct nfs4_label *label)
115 {
116         int err;
117
118         if (label == NULL)
119                 return NULL;
120
121         if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
122                 return NULL;
123
124         err = security_dentry_init_security(dentry, sattr->ia_mode,
125                                 &dentry->d_name, (void **)&label->label, &label->len);
126         if (err == 0)
127                 return label;
128
129         return NULL;
130 }
131 static inline void
132 nfs4_label_release_security(struct nfs4_label *label)
133 {
134         if (label)
135                 security_release_secctx(label->label, label->len);
136 }
137 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
138 {
139         if (label)
140                 return server->attr_bitmask;
141
142         return server->attr_bitmask_nl;
143 }
144 #else
145 static inline struct nfs4_label *
146 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
147         struct iattr *sattr, struct nfs4_label *l)
148 { return NULL; }
149 static inline void
150 nfs4_label_release_security(struct nfs4_label *label)
151 { return; }
152 static inline u32 *
153 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
154 { return server->attr_bitmask; }
155 #endif
156
157 /* Prevent leaks of NFSv4 errors into userland */
158 static int nfs4_map_errors(int err)
159 {
160         if (err >= -1000)
161                 return err;
162         switch (err) {
163         case -NFS4ERR_RESOURCE:
164         case -NFS4ERR_LAYOUTTRYLATER:
165         case -NFS4ERR_RECALLCONFLICT:
166                 return -EREMOTEIO;
167         case -NFS4ERR_WRONGSEC:
168         case -NFS4ERR_WRONG_CRED:
169                 return -EPERM;
170         case -NFS4ERR_BADOWNER:
171         case -NFS4ERR_BADNAME:
172                 return -EINVAL;
173         case -NFS4ERR_SHARE_DENIED:
174                 return -EACCES;
175         case -NFS4ERR_MINOR_VERS_MISMATCH:
176                 return -EPROTONOSUPPORT;
177         case -NFS4ERR_FILE_OPEN:
178                 return -EBUSY;
179         default:
180                 dprintk("%s could not handle NFSv4 error %d\n",
181                                 __func__, -err);
182                 break;
183         }
184         return -EIO;
185 }
186
187 /*
188  * This is our standard bitmap for GETATTR requests.
189  */
190 const u32 nfs4_fattr_bitmap[3] = {
191         FATTR4_WORD0_TYPE
192         | FATTR4_WORD0_CHANGE
193         | FATTR4_WORD0_SIZE
194         | FATTR4_WORD0_FSID
195         | FATTR4_WORD0_FILEID,
196         FATTR4_WORD1_MODE
197         | FATTR4_WORD1_NUMLINKS
198         | FATTR4_WORD1_OWNER
199         | FATTR4_WORD1_OWNER_GROUP
200         | FATTR4_WORD1_RAWDEV
201         | FATTR4_WORD1_SPACE_USED
202         | FATTR4_WORD1_TIME_ACCESS
203         | FATTR4_WORD1_TIME_METADATA
204         | FATTR4_WORD1_TIME_MODIFY
205         | FATTR4_WORD1_MOUNTED_ON_FILEID,
206 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
207         FATTR4_WORD2_SECURITY_LABEL
208 #endif
209 };
210
211 static const u32 nfs4_pnfs_open_bitmap[3] = {
212         FATTR4_WORD0_TYPE
213         | FATTR4_WORD0_CHANGE
214         | FATTR4_WORD0_SIZE
215         | FATTR4_WORD0_FSID
216         | FATTR4_WORD0_FILEID,
217         FATTR4_WORD1_MODE
218         | FATTR4_WORD1_NUMLINKS
219         | FATTR4_WORD1_OWNER
220         | FATTR4_WORD1_OWNER_GROUP
221         | FATTR4_WORD1_RAWDEV
222         | FATTR4_WORD1_SPACE_USED
223         | FATTR4_WORD1_TIME_ACCESS
224         | FATTR4_WORD1_TIME_METADATA
225         | FATTR4_WORD1_TIME_MODIFY,
226         FATTR4_WORD2_MDSTHRESHOLD
227 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
228         | FATTR4_WORD2_SECURITY_LABEL
229 #endif
230 };
231
232 static const u32 nfs4_open_noattr_bitmap[3] = {
233         FATTR4_WORD0_TYPE
234         | FATTR4_WORD0_FILEID,
235 };
236
237 const u32 nfs4_statfs_bitmap[3] = {
238         FATTR4_WORD0_FILES_AVAIL
239         | FATTR4_WORD0_FILES_FREE
240         | FATTR4_WORD0_FILES_TOTAL,
241         FATTR4_WORD1_SPACE_AVAIL
242         | FATTR4_WORD1_SPACE_FREE
243         | FATTR4_WORD1_SPACE_TOTAL
244 };
245
246 const u32 nfs4_pathconf_bitmap[3] = {
247         FATTR4_WORD0_MAXLINK
248         | FATTR4_WORD0_MAXNAME,
249         0
250 };
251
252 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
253                         | FATTR4_WORD0_MAXREAD
254                         | FATTR4_WORD0_MAXWRITE
255                         | FATTR4_WORD0_LEASE_TIME,
256                         FATTR4_WORD1_TIME_DELTA
257                         | FATTR4_WORD1_FS_LAYOUT_TYPES,
258                         FATTR4_WORD2_LAYOUT_BLKSIZE
259                         | FATTR4_WORD2_CLONE_BLKSIZE
260 };
261
262 const u32 nfs4_fs_locations_bitmap[3] = {
263         FATTR4_WORD0_CHANGE
264         | FATTR4_WORD0_SIZE
265         | FATTR4_WORD0_FSID
266         | FATTR4_WORD0_FILEID
267         | FATTR4_WORD0_FS_LOCATIONS,
268         FATTR4_WORD1_OWNER
269         | FATTR4_WORD1_OWNER_GROUP
270         | FATTR4_WORD1_RAWDEV
271         | FATTR4_WORD1_SPACE_USED
272         | FATTR4_WORD1_TIME_ACCESS
273         | FATTR4_WORD1_TIME_METADATA
274         | FATTR4_WORD1_TIME_MODIFY
275         | FATTR4_WORD1_MOUNTED_ON_FILEID,
276 };
277
278 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
279                 struct inode *inode)
280 {
281         unsigned long cache_validity;
282
283         memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
284         if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
285                 return;
286
287         cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
288         if (!(cache_validity & NFS_INO_REVAL_FORCED))
289                 cache_validity &= ~(NFS_INO_INVALID_CHANGE
290                                 | NFS_INO_INVALID_SIZE);
291
292         if (!(cache_validity & NFS_INO_INVALID_SIZE))
293                 dst[0] &= ~FATTR4_WORD0_SIZE;
294
295         if (!(cache_validity & NFS_INO_INVALID_CHANGE))
296                 dst[0] &= ~FATTR4_WORD0_CHANGE;
297 }
298
299 static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
300                 const __u32 *src, struct inode *inode)
301 {
302         nfs4_bitmap_copy_adjust(dst, src, inode);
303 }
304
305 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
306                 struct nfs4_readdir_arg *readdir)
307 {
308         unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
309         __be32 *start, *p;
310
311         if (cookie > 2) {
312                 readdir->cookie = cookie;
313                 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
314                 return;
315         }
316
317         readdir->cookie = 0;
318         memset(&readdir->verifier, 0, sizeof(readdir->verifier));
319         if (cookie == 2)
320                 return;
321         
322         /*
323          * NFSv4 servers do not return entries for '.' and '..'
324          * Therefore, we fake these entries here.  We let '.'
325          * have cookie 0 and '..' have cookie 1.  Note that
326          * when talking to the server, we always send cookie 0
327          * instead of 1 or 2.
328          */
329         start = p = kmap_atomic(*readdir->pages);
330         
331         if (cookie == 0) {
332                 *p++ = xdr_one;                                  /* next */
333                 *p++ = xdr_zero;                   /* cookie, first word */
334                 *p++ = xdr_one;                   /* cookie, second word */
335                 *p++ = xdr_one;                             /* entry len */
336                 memcpy(p, ".\0\0\0", 4);                        /* entry */
337                 p++;
338                 *p++ = xdr_one;                         /* bitmap length */
339                 *p++ = htonl(attrs);                           /* bitmap */
340                 *p++ = htonl(12);             /* attribute buffer length */
341                 *p++ = htonl(NF4DIR);
342                 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
343         }
344         
345         *p++ = xdr_one;                                  /* next */
346         *p++ = xdr_zero;                   /* cookie, first word */
347         *p++ = xdr_two;                   /* cookie, second word */
348         *p++ = xdr_two;                             /* entry len */
349         memcpy(p, "..\0\0", 4);                         /* entry */
350         p++;
351         *p++ = xdr_one;                         /* bitmap length */
352         *p++ = htonl(attrs);                           /* bitmap */
353         *p++ = htonl(12);             /* attribute buffer length */
354         *p++ = htonl(NF4DIR);
355         p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
356
357         readdir->pgbase = (char *)p - (char *)start;
358         readdir->count -= readdir->pgbase;
359         kunmap_atomic(start);
360 }
361
362 static void nfs4_test_and_free_stateid(struct nfs_server *server,
363                 nfs4_stateid *stateid,
364                 struct rpc_cred *cred)
365 {
366         const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
367
368         ops->test_and_free_expired(server, stateid, cred);
369 }
370
371 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
372                 nfs4_stateid *stateid,
373                 struct rpc_cred *cred)
374 {
375         stateid->type = NFS4_REVOKED_STATEID_TYPE;
376         nfs4_test_and_free_stateid(server, stateid, cred);
377 }
378
379 static void nfs4_free_revoked_stateid(struct nfs_server *server,
380                 const nfs4_stateid *stateid,
381                 struct rpc_cred *cred)
382 {
383         nfs4_stateid tmp;
384
385         nfs4_stateid_copy(&tmp, stateid);
386         __nfs4_free_revoked_stateid(server, &tmp, cred);
387 }
388
389 static long nfs4_update_delay(long *timeout)
390 {
391         long ret;
392         if (!timeout)
393                 return NFS4_POLL_RETRY_MAX;
394         if (*timeout <= 0)
395                 *timeout = NFS4_POLL_RETRY_MIN;
396         if (*timeout > NFS4_POLL_RETRY_MAX)
397                 *timeout = NFS4_POLL_RETRY_MAX;
398         ret = *timeout;
399         *timeout <<= 1;
400         return ret;
401 }
402
403 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
404 {
405         int res = 0;
406
407         might_sleep();
408
409         freezable_schedule_timeout_killable_unsafe(
410                 nfs4_update_delay(timeout));
411         if (fatal_signal_pending(current))
412                 res = -ERESTARTSYS;
413         return res;
414 }
415
416 /* This is the error handling routine for processes that are allowed
417  * to sleep.
418  */
419 static int nfs4_do_handle_exception(struct nfs_server *server,
420                 int errorcode, struct nfs4_exception *exception)
421 {
422         struct nfs_client *clp = server->nfs_client;
423         struct nfs4_state *state = exception->state;
424         const nfs4_stateid *stateid = exception->stateid;
425         struct inode *inode = exception->inode;
426         int ret = errorcode;
427
428         exception->delay = 0;
429         exception->recovering = 0;
430         exception->retry = 0;
431
432         if (stateid == NULL && state != NULL)
433                 stateid = &state->stateid;
434
435         switch(errorcode) {
436                 case 0:
437                         return 0;
438                 case -NFS4ERR_BADHANDLE:
439                 case -ESTALE:
440                         if (inode != NULL && S_ISREG(inode->i_mode))
441                                 pnfs_destroy_layout(NFS_I(inode));
442                         break;
443                 case -NFS4ERR_DELEG_REVOKED:
444                 case -NFS4ERR_ADMIN_REVOKED:
445                 case -NFS4ERR_EXPIRED:
446                 case -NFS4ERR_BAD_STATEID:
447                         if (inode != NULL && stateid != NULL) {
448                                 nfs_inode_find_state_and_recover(inode,
449                                                 stateid);
450                                 goto wait_on_recovery;
451                         }
452                         /* Fall through */
453                 case -NFS4ERR_OPENMODE:
454                         if (inode) {
455                                 int err;
456
457                                 err = nfs_async_inode_return_delegation(inode,
458                                                 stateid);
459                                 if (err == 0)
460                                         goto wait_on_recovery;
461                                 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
462                                         exception->retry = 1;
463                                         break;
464                                 }
465                         }
466                         if (state == NULL)
467                                 break;
468                         ret = nfs4_schedule_stateid_recovery(server, state);
469                         if (ret < 0)
470                                 break;
471                         goto wait_on_recovery;
472                 case -NFS4ERR_STALE_STATEID:
473                 case -NFS4ERR_STALE_CLIENTID:
474                         nfs4_schedule_lease_recovery(clp);
475                         goto wait_on_recovery;
476                 case -NFS4ERR_MOVED:
477                         ret = nfs4_schedule_migration_recovery(server);
478                         if (ret < 0)
479                                 break;
480                         goto wait_on_recovery;
481                 case -NFS4ERR_LEASE_MOVED:
482                         nfs4_schedule_lease_moved_recovery(clp);
483                         goto wait_on_recovery;
484 #if defined(CONFIG_NFS_V4_1)
485                 case -NFS4ERR_BADSESSION:
486                 case -NFS4ERR_BADSLOT:
487                 case -NFS4ERR_BAD_HIGH_SLOT:
488                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
489                 case -NFS4ERR_DEADSESSION:
490                 case -NFS4ERR_SEQ_FALSE_RETRY:
491                 case -NFS4ERR_SEQ_MISORDERED:
492                         dprintk("%s ERROR: %d Reset session\n", __func__,
493                                 errorcode);
494                         nfs4_schedule_session_recovery(clp->cl_session, errorcode);
495                         goto wait_on_recovery;
496 #endif /* defined(CONFIG_NFS_V4_1) */
497                 case -NFS4ERR_FILE_OPEN:
498                         if (exception->timeout > HZ) {
499                                 /* We have retried a decent amount, time to
500                                  * fail
501                                  */
502                                 ret = -EBUSY;
503                                 break;
504                         }
505                         /* Fall through */
506                 case -NFS4ERR_DELAY:
507                         nfs_inc_server_stats(server, NFSIOS_DELAY);
508                         /* Fall through */
509                 case -NFS4ERR_GRACE:
510                 case -NFS4ERR_LAYOUTTRYLATER:
511                 case -NFS4ERR_RECALLCONFLICT:
512                         exception->delay = 1;
513                         return 0;
514
515                 case -NFS4ERR_RETRY_UNCACHED_REP:
516                 case -NFS4ERR_OLD_STATEID:
517                         exception->retry = 1;
518                         break;
519                 case -NFS4ERR_BADOWNER:
520                         /* The following works around a Linux server bug! */
521                 case -NFS4ERR_BADNAME:
522                         if (server->caps & NFS_CAP_UIDGID_NOMAP) {
523                                 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
524                                 exception->retry = 1;
525                                 printk(KERN_WARNING "NFS: v4 server %s "
526                                                 "does not accept raw "
527                                                 "uid/gids. "
528                                                 "Reenabling the idmapper.\n",
529                                                 server->nfs_client->cl_hostname);
530                         }
531         }
532         /* We failed to handle the error */
533         return nfs4_map_errors(ret);
534 wait_on_recovery:
535         exception->recovering = 1;
536         return 0;
537 }
538
539 /* This is the error handling routine for processes that are allowed
540  * to sleep.
541  */
542 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
543 {
544         struct nfs_client *clp = server->nfs_client;
545         int ret;
546
547         ret = nfs4_do_handle_exception(server, errorcode, exception);
548         if (exception->delay) {
549                 ret = nfs4_delay(server->client, &exception->timeout);
550                 goto out_retry;
551         }
552         if (exception->recovering) {
553                 ret = nfs4_wait_clnt_recover(clp);
554                 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
555                         return -EIO;
556                 goto out_retry;
557         }
558         return ret;
559 out_retry:
560         if (ret == 0)
561                 exception->retry = 1;
562         return ret;
563 }
564
565 static int
566 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
567                 int errorcode, struct nfs4_exception *exception)
568 {
569         struct nfs_client *clp = server->nfs_client;
570         int ret;
571
572         ret = nfs4_do_handle_exception(server, errorcode, exception);
573         if (exception->delay) {
574                 rpc_delay(task, nfs4_update_delay(&exception->timeout));
575                 goto out_retry;
576         }
577         if (exception->recovering) {
578                 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
579                 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
580                         rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
581                 goto out_retry;
582         }
583         if (test_bit(NFS_MIG_FAILED, &server->mig_status))
584                 ret = -EIO;
585         return ret;
586 out_retry:
587         if (ret == 0) {
588                 exception->retry = 1;
589                 /*
590                  * For NFS4ERR_MOVED, the client transport will need to
591                  * be recomputed after migration recovery has completed.
592                  */
593                 if (errorcode == -NFS4ERR_MOVED)
594                         rpc_task_release_transport(task);
595         }
596         return ret;
597 }
598
599 int
600 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
601                         struct nfs4_state *state, long *timeout)
602 {
603         struct nfs4_exception exception = {
604                 .state = state,
605         };
606
607         if (task->tk_status >= 0)
608                 return 0;
609         if (timeout)
610                 exception.timeout = *timeout;
611         task->tk_status = nfs4_async_handle_exception(task, server,
612                         task->tk_status,
613                         &exception);
614         if (exception.delay && timeout)
615                 *timeout = exception.timeout;
616         if (exception.retry)
617                 return -EAGAIN;
618         return 0;
619 }
620
621 /*
622  * Return 'true' if 'clp' is using an rpc_client that is integrity protected
623  * or 'false' otherwise.
624  */
625 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
626 {
627         rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
628         return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
629 }
630
631 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
632 {
633         spin_lock(&clp->cl_lock);
634         if (time_before(clp->cl_last_renewal,timestamp))
635                 clp->cl_last_renewal = timestamp;
636         spin_unlock(&clp->cl_lock);
637 }
638
639 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
640 {
641         struct nfs_client *clp = server->nfs_client;
642
643         if (!nfs4_has_session(clp))
644                 do_renew_lease(clp, timestamp);
645 }
646
647 struct nfs4_call_sync_data {
648         const struct nfs_server *seq_server;
649         struct nfs4_sequence_args *seq_args;
650         struct nfs4_sequence_res *seq_res;
651 };
652
653 void nfs4_init_sequence(struct nfs4_sequence_args *args,
654                         struct nfs4_sequence_res *res, int cache_reply,
655                         int privileged)
656 {
657         args->sa_slot = NULL;
658         args->sa_cache_this = cache_reply;
659         args->sa_privileged = privileged;
660
661         res->sr_slot = NULL;
662 }
663
664 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
665 {
666         struct nfs4_slot *slot = res->sr_slot;
667         struct nfs4_slot_table *tbl;
668
669         tbl = slot->table;
670         spin_lock(&tbl->slot_tbl_lock);
671         if (!nfs41_wake_and_assign_slot(tbl, slot))
672                 nfs4_free_slot(tbl, slot);
673         spin_unlock(&tbl->slot_tbl_lock);
674
675         res->sr_slot = NULL;
676 }
677
678 static int nfs40_sequence_done(struct rpc_task *task,
679                                struct nfs4_sequence_res *res)
680 {
681         if (res->sr_slot != NULL)
682                 nfs40_sequence_free_slot(res);
683         return 1;
684 }
685
686 #if defined(CONFIG_NFS_V4_1)
687
688 static void nfs41_release_slot(struct nfs4_slot *slot)
689 {
690         struct nfs4_session *session;
691         struct nfs4_slot_table *tbl;
692         bool send_new_highest_used_slotid = false;
693
694         if (!slot)
695                 return;
696         tbl = slot->table;
697         session = tbl->session;
698
699         /* Bump the slot sequence number */
700         if (slot->seq_done)
701                 slot->seq_nr++;
702         slot->seq_done = 0;
703
704         spin_lock(&tbl->slot_tbl_lock);
705         /* Be nice to the server: try to ensure that the last transmitted
706          * value for highest_user_slotid <= target_highest_slotid
707          */
708         if (tbl->highest_used_slotid > tbl->target_highest_slotid)
709                 send_new_highest_used_slotid = true;
710
711         if (nfs41_wake_and_assign_slot(tbl, slot)) {
712                 send_new_highest_used_slotid = false;
713                 goto out_unlock;
714         }
715         nfs4_free_slot(tbl, slot);
716
717         if (tbl->highest_used_slotid != NFS4_NO_SLOT)
718                 send_new_highest_used_slotid = false;
719 out_unlock:
720         spin_unlock(&tbl->slot_tbl_lock);
721         if (send_new_highest_used_slotid)
722                 nfs41_notify_server(session->clp);
723         if (waitqueue_active(&tbl->slot_waitq))
724                 wake_up_all(&tbl->slot_waitq);
725 }
726
727 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
728 {
729         nfs41_release_slot(res->sr_slot);
730         res->sr_slot = NULL;
731 }
732
733 static int nfs41_sequence_process(struct rpc_task *task,
734                 struct nfs4_sequence_res *res)
735 {
736         struct nfs4_session *session;
737         struct nfs4_slot *slot = res->sr_slot;
738         struct nfs_client *clp;
739         bool interrupted = false;
740         int ret = 1;
741
742         if (slot == NULL)
743                 goto out_noaction;
744         /* don't increment the sequence number if the task wasn't sent */
745         if (!RPC_WAS_SENT(task))
746                 goto out;
747
748         session = slot->table->session;
749
750         if (slot->interrupted) {
751                 if (res->sr_status != -NFS4ERR_DELAY)
752                         slot->interrupted = 0;
753                 interrupted = true;
754         }
755
756         trace_nfs4_sequence_done(session, res);
757         /* Check the SEQUENCE operation status */
758         switch (res->sr_status) {
759         case 0:
760                 /* Update the slot's sequence and clientid lease timer */
761                 slot->seq_done = 1;
762                 clp = session->clp;
763                 do_renew_lease(clp, res->sr_timestamp);
764                 /* Check sequence flags */
765                 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
766                                 !!slot->privileged);
767                 nfs41_update_target_slotid(slot->table, slot, res);
768                 break;
769         case 1:
770                 /*
771                  * sr_status remains 1 if an RPC level error occurred.
772                  * The server may or may not have processed the sequence
773                  * operation..
774                  * Mark the slot as having hosted an interrupted RPC call.
775                  */
776                 slot->interrupted = 1;
777                 goto out;
778         case -NFS4ERR_DELAY:
779                 /* The server detected a resend of the RPC call and
780                  * returned NFS4ERR_DELAY as per Section 2.10.6.2
781                  * of RFC5661.
782                  */
783                 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
784                         __func__,
785                         slot->slot_nr,
786                         slot->seq_nr);
787                 goto out_retry;
788         case -NFS4ERR_RETRY_UNCACHED_REP:
789         case -NFS4ERR_SEQ_FALSE_RETRY:
790                 /*
791                  * The server thinks we tried to replay a request.
792                  * Retry the call after bumping the sequence ID.
793                  */
794                 goto retry_new_seq;
795         case -NFS4ERR_BADSLOT:
796                 /*
797                  * The slot id we used was probably retired. Try again
798                  * using a different slot id.
799                  */
800                 if (slot->slot_nr < slot->table->target_highest_slotid)
801                         goto session_recover;
802                 goto retry_nowait;
803         case -NFS4ERR_SEQ_MISORDERED:
804                 /*
805                  * Was the last operation on this sequence interrupted?
806                  * If so, retry after bumping the sequence number.
807                  */
808                 if (interrupted)
809                         goto retry_new_seq;
810                 /*
811                  * Could this slot have been previously retired?
812                  * If so, then the server may be expecting seq_nr = 1!
813                  */
814                 if (slot->seq_nr != 1) {
815                         slot->seq_nr = 1;
816                         goto retry_nowait;
817                 }
818                 goto session_recover;
819         default:
820                 /* Just update the slot sequence no. */
821                 slot->seq_done = 1;
822         }
823 out:
824         /* The session may be reset by one of the error handlers. */
825         dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
826 out_noaction:
827         return ret;
828 session_recover:
829         nfs4_schedule_session_recovery(session, res->sr_status);
830         goto retry_nowait;
831 retry_new_seq:
832         ++slot->seq_nr;
833 retry_nowait:
834         if (rpc_restart_call_prepare(task)) {
835                 nfs41_sequence_free_slot(res);
836                 task->tk_status = 0;
837                 ret = 0;
838         }
839         goto out;
840 out_retry:
841         if (!rpc_restart_call(task))
842                 goto out;
843         rpc_delay(task, NFS4_POLL_RETRY_MAX);
844         return 0;
845 }
846
847 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
848 {
849         if (!nfs41_sequence_process(task, res))
850                 return 0;
851         if (res->sr_slot != NULL)
852                 nfs41_sequence_free_slot(res);
853         return 1;
854
855 }
856 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
857
858 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
859 {
860         if (res->sr_slot == NULL)
861                 return 1;
862         if (res->sr_slot->table->session != NULL)
863                 return nfs41_sequence_process(task, res);
864         return nfs40_sequence_done(task, res);
865 }
866
867 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
868 {
869         if (res->sr_slot != NULL) {
870                 if (res->sr_slot->table->session != NULL)
871                         nfs41_sequence_free_slot(res);
872                 else
873                         nfs40_sequence_free_slot(res);
874         }
875 }
876
877 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
878 {
879         if (res->sr_slot == NULL)
880                 return 1;
881         if (!res->sr_slot->table->session)
882                 return nfs40_sequence_done(task, res);
883         return nfs41_sequence_done(task, res);
884 }
885 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
886
887 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
888 {
889         struct nfs4_call_sync_data *data = calldata;
890
891         dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
892
893         nfs4_setup_sequence(data->seq_server->nfs_client,
894                             data->seq_args, data->seq_res, task);
895 }
896
897 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
898 {
899         struct nfs4_call_sync_data *data = calldata;
900
901         nfs41_sequence_done(task, data->seq_res);
902 }
903
904 static const struct rpc_call_ops nfs41_call_sync_ops = {
905         .rpc_call_prepare = nfs41_call_sync_prepare,
906         .rpc_call_done = nfs41_call_sync_done,
907 };
908
909 static void
910 nfs4_sequence_process_interrupted(struct nfs_client *client,
911                 struct nfs4_slot *slot, struct rpc_cred *cred)
912 {
913         struct rpc_task *task;
914
915         task = _nfs41_proc_sequence(client, cred, slot, true);
916         if (!IS_ERR(task))
917                 rpc_put_task_async(task);
918 }
919
920 #else   /* !CONFIG_NFS_V4_1 */
921
922 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
923 {
924         return nfs40_sequence_done(task, res);
925 }
926
927 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
928 {
929         if (res->sr_slot != NULL)
930                 nfs40_sequence_free_slot(res);
931 }
932
933 int nfs4_sequence_done(struct rpc_task *task,
934                        struct nfs4_sequence_res *res)
935 {
936         return nfs40_sequence_done(task, res);
937 }
938 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
939
940 static void
941 nfs4_sequence_process_interrupted(struct nfs_client *client,
942                 struct nfs4_slot *slot, struct rpc_cred *cred)
943 {
944         WARN_ON_ONCE(1);
945         slot->interrupted = 0;
946 }
947
948 #endif  /* !CONFIG_NFS_V4_1 */
949
950 static
951 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
952                 struct nfs4_sequence_res *res,
953                 struct nfs4_slot *slot)
954 {
955         if (!slot)
956                 return;
957         slot->privileged = args->sa_privileged ? 1 : 0;
958         args->sa_slot = slot;
959
960         res->sr_slot = slot;
961         res->sr_timestamp = jiffies;
962         res->sr_status_flags = 0;
963         res->sr_status = 1;
964
965 }
966
967 int nfs4_setup_sequence(struct nfs_client *client,
968                         struct nfs4_sequence_args *args,
969                         struct nfs4_sequence_res *res,
970                         struct rpc_task *task)
971 {
972         struct nfs4_session *session = nfs4_get_session(client);
973         struct nfs4_slot_table *tbl  = client->cl_slot_tbl;
974         struct nfs4_slot *slot;
975
976         /* slot already allocated? */
977         if (res->sr_slot != NULL)
978                 goto out_start;
979
980         if (session) {
981                 tbl = &session->fc_slot_table;
982                 task->tk_timeout = 0;
983         }
984
985         for (;;) {
986                 spin_lock(&tbl->slot_tbl_lock);
987                 /* The state manager will wait until the slot table is empty */
988                 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
989                         goto out_sleep;
990
991                 slot = nfs4_alloc_slot(tbl);
992                 if (IS_ERR(slot)) {
993                         /* Try again in 1/4 second */
994                         if (slot == ERR_PTR(-ENOMEM))
995                                 task->tk_timeout = HZ >> 2;
996                         goto out_sleep;
997                 }
998                 spin_unlock(&tbl->slot_tbl_lock);
999
1000                 if (likely(!slot->interrupted))
1001                         break;
1002                 nfs4_sequence_process_interrupted(client,
1003                                 slot, task->tk_msg.rpc_cred);
1004         }
1005
1006         nfs4_sequence_attach_slot(args, res, slot);
1007
1008         trace_nfs4_setup_sequence(session, args);
1009 out_start:
1010         rpc_call_start(task);
1011         return 0;
1012
1013 out_sleep:
1014         if (args->sa_privileged)
1015                 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1016                                 NULL, RPC_PRIORITY_PRIVILEGED);
1017         else
1018                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1019         spin_unlock(&tbl->slot_tbl_lock);
1020         return -EAGAIN;
1021 }
1022 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1023
1024 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1025 {
1026         struct nfs4_call_sync_data *data = calldata;
1027         nfs4_setup_sequence(data->seq_server->nfs_client,
1028                                 data->seq_args, data->seq_res, task);
1029 }
1030
1031 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1032 {
1033         struct nfs4_call_sync_data *data = calldata;
1034         nfs4_sequence_done(task, data->seq_res);
1035 }
1036
1037 static const struct rpc_call_ops nfs40_call_sync_ops = {
1038         .rpc_call_prepare = nfs40_call_sync_prepare,
1039         .rpc_call_done = nfs40_call_sync_done,
1040 };
1041
1042 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1043                                    struct nfs_server *server,
1044                                    struct rpc_message *msg,
1045                                    struct nfs4_sequence_args *args,
1046                                    struct nfs4_sequence_res *res)
1047 {
1048         int ret;
1049         struct rpc_task *task;
1050         struct nfs_client *clp = server->nfs_client;
1051         struct nfs4_call_sync_data data = {
1052                 .seq_server = server,
1053                 .seq_args = args,
1054                 .seq_res = res,
1055         };
1056         struct rpc_task_setup task_setup = {
1057                 .rpc_client = clnt,
1058                 .rpc_message = msg,
1059                 .callback_ops = clp->cl_mvops->call_sync_ops,
1060                 .callback_data = &data
1061         };
1062
1063         task = rpc_run_task(&task_setup);
1064         if (IS_ERR(task))
1065                 ret = PTR_ERR(task);
1066         else {
1067                 ret = task->tk_status;
1068                 rpc_put_task(task);
1069         }
1070         return ret;
1071 }
1072
1073 int nfs4_call_sync(struct rpc_clnt *clnt,
1074                    struct nfs_server *server,
1075                    struct rpc_message *msg,
1076                    struct nfs4_sequence_args *args,
1077                    struct nfs4_sequence_res *res,
1078                    int cache_reply)
1079 {
1080         nfs4_init_sequence(args, res, cache_reply, 0);
1081         return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1082 }
1083
1084 static void
1085 nfs4_inc_nlink_locked(struct inode *inode)
1086 {
1087         NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1088         inc_nlink(inode);
1089 }
1090
1091 static void
1092 nfs4_dec_nlink_locked(struct inode *inode)
1093 {
1094         NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1095         drop_nlink(inode);
1096 }
1097
1098 static void
1099 update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
1100                 unsigned long timestamp, unsigned long cache_validity)
1101 {
1102         struct nfs_inode *nfsi = NFS_I(dir);
1103
1104         nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1105                 | NFS_INO_INVALID_MTIME
1106                 | NFS_INO_INVALID_DATA
1107                 | cache_validity;
1108         if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
1109                 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1110                 nfsi->attrtimeo_timestamp = jiffies;
1111         } else {
1112                 nfs_force_lookup_revalidate(dir);
1113                 if (cinfo->before != inode_peek_iversion_raw(dir))
1114                         nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1115                                 NFS_INO_INVALID_ACL;
1116         }
1117         inode_set_iversion_raw(dir, cinfo->after);
1118         nfsi->read_cache_jiffies = timestamp;
1119         nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1120         nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1121         nfs_fscache_invalidate(dir);
1122 }
1123
1124 static void
1125 update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1126                 unsigned long timestamp, unsigned long cache_validity)
1127 {
1128         spin_lock(&dir->i_lock);
1129         update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1130         spin_unlock(&dir->i_lock);
1131 }
1132
1133 struct nfs4_open_createattrs {
1134         struct nfs4_label *label;
1135         struct iattr *sattr;
1136         const __u32 verf[2];
1137 };
1138
1139 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1140                 int err, struct nfs4_exception *exception)
1141 {
1142         if (err != -EINVAL)
1143                 return false;
1144         if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1145                 return false;
1146         server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1147         exception->retry = 1;
1148         return true;
1149 }
1150
1151 static u32
1152 nfs4_map_atomic_open_share(struct nfs_server *server,
1153                 fmode_t fmode, int openflags)
1154 {
1155         u32 res = 0;
1156
1157         switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1158         case FMODE_READ:
1159                 res = NFS4_SHARE_ACCESS_READ;
1160                 break;
1161         case FMODE_WRITE:
1162                 res = NFS4_SHARE_ACCESS_WRITE;
1163                 break;
1164         case FMODE_READ|FMODE_WRITE:
1165                 res = NFS4_SHARE_ACCESS_BOTH;
1166         }
1167         if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1168                 goto out;
1169         /* Want no delegation if we're using O_DIRECT */
1170         if (openflags & O_DIRECT)
1171                 res |= NFS4_SHARE_WANT_NO_DELEG;
1172 out:
1173         return res;
1174 }
1175
1176 static enum open_claim_type4
1177 nfs4_map_atomic_open_claim(struct nfs_server *server,
1178                 enum open_claim_type4 claim)
1179 {
1180         if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1181                 return claim;
1182         switch (claim) {
1183         default:
1184                 return claim;
1185         case NFS4_OPEN_CLAIM_FH:
1186                 return NFS4_OPEN_CLAIM_NULL;
1187         case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1188                 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1189         case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1190                 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1191         }
1192 }
1193
1194 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1195 {
1196         p->o_res.f_attr = &p->f_attr;
1197         p->o_res.f_label = p->f_label;
1198         p->o_res.seqid = p->o_arg.seqid;
1199         p->c_res.seqid = p->c_arg.seqid;
1200         p->o_res.server = p->o_arg.server;
1201         p->o_res.access_request = p->o_arg.access;
1202         nfs_fattr_init(&p->f_attr);
1203         nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1204 }
1205
1206 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1207                 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1208                 const struct nfs4_open_createattrs *c,
1209                 enum open_claim_type4 claim,
1210                 gfp_t gfp_mask)
1211 {
1212         struct dentry *parent = dget_parent(dentry);
1213         struct inode *dir = d_inode(parent);
1214         struct nfs_server *server = NFS_SERVER(dir);
1215         struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1216         struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1217         struct nfs4_opendata *p;
1218
1219         p = kzalloc(sizeof(*p), gfp_mask);
1220         if (p == NULL)
1221                 goto err;
1222
1223         p->f_label = nfs4_label_alloc(server, gfp_mask);
1224         if (IS_ERR(p->f_label))
1225                 goto err_free_p;
1226
1227         p->a_label = nfs4_label_alloc(server, gfp_mask);
1228         if (IS_ERR(p->a_label))
1229                 goto err_free_f;
1230
1231         alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1232         p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1233         if (IS_ERR(p->o_arg.seqid))
1234                 goto err_free_label;
1235         nfs_sb_active(dentry->d_sb);
1236         p->dentry = dget(dentry);
1237         p->dir = parent;
1238         p->owner = sp;
1239         atomic_inc(&sp->so_count);
1240         p->o_arg.open_flags = flags;
1241         p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1242         p->o_arg.umask = current_umask();
1243         p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1244         p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1245                         fmode, flags);
1246         /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1247          * will return permission denied for all bits until close */
1248         if (!(flags & O_EXCL)) {
1249                 /* ask server to check for all possible rights as results
1250                  * are cached */
1251                 switch (p->o_arg.claim) {
1252                 default:
1253                         break;
1254                 case NFS4_OPEN_CLAIM_NULL:
1255                 case NFS4_OPEN_CLAIM_FH:
1256                         p->o_arg.access = NFS4_ACCESS_READ |
1257                                 NFS4_ACCESS_MODIFY |
1258                                 NFS4_ACCESS_EXTEND |
1259                                 NFS4_ACCESS_EXECUTE;
1260                 }
1261         }
1262         p->o_arg.clientid = server->nfs_client->cl_clientid;
1263         p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1264         p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1265         p->o_arg.name = &dentry->d_name;
1266         p->o_arg.server = server;
1267         p->o_arg.bitmask = nfs4_bitmask(server, label);
1268         p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1269         p->o_arg.label = nfs4_label_copy(p->a_label, label);
1270         switch (p->o_arg.claim) {
1271         case NFS4_OPEN_CLAIM_NULL:
1272         case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1273         case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1274                 p->o_arg.fh = NFS_FH(dir);
1275                 break;
1276         case NFS4_OPEN_CLAIM_PREVIOUS:
1277         case NFS4_OPEN_CLAIM_FH:
1278         case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1279         case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1280                 p->o_arg.fh = NFS_FH(d_inode(dentry));
1281         }
1282         if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
1283                 p->o_arg.u.attrs = &p->attrs;
1284                 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1285
1286                 memcpy(p->o_arg.u.verifier.data, c->verf,
1287                                 sizeof(p->o_arg.u.verifier.data));
1288         }
1289         p->c_arg.fh = &p->o_res.fh;
1290         p->c_arg.stateid = &p->o_res.stateid;
1291         p->c_arg.seqid = p->o_arg.seqid;
1292         nfs4_init_opendata_res(p);
1293         kref_init(&p->kref);
1294         return p;
1295
1296 err_free_label:
1297         nfs4_label_free(p->a_label);
1298 err_free_f:
1299         nfs4_label_free(p->f_label);
1300 err_free_p:
1301         kfree(p);
1302 err:
1303         dput(parent);
1304         return NULL;
1305 }
1306
1307 static void nfs4_opendata_free(struct kref *kref)
1308 {
1309         struct nfs4_opendata *p = container_of(kref,
1310                         struct nfs4_opendata, kref);
1311         struct super_block *sb = p->dentry->d_sb;
1312
1313         nfs4_lgopen_release(p->lgp);
1314         nfs_free_seqid(p->o_arg.seqid);
1315         nfs4_sequence_free_slot(&p->o_res.seq_res);
1316         if (p->state != NULL)
1317                 nfs4_put_open_state(p->state);
1318         nfs4_put_state_owner(p->owner);
1319
1320         nfs4_label_free(p->a_label);
1321         nfs4_label_free(p->f_label);
1322
1323         dput(p->dir);
1324         dput(p->dentry);
1325         nfs_sb_deactive(sb);
1326         nfs_fattr_free_names(&p->f_attr);
1327         kfree(p->f_attr.mdsthreshold);
1328         kfree(p);
1329 }
1330
1331 static void nfs4_opendata_put(struct nfs4_opendata *p)
1332 {
1333         if (p != NULL)
1334                 kref_put(&p->kref, nfs4_opendata_free);
1335 }
1336
1337 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1338                 fmode_t fmode)
1339 {
1340         switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1341         case FMODE_READ|FMODE_WRITE:
1342                 return state->n_rdwr != 0;
1343         case FMODE_WRITE:
1344                 return state->n_wronly != 0;
1345         case FMODE_READ:
1346                 return state->n_rdonly != 0;
1347         }
1348         WARN_ON_ONCE(1);
1349         return false;
1350 }
1351
1352 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1353                 int open_mode, enum open_claim_type4 claim)
1354 {
1355         int ret = 0;
1356
1357         if (open_mode & (O_EXCL|O_TRUNC))
1358                 goto out;
1359         switch (claim) {
1360         case NFS4_OPEN_CLAIM_NULL:
1361         case NFS4_OPEN_CLAIM_FH:
1362                 goto out;
1363         default:
1364                 break;
1365         }
1366         switch (mode & (FMODE_READ|FMODE_WRITE)) {
1367                 case FMODE_READ:
1368                         ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1369                                 && state->n_rdonly != 0;
1370                         break;
1371                 case FMODE_WRITE:
1372                         ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1373                                 && state->n_wronly != 0;
1374                         break;
1375                 case FMODE_READ|FMODE_WRITE:
1376                         ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1377                                 && state->n_rdwr != 0;
1378         }
1379 out:
1380         return ret;
1381 }
1382
1383 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1384                 enum open_claim_type4 claim)
1385 {
1386         if (delegation == NULL)
1387                 return 0;
1388         if ((delegation->type & fmode) != fmode)
1389                 return 0;
1390         if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1391                 return 0;
1392         switch (claim) {
1393         case NFS4_OPEN_CLAIM_NULL:
1394         case NFS4_OPEN_CLAIM_FH:
1395                 break;
1396         case NFS4_OPEN_CLAIM_PREVIOUS:
1397                 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1398                         break;
1399                 /* Fall through */
1400         default:
1401                 return 0;
1402         }
1403         nfs_mark_delegation_referenced(delegation);
1404         return 1;
1405 }
1406
1407 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1408 {
1409         switch (fmode) {
1410                 case FMODE_WRITE:
1411                         state->n_wronly++;
1412                         break;
1413                 case FMODE_READ:
1414                         state->n_rdonly++;
1415                         break;
1416                 case FMODE_READ|FMODE_WRITE:
1417                         state->n_rdwr++;
1418         }
1419         nfs4_state_set_mode_locked(state, state->state | fmode);
1420 }
1421
1422 #ifdef CONFIG_NFS_V4_1
1423 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1424 {
1425         if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1426                 return true;
1427         if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1428                 return true;
1429         if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1430                 return true;
1431         return false;
1432 }
1433 #endif /* CONFIG_NFS_V4_1 */
1434
1435 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1436 {
1437         if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1438                 wake_up_all(&state->waitq);
1439 }
1440
1441 static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1442                 const nfs4_stateid *stateid)
1443 {
1444         u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1445         u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1446
1447         if (stateid_seqid == state_seqid + 1U ||
1448             (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1449                 nfs_state_log_update_open_stateid(state);
1450         else
1451                 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1452 }
1453
1454 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1455 {
1456         struct nfs_client *clp = state->owner->so_server->nfs_client;
1457         bool need_recover = false;
1458
1459         if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1460                 need_recover = true;
1461         if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1462                 need_recover = true;
1463         if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1464                 need_recover = true;
1465         if (need_recover)
1466                 nfs4_state_mark_reclaim_nograce(clp, state);
1467 }
1468
1469 /*
1470  * Check for whether or not the caller may update the open stateid
1471  * to the value passed in by stateid.
1472  *
1473  * Note: This function relies heavily on the server implementing
1474  * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1475  * correctly.
1476  * i.e. The stateid seqids have to be initialised to 1, and
1477  * are then incremented on every state transition.
1478  */
1479 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1480                 const nfs4_stateid *stateid)
1481 {
1482         if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1483             !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1484                 if (stateid->seqid == cpu_to_be32(1))
1485                         nfs_state_log_update_open_stateid(state);
1486                 else
1487                         set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1488                 return true;
1489         }
1490
1491         if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1492                 nfs_state_log_out_of_order_open_stateid(state, stateid);
1493                 return true;
1494         }
1495         return false;
1496 }
1497
1498 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1499 {
1500         if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1501                 return;
1502         if (state->n_wronly)
1503                 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1504         if (state->n_rdonly)
1505                 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1506         if (state->n_rdwr)
1507                 set_bit(NFS_O_RDWR_STATE, &state->flags);
1508         set_bit(NFS_OPEN_STATE, &state->flags);
1509 }
1510
1511 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1512                 nfs4_stateid *stateid, fmode_t fmode)
1513 {
1514         clear_bit(NFS_O_RDWR_STATE, &state->flags);
1515         switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1516         case FMODE_WRITE:
1517                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1518                 break;
1519         case FMODE_READ:
1520                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1521                 break;
1522         case 0:
1523                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1524                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1525                 clear_bit(NFS_OPEN_STATE, &state->flags);
1526         }
1527         if (stateid == NULL)
1528                 return;
1529         /* Handle OPEN+OPEN_DOWNGRADE races */
1530         if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1531             !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1532                 nfs_resync_open_stateid_locked(state);
1533                 goto out;
1534         }
1535         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1536                 nfs4_stateid_copy(&state->stateid, stateid);
1537         nfs4_stateid_copy(&state->open_stateid, stateid);
1538         trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1539 out:
1540         nfs_state_log_update_open_stateid(state);
1541 }
1542
1543 static void nfs_clear_open_stateid(struct nfs4_state *state,
1544         nfs4_stateid *arg_stateid,
1545         nfs4_stateid *stateid, fmode_t fmode)
1546 {
1547         write_seqlock(&state->seqlock);
1548         /* Ignore, if the CLOSE argment doesn't match the current stateid */
1549         if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1550                 nfs_clear_open_stateid_locked(state, stateid, fmode);
1551         write_sequnlock(&state->seqlock);
1552         if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1553                 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1554 }
1555
1556 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1557                 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1558 {
1559         DEFINE_WAIT(wait);
1560         int status = 0;
1561         for (;;) {
1562
1563                 if (!nfs_need_update_open_stateid(state, stateid))
1564                         return;
1565                 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1566                         break;
1567                 if (status)
1568                         break;
1569                 /* Rely on seqids for serialisation with NFSv4.0 */
1570                 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1571                         break;
1572
1573                 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1574                 /*
1575                  * Ensure we process the state changes in the same order
1576                  * in which the server processed them by delaying the
1577                  * update of the stateid until we are in sequence.
1578                  */
1579                 write_sequnlock(&state->seqlock);
1580                 spin_unlock(&state->owner->so_lock);
1581                 rcu_read_unlock();
1582                 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1583                 if (!signal_pending(current)) {
1584                         if (schedule_timeout(5*HZ) == 0)
1585                                 status = -EAGAIN;
1586                         else
1587                                 status = 0;
1588                 } else
1589                         status = -EINTR;
1590                 finish_wait(&state->waitq, &wait);
1591                 rcu_read_lock();
1592                 spin_lock(&state->owner->so_lock);
1593                 write_seqlock(&state->seqlock);
1594         }
1595
1596         if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1597             !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1598                 nfs4_stateid_copy(freeme, &state->open_stateid);
1599                 nfs_test_and_clear_all_open_stateid(state);
1600         }
1601
1602         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1603                 nfs4_stateid_copy(&state->stateid, stateid);
1604         nfs4_stateid_copy(&state->open_stateid, stateid);
1605         trace_nfs4_open_stateid_update(state->inode, stateid, status);
1606         nfs_state_log_update_open_stateid(state);
1607 }
1608
1609 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1610                 const nfs4_stateid *open_stateid,
1611                 fmode_t fmode,
1612                 nfs4_stateid *freeme)
1613 {
1614         /*
1615          * Protect the call to nfs4_state_set_mode_locked and
1616          * serialise the stateid update
1617          */
1618         write_seqlock(&state->seqlock);
1619         nfs_set_open_stateid_locked(state, open_stateid, freeme);
1620         switch (fmode) {
1621         case FMODE_READ:
1622                 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1623                 break;
1624         case FMODE_WRITE:
1625                 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1626                 break;
1627         case FMODE_READ|FMODE_WRITE:
1628                 set_bit(NFS_O_RDWR_STATE, &state->flags);
1629         }
1630         set_bit(NFS_OPEN_STATE, &state->flags);
1631         write_sequnlock(&state->seqlock);
1632 }
1633
1634 static void nfs_state_set_delegation(struct nfs4_state *state,
1635                 const nfs4_stateid *deleg_stateid,
1636                 fmode_t fmode)
1637 {
1638         /*
1639          * Protect the call to nfs4_state_set_mode_locked and
1640          * serialise the stateid update
1641          */
1642         write_seqlock(&state->seqlock);
1643         nfs4_stateid_copy(&state->stateid, deleg_stateid);
1644         set_bit(NFS_DELEGATED_STATE, &state->flags);
1645         write_sequnlock(&state->seqlock);
1646 }
1647
1648 static void nfs_state_clear_delegation(struct nfs4_state *state)
1649 {
1650         write_seqlock(&state->seqlock);
1651         nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1652         clear_bit(NFS_DELEGATED_STATE, &state->flags);
1653         write_sequnlock(&state->seqlock);
1654 }
1655
1656 static int update_open_stateid(struct nfs4_state *state,
1657                 const nfs4_stateid *open_stateid,
1658                 const nfs4_stateid *delegation,
1659                 fmode_t fmode)
1660 {
1661         struct nfs_server *server = NFS_SERVER(state->inode);
1662         struct nfs_client *clp = server->nfs_client;
1663         struct nfs_inode *nfsi = NFS_I(state->inode);
1664         struct nfs_delegation *deleg_cur;
1665         nfs4_stateid freeme = { };
1666         int ret = 0;
1667
1668         fmode &= (FMODE_READ|FMODE_WRITE);
1669
1670         rcu_read_lock();
1671         spin_lock(&state->owner->so_lock);
1672         if (open_stateid != NULL) {
1673                 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1674                 ret = 1;
1675         }
1676
1677         deleg_cur = rcu_dereference(nfsi->delegation);
1678         if (deleg_cur == NULL)
1679                 goto no_delegation;
1680
1681         spin_lock(&deleg_cur->lock);
1682         if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1683            test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1684             (deleg_cur->type & fmode) != fmode)
1685                 goto no_delegation_unlock;
1686
1687         if (delegation == NULL)
1688                 delegation = &deleg_cur->stateid;
1689         else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1690                 goto no_delegation_unlock;
1691
1692         nfs_mark_delegation_referenced(deleg_cur);
1693         nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1694         ret = 1;
1695 no_delegation_unlock:
1696         spin_unlock(&deleg_cur->lock);
1697 no_delegation:
1698         if (ret)
1699                 update_open_stateflags(state, fmode);
1700         spin_unlock(&state->owner->so_lock);
1701         rcu_read_unlock();
1702
1703         if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1704                 nfs4_schedule_state_manager(clp);
1705         if (freeme.type != 0)
1706                 nfs4_test_and_free_stateid(server, &freeme,
1707                                 state->owner->so_cred);
1708
1709         return ret;
1710 }
1711
1712 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1713                 const nfs4_stateid *stateid)
1714 {
1715         struct nfs4_state *state = lsp->ls_state;
1716         bool ret = false;
1717
1718         spin_lock(&state->state_lock);
1719         if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1720                 goto out_noupdate;
1721         if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1722                 goto out_noupdate;
1723         nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1724         ret = true;
1725 out_noupdate:
1726         spin_unlock(&state->state_lock);
1727         return ret;
1728 }
1729
1730 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1731 {
1732         struct nfs_delegation *delegation;
1733
1734         fmode &= FMODE_READ|FMODE_WRITE;
1735         rcu_read_lock();
1736         delegation = rcu_dereference(NFS_I(inode)->delegation);
1737         if (delegation == NULL || (delegation->type & fmode) == fmode) {
1738                 rcu_read_unlock();
1739                 return;
1740         }
1741         rcu_read_unlock();
1742         nfs4_inode_return_delegation(inode);
1743 }
1744
1745 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1746 {
1747         struct nfs4_state *state = opendata->state;
1748         struct nfs_inode *nfsi = NFS_I(state->inode);
1749         struct nfs_delegation *delegation;
1750         int open_mode = opendata->o_arg.open_flags;
1751         fmode_t fmode = opendata->o_arg.fmode;
1752         enum open_claim_type4 claim = opendata->o_arg.claim;
1753         nfs4_stateid stateid;
1754         int ret = -EAGAIN;
1755
1756         for (;;) {
1757                 spin_lock(&state->owner->so_lock);
1758                 if (can_open_cached(state, fmode, open_mode, claim)) {
1759                         update_open_stateflags(state, fmode);
1760                         spin_unlock(&state->owner->so_lock);
1761                         goto out_return_state;
1762                 }
1763                 spin_unlock(&state->owner->so_lock);
1764                 rcu_read_lock();
1765                 delegation = rcu_dereference(nfsi->delegation);
1766                 if (!can_open_delegated(delegation, fmode, claim)) {
1767                         rcu_read_unlock();
1768                         break;
1769                 }
1770                 /* Save the delegation */
1771                 nfs4_stateid_copy(&stateid, &delegation->stateid);
1772                 rcu_read_unlock();
1773                 nfs_release_seqid(opendata->o_arg.seqid);
1774                 if (!opendata->is_recover) {
1775                         ret = nfs_may_open(state->inode, state->owner->so_cred->cr_cred, open_mode);
1776                         if (ret != 0)
1777                                 goto out;
1778                 }
1779                 ret = -EAGAIN;
1780
1781                 /* Try to update the stateid using the delegation */
1782                 if (update_open_stateid(state, NULL, &stateid, fmode))
1783                         goto out_return_state;
1784         }
1785 out:
1786         return ERR_PTR(ret);
1787 out_return_state:
1788         refcount_inc(&state->count);
1789         return state;
1790 }
1791
1792 static void
1793 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1794 {
1795         struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1796         struct nfs_delegation *delegation;
1797         int delegation_flags = 0;
1798
1799         rcu_read_lock();
1800         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1801         if (delegation)
1802                 delegation_flags = delegation->flags;
1803         rcu_read_unlock();
1804         switch (data->o_arg.claim) {
1805         default:
1806                 break;
1807         case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1808         case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1809                 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1810                                    "returning a delegation for "
1811                                    "OPEN(CLAIM_DELEGATE_CUR)\n",
1812                                    clp->cl_hostname);
1813                 return;
1814         }
1815         if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1816                 nfs_inode_set_delegation(state->inode,
1817                                 data->owner->so_cred,
1818                                 data->o_res.delegation_type,
1819                                 &data->o_res.delegation,
1820                                 data->o_res.pagemod_limit);
1821         else
1822                 nfs_inode_reclaim_delegation(state->inode,
1823                                 data->owner->so_cred,
1824                                 data->o_res.delegation_type,
1825                                 &data->o_res.delegation,
1826                                 data->o_res.pagemod_limit);
1827
1828         if (data->o_res.do_recall)
1829                 nfs_async_inode_return_delegation(state->inode,
1830                                                   &data->o_res.delegation);
1831 }
1832
1833 /*
1834  * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1835  * and update the nfs4_state.
1836  */
1837 static struct nfs4_state *
1838 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1839 {
1840         struct inode *inode = data->state->inode;
1841         struct nfs4_state *state = data->state;
1842         int ret;
1843
1844         if (!data->rpc_done) {
1845                 if (data->rpc_status)
1846                         return ERR_PTR(data->rpc_status);
1847                 /* cached opens have already been processed */
1848                 goto update;
1849         }
1850
1851         ret = nfs_refresh_inode(inode, &data->f_attr);
1852         if (ret)
1853                 return ERR_PTR(ret);
1854
1855         if (data->o_res.delegation_type != 0)
1856                 nfs4_opendata_check_deleg(data, state);
1857 update:
1858         update_open_stateid(state, &data->o_res.stateid, NULL,
1859                             data->o_arg.fmode);
1860         refcount_inc(&state->count);
1861
1862         return state;
1863 }
1864
1865 static struct inode *
1866 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1867 {
1868         struct inode *inode;
1869
1870         switch (data->o_arg.claim) {
1871         case NFS4_OPEN_CLAIM_NULL:
1872         case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1873         case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1874                 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1875                         return ERR_PTR(-EAGAIN);
1876                 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1877                                 &data->f_attr, data->f_label);
1878                 break;
1879         default:
1880                 inode = d_inode(data->dentry);
1881                 ihold(inode);
1882                 nfs_refresh_inode(inode, &data->f_attr);
1883         }
1884         return inode;
1885 }
1886
1887 static struct nfs4_state *
1888 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1889 {
1890         struct nfs4_state *state;
1891         struct inode *inode;
1892
1893         inode = nfs4_opendata_get_inode(data);
1894         if (IS_ERR(inode))
1895                 return ERR_CAST(inode);
1896         if (data->state != NULL && data->state->inode == inode) {
1897                 state = data->state;
1898                 refcount_inc(&state->count);
1899         } else
1900                 state = nfs4_get_open_state(inode, data->owner);
1901         iput(inode);
1902         if (state == NULL)
1903                 state = ERR_PTR(-ENOMEM);
1904         return state;
1905 }
1906
1907 static struct nfs4_state *
1908 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1909 {
1910         struct nfs4_state *state;
1911
1912         if (!data->rpc_done) {
1913                 state = nfs4_try_open_cached(data);
1914                 trace_nfs4_cached_open(data->state);
1915                 goto out;
1916         }
1917
1918         state = nfs4_opendata_find_nfs4_state(data);
1919         if (IS_ERR(state))
1920                 goto out;
1921
1922         if (data->o_res.delegation_type != 0)
1923                 nfs4_opendata_check_deleg(data, state);
1924         update_open_stateid(state, &data->o_res.stateid, NULL,
1925                         data->o_arg.fmode);
1926 out:
1927         nfs_release_seqid(data->o_arg.seqid);
1928         return state;
1929 }
1930
1931 static struct nfs4_state *
1932 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1933 {
1934         struct nfs4_state *ret;
1935
1936         if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1937                 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1938         else
1939                 ret = _nfs4_opendata_to_nfs4_state(data);
1940         nfs4_sequence_free_slot(&data->o_res.seq_res);
1941         return ret;
1942 }
1943
1944 static struct nfs_open_context *
1945 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
1946 {
1947         struct nfs_inode *nfsi = NFS_I(state->inode);
1948         struct nfs_open_context *ctx;
1949
1950         rcu_read_lock();
1951         list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
1952                 if (ctx->state != state)
1953                         continue;
1954                 if ((ctx->mode & mode) != mode)
1955                         continue;
1956                 if (!get_nfs_open_context(ctx))
1957                         continue;
1958                 rcu_read_unlock();
1959                 return ctx;
1960         }
1961         rcu_read_unlock();
1962         return ERR_PTR(-ENOENT);
1963 }
1964
1965 static struct nfs_open_context *
1966 nfs4_state_find_open_context(struct nfs4_state *state)
1967 {
1968         struct nfs_open_context *ctx;
1969
1970         ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
1971         if (!IS_ERR(ctx))
1972                 return ctx;
1973         ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
1974         if (!IS_ERR(ctx))
1975                 return ctx;
1976         return nfs4_state_find_open_context_mode(state, FMODE_READ);
1977 }
1978
1979 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1980                 struct nfs4_state *state, enum open_claim_type4 claim)
1981 {
1982         struct nfs4_opendata *opendata;
1983
1984         opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1985                         NULL, claim, GFP_NOFS);
1986         if (opendata == NULL)
1987                 return ERR_PTR(-ENOMEM);
1988         opendata->state = state;
1989         refcount_inc(&state->count);
1990         return opendata;
1991 }
1992
1993 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1994                 fmode_t fmode)
1995 {
1996         struct nfs4_state *newstate;
1997         int ret;
1998
1999         if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2000                 return 0;
2001         opendata->o_arg.open_flags = 0;
2002         opendata->o_arg.fmode = fmode;
2003         opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2004                         NFS_SB(opendata->dentry->d_sb),
2005                         fmode, 0);
2006         memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2007         memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2008         nfs4_init_opendata_res(opendata);
2009         ret = _nfs4_recover_proc_open(opendata);
2010         if (ret != 0)
2011                 return ret; 
2012         newstate = nfs4_opendata_to_nfs4_state(opendata);
2013         if (IS_ERR(newstate))
2014                 return PTR_ERR(newstate);
2015         if (newstate != opendata->state)
2016                 ret = -ESTALE;
2017         nfs4_close_state(newstate, fmode);
2018         return ret;
2019 }
2020
2021 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2022 {
2023         int ret;
2024
2025         /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
2026         clear_bit(NFS_O_RDWR_STATE, &state->flags);
2027         clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2028         clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2029         /* memory barrier prior to reading state->n_* */
2030         clear_bit(NFS_DELEGATED_STATE, &state->flags);
2031         clear_bit(NFS_OPEN_STATE, &state->flags);
2032         smp_rmb();
2033         ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2034         if (ret != 0)
2035                 return ret;
2036         ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2037         if (ret != 0)
2038                 return ret;
2039         ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2040         if (ret != 0)
2041                 return ret;
2042         /*
2043          * We may have performed cached opens for all three recoveries.
2044          * Check if we need to update the current stateid.
2045          */
2046         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2047             !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2048                 write_seqlock(&state->seqlock);
2049                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2050                         nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2051                 write_sequnlock(&state->seqlock);
2052         }
2053         return 0;
2054 }
2055
2056 /*
2057  * OPEN_RECLAIM:
2058  *      reclaim state on the server after a reboot.
2059  */
2060 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2061 {
2062         struct nfs_delegation *delegation;
2063         struct nfs4_opendata *opendata;
2064         fmode_t delegation_type = 0;
2065         int status;
2066
2067         opendata = nfs4_open_recoverdata_alloc(ctx, state,
2068                         NFS4_OPEN_CLAIM_PREVIOUS);
2069         if (IS_ERR(opendata))
2070                 return PTR_ERR(opendata);
2071         rcu_read_lock();
2072         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2073         if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2074                 delegation_type = delegation->type;
2075         rcu_read_unlock();
2076         opendata->o_arg.u.delegation_type = delegation_type;
2077         status = nfs4_open_recover(opendata, state);
2078         nfs4_opendata_put(opendata);
2079         return status;
2080 }
2081
2082 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2083 {
2084         struct nfs_server *server = NFS_SERVER(state->inode);
2085         struct nfs4_exception exception = { };
2086         int err;
2087         do {
2088                 err = _nfs4_do_open_reclaim(ctx, state);
2089                 trace_nfs4_open_reclaim(ctx, 0, err);
2090                 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2091                         continue;
2092                 if (err != -NFS4ERR_DELAY)
2093                         break;
2094                 nfs4_handle_exception(server, err, &exception);
2095         } while (exception.retry);
2096         return err;
2097 }
2098
2099 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2100 {
2101         struct nfs_open_context *ctx;
2102         int ret;
2103
2104         ctx = nfs4_state_find_open_context(state);
2105         if (IS_ERR(ctx))
2106                 return -EAGAIN;
2107         ret = nfs4_do_open_reclaim(ctx, state);
2108         put_nfs_open_context(ctx);
2109         return ret;
2110 }
2111
2112 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)
2113 {
2114         switch (err) {
2115                 default:
2116                         printk(KERN_ERR "NFS: %s: unhandled error "
2117                                         "%d.\n", __func__, err);
2118                 case 0:
2119                 case -ENOENT:
2120                 case -EAGAIN:
2121                 case -ESTALE:
2122                         break;
2123                 case -NFS4ERR_BADSESSION:
2124                 case -NFS4ERR_BADSLOT:
2125                 case -NFS4ERR_BAD_HIGH_SLOT:
2126                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2127                 case -NFS4ERR_DEADSESSION:
2128                         set_bit(NFS_DELEGATED_STATE, &state->flags);
2129                         nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2130                         return -EAGAIN;
2131                 case -NFS4ERR_STALE_CLIENTID:
2132                 case -NFS4ERR_STALE_STATEID:
2133                         set_bit(NFS_DELEGATED_STATE, &state->flags);
2134                         /* Don't recall a delegation if it was lost */
2135                         nfs4_schedule_lease_recovery(server->nfs_client);
2136                         return -EAGAIN;
2137                 case -NFS4ERR_MOVED:
2138                         nfs4_schedule_migration_recovery(server);
2139                         return -EAGAIN;
2140                 case -NFS4ERR_LEASE_MOVED:
2141                         nfs4_schedule_lease_moved_recovery(server->nfs_client);
2142                         return -EAGAIN;
2143                 case -NFS4ERR_DELEG_REVOKED:
2144                 case -NFS4ERR_ADMIN_REVOKED:
2145                 case -NFS4ERR_EXPIRED:
2146                 case -NFS4ERR_BAD_STATEID:
2147                 case -NFS4ERR_OPENMODE:
2148                         nfs_inode_find_state_and_recover(state->inode,
2149                                         stateid);
2150                         nfs4_schedule_stateid_recovery(server, state);
2151                         return -EAGAIN;
2152                 case -NFS4ERR_DELAY:
2153                 case -NFS4ERR_GRACE:
2154                         set_bit(NFS_DELEGATED_STATE, &state->flags);
2155                         ssleep(1);
2156                         return -EAGAIN;
2157                 case -ENOMEM:
2158                 case -NFS4ERR_DENIED:
2159                         if (fl) {
2160                                 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2161                                 if (lsp)
2162                                         set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2163                         }
2164                         return 0;
2165         }
2166         return err;
2167 }
2168
2169 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2170                 struct nfs4_state *state, const nfs4_stateid *stateid,
2171                 fmode_t type)
2172 {
2173         struct nfs_server *server = NFS_SERVER(state->inode);
2174         struct nfs4_opendata *opendata;
2175         int err = 0;
2176
2177         opendata = nfs4_open_recoverdata_alloc(ctx, state,
2178                         NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2179         if (IS_ERR(opendata))
2180                 return PTR_ERR(opendata);
2181         nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2182         nfs_state_clear_delegation(state);
2183         switch (type & (FMODE_READ|FMODE_WRITE)) {
2184         case FMODE_READ|FMODE_WRITE:
2185         case FMODE_WRITE:
2186                 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2187                 if (err)
2188                         break;
2189                 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2190                 if (err)
2191                         break;
2192                 /* Fall through */
2193         case FMODE_READ:
2194                 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2195         }
2196         nfs4_opendata_put(opendata);
2197         return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2198 }
2199
2200 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2201 {
2202         struct nfs4_opendata *data = calldata;
2203
2204         nfs4_setup_sequence(data->o_arg.server->nfs_client,
2205                            &data->c_arg.seq_args, &data->c_res.seq_res, task);
2206 }
2207
2208 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2209 {
2210         struct nfs4_opendata *data = calldata;
2211
2212         nfs40_sequence_done(task, &data->c_res.seq_res);
2213
2214         data->rpc_status = task->tk_status;
2215         if (data->rpc_status == 0) {
2216                 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2217                 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2218                 renew_lease(data->o_res.server, data->timestamp);
2219                 data->rpc_done = true;
2220         }
2221 }
2222
2223 static void nfs4_open_confirm_release(void *calldata)
2224 {
2225         struct nfs4_opendata *data = calldata;
2226         struct nfs4_state *state = NULL;
2227
2228         /* If this request hasn't been cancelled, do nothing */
2229         if (!data->cancelled)
2230                 goto out_free;
2231         /* In case of error, no cleanup! */
2232         if (!data->rpc_done)
2233                 goto out_free;
2234         state = nfs4_opendata_to_nfs4_state(data);
2235         if (!IS_ERR(state))
2236                 nfs4_close_state(state, data->o_arg.fmode);
2237 out_free:
2238         nfs4_opendata_put(data);
2239 }
2240
2241 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2242         .rpc_call_prepare = nfs4_open_confirm_prepare,
2243         .rpc_call_done = nfs4_open_confirm_done,
2244         .rpc_release = nfs4_open_confirm_release,
2245 };
2246
2247 /*
2248  * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2249  */
2250 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2251 {
2252         struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2253         struct rpc_task *task;
2254         struct  rpc_message msg = {
2255                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2256                 .rpc_argp = &data->c_arg,
2257                 .rpc_resp = &data->c_res,
2258                 .rpc_cred = data->owner->so_cred,
2259         };
2260         struct rpc_task_setup task_setup_data = {
2261                 .rpc_client = server->client,
2262                 .rpc_message = &msg,
2263                 .callback_ops = &nfs4_open_confirm_ops,
2264                 .callback_data = data,
2265                 .workqueue = nfsiod_workqueue,
2266                 .flags = RPC_TASK_ASYNC,
2267         };
2268         int status;
2269
2270         nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2271                                 data->is_recover);
2272         kref_get(&data->kref);
2273         data->rpc_done = false;
2274         data->rpc_status = 0;
2275         data->timestamp = jiffies;
2276         task = rpc_run_task(&task_setup_data);
2277         if (IS_ERR(task))
2278                 return PTR_ERR(task);
2279         status = rpc_wait_for_completion_task(task);
2280         if (status != 0) {
2281                 data->cancelled = true;
2282                 smp_wmb();
2283         } else
2284                 status = data->rpc_status;
2285         rpc_put_task(task);
2286         return status;
2287 }
2288
2289 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2290 {
2291         struct nfs4_opendata *data = calldata;
2292         struct nfs4_state_owner *sp = data->owner;
2293         struct nfs_client *clp = sp->so_server->nfs_client;
2294         enum open_claim_type4 claim = data->o_arg.claim;
2295
2296         if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2297                 goto out_wait;
2298         /*
2299          * Check if we still need to send an OPEN call, or if we can use
2300          * a delegation instead.
2301          */
2302         if (data->state != NULL) {
2303                 struct nfs_delegation *delegation;
2304
2305                 if (can_open_cached(data->state, data->o_arg.fmode,
2306                                         data->o_arg.open_flags, claim))
2307                         goto out_no_action;
2308                 rcu_read_lock();
2309                 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
2310                 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2311                         goto unlock_no_action;
2312                 rcu_read_unlock();
2313         }
2314         /* Update client id. */
2315         data->o_arg.clientid = clp->cl_clientid;
2316         switch (claim) {
2317         default:
2318                 break;
2319         case NFS4_OPEN_CLAIM_PREVIOUS:
2320         case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2321         case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2322                 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2323                 /* Fall through */
2324         case NFS4_OPEN_CLAIM_FH:
2325                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2326         }
2327         data->timestamp = jiffies;
2328         if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2329                                 &data->o_arg.seq_args,
2330                                 &data->o_res.seq_res,
2331                                 task) != 0)
2332                 nfs_release_seqid(data->o_arg.seqid);
2333
2334         /* Set the create mode (note dependency on the session type) */
2335         data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2336         if (data->o_arg.open_flags & O_EXCL) {
2337                 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2338                 if (nfs4_has_persistent_session(clp))
2339                         data->o_arg.createmode = NFS4_CREATE_GUARDED;
2340                 else if (clp->cl_mvops->minor_version > 0)
2341                         data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2342         }
2343         return;
2344 unlock_no_action:
2345         trace_nfs4_cached_open(data->state);
2346         rcu_read_unlock();
2347 out_no_action:
2348         task->tk_action = NULL;
2349 out_wait:
2350         nfs4_sequence_done(task, &data->o_res.seq_res);
2351 }
2352
2353 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2354 {
2355         struct nfs4_opendata *data = calldata;
2356
2357         data->rpc_status = task->tk_status;
2358
2359         if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2360                 return;
2361
2362         if (task->tk_status == 0) {
2363                 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2364                         switch (data->o_res.f_attr->mode & S_IFMT) {
2365                         case S_IFREG:
2366                                 break;
2367                         case S_IFLNK:
2368                                 data->rpc_status = -ELOOP;
2369                                 break;
2370                         case S_IFDIR:
2371                                 data->rpc_status = -EISDIR;
2372                                 break;
2373                         default:
2374                                 data->rpc_status = -ENOTDIR;
2375                         }
2376                 }
2377                 renew_lease(data->o_res.server, data->timestamp);
2378                 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2379                         nfs_confirm_seqid(&data->owner->so_seqid, 0);
2380         }
2381         data->rpc_done = true;
2382 }
2383
2384 static void nfs4_open_release(void *calldata)
2385 {
2386         struct nfs4_opendata *data = calldata;
2387         struct nfs4_state *state = NULL;
2388
2389         /* If this request hasn't been cancelled, do nothing */
2390         if (!data->cancelled)
2391                 goto out_free;
2392         /* In case of error, no cleanup! */
2393         if (data->rpc_status != 0 || !data->rpc_done)
2394                 goto out_free;
2395         /* In case we need an open_confirm, no cleanup! */
2396         if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2397                 goto out_free;
2398         state = nfs4_opendata_to_nfs4_state(data);
2399         if (!IS_ERR(state))
2400                 nfs4_close_state(state, data->o_arg.fmode);
2401 out_free:
2402         nfs4_opendata_put(data);
2403 }
2404
2405 static const struct rpc_call_ops nfs4_open_ops = {
2406         .rpc_call_prepare = nfs4_open_prepare,
2407         .rpc_call_done = nfs4_open_done,
2408         .rpc_release = nfs4_open_release,
2409 };
2410
2411 static int nfs4_run_open_task(struct nfs4_opendata *data,
2412                               struct nfs_open_context *ctx)
2413 {
2414         struct inode *dir = d_inode(data->dir);
2415         struct nfs_server *server = NFS_SERVER(dir);
2416         struct nfs_openargs *o_arg = &data->o_arg;
2417         struct nfs_openres *o_res = &data->o_res;
2418         struct rpc_task *task;
2419         struct rpc_message msg = {
2420                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2421                 .rpc_argp = o_arg,
2422                 .rpc_resp = o_res,
2423                 .rpc_cred = data->owner->so_cred,
2424         };
2425         struct rpc_task_setup task_setup_data = {
2426                 .rpc_client = server->client,
2427                 .rpc_message = &msg,
2428                 .callback_ops = &nfs4_open_ops,
2429                 .callback_data = data,
2430                 .workqueue = nfsiod_workqueue,
2431                 .flags = RPC_TASK_ASYNC,
2432         };
2433         int status;
2434
2435         kref_get(&data->kref);
2436         data->rpc_done = false;
2437         data->rpc_status = 0;
2438         data->cancelled = false;
2439         data->is_recover = false;
2440         if (!ctx) {
2441                 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2442                 data->is_recover = true;
2443         } else {
2444                 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2445                 pnfs_lgopen_prepare(data, ctx);
2446         }
2447         task = rpc_run_task(&task_setup_data);
2448         if (IS_ERR(task))
2449                 return PTR_ERR(task);
2450         status = rpc_wait_for_completion_task(task);
2451         if (status != 0) {
2452                 data->cancelled = true;
2453                 smp_wmb();
2454         } else
2455                 status = data->rpc_status;
2456         rpc_put_task(task);
2457
2458         return status;
2459 }
2460
2461 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2462 {
2463         struct inode *dir = d_inode(data->dir);
2464         struct nfs_openres *o_res = &data->o_res;
2465         int status;
2466
2467         status = nfs4_run_open_task(data, NULL);
2468         if (status != 0 || !data->rpc_done)
2469                 return status;
2470
2471         nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2472
2473         if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2474                 status = _nfs4_proc_open_confirm(data);
2475
2476         return status;
2477 }
2478
2479 /*
2480  * Additional permission checks in order to distinguish between an
2481  * open for read, and an open for execute. This works around the
2482  * fact that NFSv4 OPEN treats read and execute permissions as being
2483  * the same.
2484  * Note that in the non-execute case, we want to turn off permission
2485  * checking if we just created a new file (POSIX open() semantics).
2486  */
2487 static int nfs4_opendata_access(struct rpc_cred *cred,
2488                                 struct nfs4_opendata *opendata,
2489                                 struct nfs4_state *state, fmode_t fmode,
2490                                 int openflags)
2491 {
2492         struct nfs_access_entry cache;
2493         u32 mask, flags;
2494
2495         /* access call failed or for some reason the server doesn't
2496          * support any access modes -- defer access call until later */
2497         if (opendata->o_res.access_supported == 0)
2498                 return 0;
2499
2500         mask = 0;
2501         /*
2502          * Use openflags to check for exec, because fmode won't
2503          * always have FMODE_EXEC set when file open for exec.
2504          */
2505         if (openflags & __FMODE_EXEC) {
2506                 /* ONLY check for exec rights */
2507                 if (S_ISDIR(state->inode->i_mode))
2508                         mask = NFS4_ACCESS_LOOKUP;
2509                 else
2510                         mask = NFS4_ACCESS_EXECUTE;
2511         } else if ((fmode & FMODE_READ) && !opendata->file_created)
2512                 mask = NFS4_ACCESS_READ;
2513
2514         cache.cred = cred->cr_cred;
2515         nfs_access_set_mask(&cache, opendata->o_res.access_result);
2516         nfs_access_add_cache(state->inode, &cache);
2517
2518         flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2519         if ((mask & ~cache.mask & flags) == 0)
2520                 return 0;
2521
2522         return -EACCES;
2523 }
2524
2525 /*
2526  * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2527  */
2528 static int _nfs4_proc_open(struct nfs4_opendata *data,
2529                            struct nfs_open_context *ctx)
2530 {
2531         struct inode *dir = d_inode(data->dir);
2532         struct nfs_server *server = NFS_SERVER(dir);
2533         struct nfs_openargs *o_arg = &data->o_arg;
2534         struct nfs_openres *o_res = &data->o_res;
2535         int status;
2536
2537         status = nfs4_run_open_task(data, ctx);
2538         if (!data->rpc_done)
2539                 return status;
2540         if (status != 0) {
2541                 if (status == -NFS4ERR_BADNAME &&
2542                                 !(o_arg->open_flags & O_CREAT))
2543                         return -ENOENT;
2544                 return status;
2545         }
2546
2547         nfs_fattr_map_and_free_names(server, &data->f_attr);
2548
2549         if (o_arg->open_flags & O_CREAT) {
2550                 if (o_arg->open_flags & O_EXCL)
2551                         data->file_created = true;
2552                 else if (o_res->cinfo.before != o_res->cinfo.after)
2553                         data->file_created = true;
2554                 if (data->file_created ||
2555                     inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2556                         update_changeattr(dir, &o_res->cinfo,
2557                                         o_res->f_attr->time_start, 0);
2558         }
2559         if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2560                 server->caps &= ~NFS_CAP_POSIX_LOCK;
2561         if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2562                 status = _nfs4_proc_open_confirm(data);
2563                 if (status != 0)
2564                         return status;
2565         }
2566         if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2567                 nfs4_sequence_free_slot(&o_res->seq_res);
2568                 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2569                                 o_res->f_label, NULL);
2570         }
2571         return 0;
2572 }
2573
2574 /*
2575  * OPEN_EXPIRED:
2576  *      reclaim state on the server after a network partition.
2577  *      Assumes caller holds the appropriate lock
2578  */
2579 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2580 {
2581         struct nfs4_opendata *opendata;
2582         int ret;
2583
2584         opendata = nfs4_open_recoverdata_alloc(ctx, state,
2585                         NFS4_OPEN_CLAIM_FH);
2586         if (IS_ERR(opendata))
2587                 return PTR_ERR(opendata);
2588         ret = nfs4_open_recover(opendata, state);
2589         if (ret == -ESTALE)
2590                 d_drop(ctx->dentry);
2591         nfs4_opendata_put(opendata);
2592         return ret;
2593 }
2594
2595 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2596 {
2597         struct nfs_server *server = NFS_SERVER(state->inode);
2598         struct nfs4_exception exception = { };
2599         int err;
2600
2601         do {
2602                 err = _nfs4_open_expired(ctx, state);
2603                 trace_nfs4_open_expired(ctx, 0, err);
2604                 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2605                         continue;
2606                 switch (err) {
2607                 default:
2608                         goto out;
2609                 case -NFS4ERR_GRACE:
2610                 case -NFS4ERR_DELAY:
2611                         nfs4_handle_exception(server, err, &exception);
2612                         err = 0;
2613                 }
2614         } while (exception.retry);
2615 out:
2616         return err;
2617 }
2618
2619 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2620 {
2621         struct nfs_open_context *ctx;
2622         int ret;
2623
2624         ctx = nfs4_state_find_open_context(state);
2625         if (IS_ERR(ctx))
2626                 return -EAGAIN;
2627         ret = nfs4_do_open_expired(ctx, state);
2628         put_nfs_open_context(ctx);
2629         return ret;
2630 }
2631
2632 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2633                 const nfs4_stateid *stateid)
2634 {
2635         nfs_remove_bad_delegation(state->inode, stateid);
2636         nfs_state_clear_delegation(state);
2637 }
2638
2639 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2640 {
2641         if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2642                 nfs_finish_clear_delegation_stateid(state, NULL);
2643 }
2644
2645 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2646 {
2647         /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2648         nfs40_clear_delegation_stateid(state);
2649         return nfs4_open_expired(sp, state);
2650 }
2651
2652 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2653                 nfs4_stateid *stateid,
2654                 struct rpc_cred *cred)
2655 {
2656         return -NFS4ERR_BAD_STATEID;
2657 }
2658
2659 #if defined(CONFIG_NFS_V4_1)
2660 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2661                 nfs4_stateid *stateid,
2662                 struct rpc_cred *cred)
2663 {
2664         int status;
2665
2666         switch (stateid->type) {
2667         default:
2668                 break;
2669         case NFS4_INVALID_STATEID_TYPE:
2670         case NFS4_SPECIAL_STATEID_TYPE:
2671                 return -NFS4ERR_BAD_STATEID;
2672         case NFS4_REVOKED_STATEID_TYPE:
2673                 goto out_free;
2674         }
2675
2676         status = nfs41_test_stateid(server, stateid, cred);
2677         switch (status) {
2678         case -NFS4ERR_EXPIRED:
2679         case -NFS4ERR_ADMIN_REVOKED:
2680         case -NFS4ERR_DELEG_REVOKED:
2681                 break;
2682         default:
2683                 return status;
2684         }
2685 out_free:
2686         /* Ack the revoked state to the server */
2687         nfs41_free_stateid(server, stateid, cred, true);
2688         return -NFS4ERR_EXPIRED;
2689 }
2690
2691 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2692 {
2693         struct nfs_server *server = NFS_SERVER(state->inode);
2694         nfs4_stateid stateid;
2695         struct nfs_delegation *delegation;
2696         struct rpc_cred *cred;
2697         int status;
2698
2699         /* Get the delegation credential for use by test/free_stateid */
2700         rcu_read_lock();
2701         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2702         if (delegation == NULL) {
2703                 rcu_read_unlock();
2704                 nfs_state_clear_delegation(state);
2705                 return;
2706         }
2707
2708         nfs4_stateid_copy(&stateid, &delegation->stateid);
2709         if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2710                 rcu_read_unlock();
2711                 nfs_state_clear_delegation(state);
2712                 return;
2713         }
2714
2715         if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2716                                 &delegation->flags)) {
2717                 rcu_read_unlock();
2718                 return;
2719         }
2720
2721         cred = get_rpccred(delegation->cred);
2722         rcu_read_unlock();
2723         status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2724         trace_nfs4_test_delegation_stateid(state, NULL, status);
2725         if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2726                 nfs_finish_clear_delegation_stateid(state, &stateid);
2727
2728         put_rpccred(cred);
2729 }
2730
2731 /**
2732  * nfs41_check_expired_locks - possibly free a lock stateid
2733  *
2734  * @state: NFSv4 state for an inode
2735  *
2736  * Returns NFS_OK if recovery for this stateid is now finished.
2737  * Otherwise a negative NFS4ERR value is returned.
2738  */
2739 static int nfs41_check_expired_locks(struct nfs4_state *state)
2740 {
2741         int status, ret = NFS_OK;
2742         struct nfs4_lock_state *lsp, *prev = NULL;
2743         struct nfs_server *server = NFS_SERVER(state->inode);
2744
2745         if (!test_bit(LK_STATE_IN_USE, &state->flags))
2746                 goto out;
2747
2748         spin_lock(&state->state_lock);
2749         list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2750                 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2751                         struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2752
2753                         refcount_inc(&lsp->ls_count);
2754                         spin_unlock(&state->state_lock);
2755
2756                         nfs4_put_lock_state(prev);
2757                         prev = lsp;
2758
2759                         status = nfs41_test_and_free_expired_stateid(server,
2760                                         &lsp->ls_stateid,
2761                                         cred);
2762                         trace_nfs4_test_lock_stateid(state, lsp, status);
2763                         if (status == -NFS4ERR_EXPIRED ||
2764                             status == -NFS4ERR_BAD_STATEID) {
2765                                 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2766                                 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2767                                 if (!recover_lost_locks)
2768                                         set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2769                         } else if (status != NFS_OK) {
2770                                 ret = status;
2771                                 nfs4_put_lock_state(prev);
2772                                 goto out;
2773                         }
2774                         spin_lock(&state->state_lock);
2775                 }
2776         }
2777         spin_unlock(&state->state_lock);
2778         nfs4_put_lock_state(prev);
2779 out:
2780         return ret;
2781 }
2782
2783 /**
2784  * nfs41_check_open_stateid - possibly free an open stateid
2785  *
2786  * @state: NFSv4 state for an inode
2787  *
2788  * Returns NFS_OK if recovery for this stateid is now finished.
2789  * Otherwise a negative NFS4ERR value is returned.
2790  */
2791 static int nfs41_check_open_stateid(struct nfs4_state *state)
2792 {
2793         struct nfs_server *server = NFS_SERVER(state->inode);
2794         nfs4_stateid *stateid = &state->open_stateid;
2795         struct rpc_cred *cred = state->owner->so_cred;
2796         int status;
2797
2798         if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2799                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)  {
2800                         if (nfs4_have_delegation(state->inode, state->state))
2801                                 return NFS_OK;
2802                         return -NFS4ERR_OPENMODE;
2803                 }
2804                 return -NFS4ERR_BAD_STATEID;
2805         }
2806         status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2807         trace_nfs4_test_open_stateid(state, NULL, status);
2808         if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2809                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2810                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2811                 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2812                 clear_bit(NFS_OPEN_STATE, &state->flags);
2813                 stateid->type = NFS4_INVALID_STATEID_TYPE;
2814                 return status;
2815         }
2816         if (nfs_open_stateid_recover_openmode(state))
2817                 return -NFS4ERR_OPENMODE;
2818         return NFS_OK;
2819 }
2820
2821 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2822 {
2823         int status;
2824
2825         nfs41_check_delegation_stateid(state);
2826         status = nfs41_check_expired_locks(state);
2827         if (status != NFS_OK)
2828                 return status;
2829         status = nfs41_check_open_stateid(state);
2830         if (status != NFS_OK)
2831                 status = nfs4_open_expired(sp, state);
2832         return status;
2833 }
2834 #endif
2835
2836 /*
2837  * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2838  * fields corresponding to attributes that were used to store the verifier.
2839  * Make sure we clobber those fields in the later setattr call
2840  */
2841 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2842                                 struct iattr *sattr, struct nfs4_label **label)
2843 {
2844         const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2845         __u32 attrset[3];
2846         unsigned ret;
2847         unsigned i;
2848
2849         for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2850                 attrset[i] = opendata->o_res.attrset[i];
2851                 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2852                         attrset[i] &= ~bitmask[i];
2853         }
2854
2855         ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2856                 sattr->ia_valid : 0;
2857
2858         if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2859                 if (sattr->ia_valid & ATTR_ATIME_SET)
2860                         ret |= ATTR_ATIME_SET;
2861                 else
2862                         ret |= ATTR_ATIME;
2863         }
2864
2865         if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2866                 if (sattr->ia_valid & ATTR_MTIME_SET)
2867                         ret |= ATTR_MTIME_SET;
2868                 else
2869                         ret |= ATTR_MTIME;
2870         }
2871
2872         if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
2873                 *label = NULL;
2874         return ret;
2875 }
2876
2877 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2878                 fmode_t fmode,
2879                 int flags,
2880                 struct nfs_open_context *ctx)
2881 {
2882         struct nfs4_state_owner *sp = opendata->owner;
2883         struct nfs_server *server = sp->so_server;
2884         struct dentry *dentry;
2885         struct nfs4_state *state;
2886         unsigned int seq;
2887         int ret;
2888
2889         seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2890
2891         ret = _nfs4_proc_open(opendata, ctx);
2892         if (ret != 0)
2893                 goto out;
2894
2895         state = _nfs4_opendata_to_nfs4_state(opendata);
2896         ret = PTR_ERR(state);
2897         if (IS_ERR(state))
2898                 goto out;
2899         ctx->state = state;
2900         if (server->caps & NFS_CAP_POSIX_LOCK)
2901                 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2902         if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2903                 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2904
2905         dentry = opendata->dentry;
2906         if (d_really_is_negative(dentry)) {
2907                 struct dentry *alias;
2908                 d_drop(dentry);
2909                 alias = d_exact_alias(dentry, state->inode);
2910                 if (!alias)
2911                         alias = d_splice_alias(igrab(state->inode), dentry);
2912                 /* d_splice_alias() can't fail here - it's a non-directory */
2913                 if (alias) {
2914                         dput(ctx->dentry);
2915                         ctx->dentry = dentry = alias;
2916                 }
2917                 nfs_set_verifier(dentry,
2918                                 nfs_save_change_attribute(d_inode(opendata->dir)));
2919         }
2920
2921         /* Parse layoutget results before we check for access */
2922         pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
2923
2924         ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2925         if (ret != 0)
2926                 goto out;
2927
2928         if (d_inode(dentry) == state->inode) {
2929                 nfs_inode_attach_open_context(ctx);
2930                 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2931                         nfs4_schedule_stateid_recovery(server, state);
2932         }
2933
2934 out:
2935         nfs4_sequence_free_slot(&opendata->o_res.seq_res);
2936         return ret;
2937 }
2938
2939 /*
2940  * Returns a referenced nfs4_state
2941  */
2942 static int _nfs4_do_open(struct inode *dir,
2943                         struct nfs_open_context *ctx,
2944                         int flags,
2945                         const struct nfs4_open_createattrs *c,
2946                         int *opened)
2947 {
2948         struct nfs4_state_owner  *sp;
2949         struct nfs4_state     *state = NULL;
2950         struct nfs_server       *server = NFS_SERVER(dir);
2951         struct nfs4_opendata *opendata;
2952         struct dentry *dentry = ctx->dentry;
2953         struct rpc_cred *cred = ctx->cred;
2954         struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2955         fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2956         enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2957         struct iattr *sattr = c->sattr;
2958         struct nfs4_label *label = c->label;
2959         struct nfs4_label *olabel = NULL;
2960         int status;
2961
2962         /* Protect against reboot recovery conflicts */
2963         status = -ENOMEM;
2964         sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2965         if (sp == NULL) {
2966                 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2967                 goto out_err;
2968         }
2969         status = nfs4_client_recover_expired_lease(server->nfs_client);
2970         if (status != 0)
2971                 goto err_put_state_owner;
2972         if (d_really_is_positive(dentry))
2973                 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2974         status = -ENOMEM;
2975         if (d_really_is_positive(dentry))
2976                 claim = NFS4_OPEN_CLAIM_FH;
2977         opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2978                         c, claim, GFP_KERNEL);
2979         if (opendata == NULL)
2980                 goto err_put_state_owner;
2981
2982         if (label) {
2983                 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2984                 if (IS_ERR(olabel)) {
2985                         status = PTR_ERR(olabel);
2986                         goto err_opendata_put;
2987                 }
2988         }
2989
2990         if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2991                 if (!opendata->f_attr.mdsthreshold) {
2992                         opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2993                         if (!opendata->f_attr.mdsthreshold)
2994                                 goto err_free_label;
2995                 }
2996                 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2997         }
2998         if (d_really_is_positive(dentry))
2999                 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3000
3001         status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
3002         if (status != 0)
3003                 goto err_free_label;
3004         state = ctx->state;
3005
3006         if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3007             (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3008                 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3009                 /*
3010                  * send create attributes which was not set by open
3011                  * with an extra setattr.
3012                  */
3013                 if (attrs || label) {
3014                         unsigned ia_old = sattr->ia_valid;
3015
3016                         sattr->ia_valid = attrs;
3017                         nfs_fattr_init(opendata->o_res.f_attr);
3018                         status = nfs4_do_setattr(state->inode, cred,
3019                                         opendata->o_res.f_attr, sattr,
3020                                         ctx, label, olabel);
3021                         if (status == 0) {
3022                                 nfs_setattr_update_inode(state->inode, sattr,
3023                                                 opendata->o_res.f_attr);
3024                                 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3025                         }
3026                         sattr->ia_valid = ia_old;
3027                 }
3028         }
3029         if (opened && opendata->file_created)
3030                 *opened = 1;
3031
3032         if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3033                 *ctx_th = opendata->f_attr.mdsthreshold;
3034                 opendata->f_attr.mdsthreshold = NULL;
3035         }
3036
3037         nfs4_label_free(olabel);
3038
3039         nfs4_opendata_put(opendata);
3040         nfs4_put_state_owner(sp);
3041         return 0;
3042 err_free_label:
3043         nfs4_label_free(olabel);
3044 err_opendata_put:
3045         nfs4_opendata_put(opendata);
3046 err_put_state_owner:
3047         nfs4_put_state_owner(sp);
3048 out_err:
3049         return status;
3050 }
3051
3052
3053 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3054                                         struct nfs_open_context *ctx,
3055                                         int flags,
3056                                         struct iattr *sattr,
3057                                         struct nfs4_label *label,
3058                                         int *opened)
3059 {
3060         struct nfs_server *server = NFS_SERVER(dir);
3061         struct nfs4_exception exception = { };
3062         struct nfs4_state *res;
3063         struct nfs4_open_createattrs c = {
3064                 .label = label,
3065                 .sattr = sattr,
3066                 .verf = {
3067                         [0] = (__u32)jiffies,
3068                         [1] = (__u32)current->pid,
3069                 },
3070         };
3071         int status;
3072
3073         do {
3074                 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3075                 res = ctx->state;
3076                 trace_nfs4_open_file(ctx, flags, status);
3077                 if (status == 0)
3078                         break;
3079                 /* NOTE: BAD_SEQID means the server and client disagree about the
3080                  * book-keeping w.r.t. state-changing operations
3081                  * (OPEN/CLOSE/LOCK/LOCKU...)
3082                  * It is actually a sign of a bug on the client or on the server.
3083                  *
3084                  * If we receive a BAD_SEQID error in the particular case of
3085                  * doing an OPEN, we assume that nfs_increment_open_seqid() will
3086                  * have unhashed the old state_owner for us, and that we can
3087                  * therefore safely retry using a new one. We should still warn
3088                  * the user though...
3089                  */
3090                 if (status == -NFS4ERR_BAD_SEQID) {
3091                         pr_warn_ratelimited("NFS: v4 server %s "
3092                                         " returned a bad sequence-id error!\n",
3093                                         NFS_SERVER(dir)->nfs_client->cl_hostname);
3094                         exception.retry = 1;
3095                         continue;
3096                 }
3097                 /*
3098                  * BAD_STATEID on OPEN means that the server cancelled our
3099                  * state before it received the OPEN_CONFIRM.
3100                  * Recover by retrying the request as per the discussion
3101                  * on Page 181 of RFC3530.
3102                  */
3103                 if (status == -NFS4ERR_BAD_STATEID) {
3104                         exception.retry = 1;
3105                         continue;
3106                 }
3107                 if (status == -EAGAIN) {
3108                         /* We must have found a delegation */
3109                         exception.retry = 1;
3110                         continue;
3111                 }
3112                 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3113                         continue;
3114                 res = ERR_PTR(nfs4_handle_exception(server,
3115                                         status, &exception));
3116         } while (exception.retry);
3117         return res;
3118 }
3119
3120 static int _nfs4_do_setattr(struct inode *inode,
3121                             struct nfs_setattrargs *arg,
3122                             struct nfs_setattrres *res,
3123                             struct rpc_cred *cred,
3124                             struct nfs_open_context *ctx)
3125 {
3126         struct nfs_server *server = NFS_SERVER(inode);
3127         struct rpc_message msg = {
3128                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3129                 .rpc_argp       = arg,
3130                 .rpc_resp       = res,
3131                 .rpc_cred       = cred,
3132         };
3133         struct rpc_cred *delegation_cred = NULL;
3134         unsigned long timestamp = jiffies;
3135         bool truncate;
3136         int status;
3137
3138         nfs_fattr_init(res->fattr);
3139
3140         /* Servers should only apply open mode checks for file size changes */
3141         truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3142         if (!truncate)
3143                 goto zero_stateid;
3144
3145         if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3146                 /* Use that stateid */
3147         } else if (ctx != NULL) {
3148                 struct nfs_lock_context *l_ctx;
3149                 if (!nfs4_valid_open_stateid(ctx->state))
3150                         return -EBADF;
3151                 l_ctx = nfs_get_lock_context(ctx);
3152                 if (IS_ERR(l_ctx))
3153                         return PTR_ERR(l_ctx);
3154                 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3155                                                 &arg->stateid, &delegation_cred);
3156                 nfs_put_lock_context(l_ctx);
3157                 if (status == -EIO)
3158                         return -EBADF;
3159         } else {
3160 zero_stateid:
3161                 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3162         }
3163         if (delegation_cred)
3164                 msg.rpc_cred = delegation_cred;
3165
3166         status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3167
3168         put_rpccred(delegation_cred);
3169         if (status == 0 && ctx != NULL)
3170                 renew_lease(server, timestamp);
3171         trace_nfs4_setattr(inode, &arg->stateid, status);
3172         return status;
3173 }
3174
3175 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3176                            struct nfs_fattr *fattr, struct iattr *sattr,
3177                            struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3178                            struct nfs4_label *olabel)
3179 {
3180         struct nfs_server *server = NFS_SERVER(inode);
3181         __u32 bitmask[NFS4_BITMASK_SZ];
3182         struct nfs4_state *state = ctx ? ctx->state : NULL;
3183         struct nfs_setattrargs  arg = {
3184                 .fh             = NFS_FH(inode),
3185                 .iap            = sattr,
3186                 .server         = server,
3187                 .bitmask = bitmask,
3188                 .label          = ilabel,
3189         };
3190         struct nfs_setattrres  res = {
3191                 .fattr          = fattr,
3192                 .label          = olabel,
3193                 .server         = server,
3194         };
3195         struct nfs4_exception exception = {
3196                 .state = state,
3197                 .inode = inode,
3198                 .stateid = &arg.stateid,
3199         };
3200         int err;
3201
3202         do {
3203                 nfs4_bitmap_copy_adjust_setattr(bitmask,
3204                                 nfs4_bitmask(server, olabel),
3205                                 inode);
3206
3207                 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3208                 switch (err) {
3209                 case -NFS4ERR_OPENMODE:
3210                         if (!(sattr->ia_valid & ATTR_SIZE)) {
3211                                 pr_warn_once("NFSv4: server %s is incorrectly "
3212                                                 "applying open mode checks to "
3213                                                 "a SETATTR that is not "
3214                                                 "changing file size.\n",
3215                                                 server->nfs_client->cl_hostname);
3216                         }
3217                         if (state && !(state->state & FMODE_WRITE)) {
3218                                 err = -EBADF;
3219                                 if (sattr->ia_valid & ATTR_OPEN)
3220                                         err = -EACCES;
3221                                 goto out;
3222                         }
3223                 }
3224                 err = nfs4_handle_exception(server, err, &exception);
3225         } while (exception.retry);
3226 out:
3227         return err;
3228 }
3229
3230 static bool
3231 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3232 {
3233         if (inode == NULL || !nfs_have_layout(inode))
3234                 return false;
3235
3236         return pnfs_wait_on_layoutreturn(inode, task);
3237 }
3238
3239 struct nfs4_closedata {
3240         struct inode *inode;
3241         struct nfs4_state *state;
3242         struct nfs_closeargs arg;
3243         struct nfs_closeres res;
3244         struct {
3245                 struct nfs4_layoutreturn_args arg;
3246                 struct nfs4_layoutreturn_res res;
3247                 struct nfs4_xdr_opaque_data ld_private;
3248                 u32 roc_barrier;
3249                 bool roc;
3250         } lr;
3251         struct nfs_fattr fattr;
3252         unsigned long timestamp;
3253 };
3254
3255 static void nfs4_free_closedata(void *data)
3256 {
3257         struct nfs4_closedata *calldata = data;
3258         struct nfs4_state_owner *sp = calldata->state->owner;
3259         struct super_block *sb = calldata->state->inode->i_sb;
3260
3261         if (calldata->lr.roc)
3262                 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3263                                 calldata->res.lr_ret);
3264         nfs4_put_open_state(calldata->state);
3265         nfs_free_seqid(calldata->arg.seqid);
3266         nfs4_put_state_owner(sp);
3267         nfs_sb_deactive(sb);
3268         kfree(calldata);
3269 }
3270
3271 static void nfs4_close_done(struct rpc_task *task, void *data)
3272 {
3273         struct nfs4_closedata *calldata = data;
3274         struct nfs4_state *state = calldata->state;
3275         struct nfs_server *server = NFS_SERVER(calldata->inode);
3276         nfs4_stateid *res_stateid = NULL;
3277         struct nfs4_exception exception = {
3278                 .state = state,
3279                 .inode = calldata->inode,
3280                 .stateid = &calldata->arg.stateid,
3281         };