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