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