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