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