x86/unwind: Move common code into update_stack_state()
[linux-2.6-microblaze.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                 rcu_read_unlock();
2447                 nfs_finish_clear_delegation_stateid(state, &stateid);
2448                 return;
2449         }
2450
2451         if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2452                 rcu_read_unlock();
2453                 return;
2454         }
2455
2456         cred = get_rpccred(delegation->cred);
2457         rcu_read_unlock();
2458         status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2459         trace_nfs4_test_delegation_stateid(state, NULL, status);
2460         if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2461                 nfs_finish_clear_delegation_stateid(state, &stateid);
2462
2463         put_rpccred(cred);
2464 }
2465
2466 /**
2467  * nfs41_check_expired_locks - possibly free a lock stateid
2468  *
2469  * @state: NFSv4 state for an inode
2470  *
2471  * Returns NFS_OK if recovery for this stateid is now finished.
2472  * Otherwise a negative NFS4ERR value is returned.
2473  */
2474 static int nfs41_check_expired_locks(struct nfs4_state *state)
2475 {
2476         int status, ret = NFS_OK;
2477         struct nfs4_lock_state *lsp, *prev = NULL;
2478         struct nfs_server *server = NFS_SERVER(state->inode);
2479
2480         if (!test_bit(LK_STATE_IN_USE, &state->flags))
2481                 goto out;
2482
2483         spin_lock(&state->state_lock);
2484         list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2485                 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2486                         struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2487
2488                         atomic_inc(&lsp->ls_count);
2489                         spin_unlock(&state->state_lock);
2490
2491                         nfs4_put_lock_state(prev);
2492                         prev = lsp;
2493
2494                         status = nfs41_test_and_free_expired_stateid(server,
2495                                         &lsp->ls_stateid,
2496                                         cred);
2497                         trace_nfs4_test_lock_stateid(state, lsp, status);
2498                         if (status == -NFS4ERR_EXPIRED ||
2499                             status == -NFS4ERR_BAD_STATEID) {
2500                                 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2501                                 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2502                                 if (!recover_lost_locks)
2503                                         set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2504                         } else if (status != NFS_OK) {
2505                                 ret = status;
2506                                 nfs4_put_lock_state(prev);
2507                                 goto out;
2508                         }
2509                         spin_lock(&state->state_lock);
2510                 }
2511         }
2512         spin_unlock(&state->state_lock);
2513         nfs4_put_lock_state(prev);
2514 out:
2515         return ret;
2516 }
2517
2518 /**
2519  * nfs41_check_open_stateid - possibly free an open stateid
2520  *
2521  * @state: NFSv4 state for an inode
2522  *
2523  * Returns NFS_OK if recovery for this stateid is now finished.
2524  * Otherwise a negative NFS4ERR value is returned.
2525  */
2526 static int nfs41_check_open_stateid(struct nfs4_state *state)
2527 {
2528         struct nfs_server *server = NFS_SERVER(state->inode);
2529         nfs4_stateid *stateid = &state->open_stateid;
2530         struct rpc_cred *cred = state->owner->so_cred;
2531         int status;
2532
2533         if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2534                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)  {
2535                         if (nfs4_have_delegation(state->inode, state->state))
2536                                 return NFS_OK;
2537                         return -NFS4ERR_OPENMODE;
2538                 }
2539                 return -NFS4ERR_BAD_STATEID;
2540         }
2541         status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2542         trace_nfs4_test_open_stateid(state, NULL, status);
2543         if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2544                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2545                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2546                 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2547                 clear_bit(NFS_OPEN_STATE, &state->flags);
2548                 stateid->type = NFS4_INVALID_STATEID_TYPE;
2549         }
2550         if (status != NFS_OK)
2551                 return status;
2552         if (nfs_open_stateid_recover_openmode(state))
2553                 return -NFS4ERR_OPENMODE;
2554         return NFS_OK;
2555 }
2556
2557 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2558 {
2559         int status;
2560
2561         nfs41_check_delegation_stateid(state);
2562         status = nfs41_check_expired_locks(state);
2563         if (status != NFS_OK)
2564                 return status;
2565         status = nfs41_check_open_stateid(state);
2566         if (status != NFS_OK)
2567                 status = nfs4_open_expired(sp, state);
2568         return status;
2569 }
2570 #endif
2571
2572 /*
2573  * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2574  * fields corresponding to attributes that were used to store the verifier.
2575  * Make sure we clobber those fields in the later setattr call
2576  */
2577 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2578                                 struct iattr *sattr, struct nfs4_label **label)
2579 {
2580         const u32 *attrset = opendata->o_res.attrset;
2581
2582         if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2583             !(sattr->ia_valid & ATTR_ATIME_SET))
2584                 sattr->ia_valid |= ATTR_ATIME;
2585
2586         if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2587             !(sattr->ia_valid & ATTR_MTIME_SET))
2588                 sattr->ia_valid |= ATTR_MTIME;
2589
2590         /* Except MODE, it seems harmless of setting twice. */
2591         if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2592                 attrset[1] & FATTR4_WORD1_MODE)
2593                 sattr->ia_valid &= ~ATTR_MODE;
2594
2595         if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2596                 *label = NULL;
2597 }
2598
2599 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2600                 fmode_t fmode,
2601                 int flags,
2602                 struct nfs_open_context *ctx)
2603 {
2604         struct nfs4_state_owner *sp = opendata->owner;
2605         struct nfs_server *server = sp->so_server;
2606         struct dentry *dentry;
2607         struct nfs4_state *state;
2608         unsigned int seq;
2609         int ret;
2610
2611         seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2612
2613         ret = _nfs4_proc_open(opendata);
2614         if (ret != 0)
2615                 goto out;
2616
2617         state = nfs4_opendata_to_nfs4_state(opendata);
2618         ret = PTR_ERR(state);
2619         if (IS_ERR(state))
2620                 goto out;
2621         ctx->state = state;
2622         if (server->caps & NFS_CAP_POSIX_LOCK)
2623                 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2624         if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2625                 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2626
2627         dentry = opendata->dentry;
2628         if (d_really_is_negative(dentry)) {
2629                 struct dentry *alias;
2630                 d_drop(dentry);
2631                 alias = d_exact_alias(dentry, state->inode);
2632                 if (!alias)
2633                         alias = d_splice_alias(igrab(state->inode), dentry);
2634                 /* d_splice_alias() can't fail here - it's a non-directory */
2635                 if (alias) {
2636                         dput(ctx->dentry);
2637                         ctx->dentry = dentry = alias;
2638                 }
2639                 nfs_set_verifier(dentry,
2640                                 nfs_save_change_attribute(d_inode(opendata->dir)));
2641         }
2642
2643         ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2644         if (ret != 0)
2645                 goto out;
2646
2647         if (d_inode(dentry) == state->inode) {
2648                 nfs_inode_attach_open_context(ctx);
2649                 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2650                         nfs4_schedule_stateid_recovery(server, state);
2651         }
2652 out:
2653         return ret;
2654 }
2655
2656 /*
2657  * Returns a referenced nfs4_state
2658  */
2659 static int _nfs4_do_open(struct inode *dir,
2660                         struct nfs_open_context *ctx,
2661                         int flags,
2662                         struct iattr *sattr,
2663                         struct nfs4_label *label,
2664                         int *opened)
2665 {
2666         struct nfs4_state_owner  *sp;
2667         struct nfs4_state     *state = NULL;
2668         struct nfs_server       *server = NFS_SERVER(dir);
2669         struct nfs4_opendata *opendata;
2670         struct dentry *dentry = ctx->dentry;
2671         struct rpc_cred *cred = ctx->cred;
2672         struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2673         fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2674         enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2675         struct nfs4_label *olabel = NULL;
2676         int status;
2677
2678         /* Protect against reboot recovery conflicts */
2679         status = -ENOMEM;
2680         sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2681         if (sp == NULL) {
2682                 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2683                 goto out_err;
2684         }
2685         status = nfs4_client_recover_expired_lease(server->nfs_client);
2686         if (status != 0)
2687                 goto err_put_state_owner;
2688         if (d_really_is_positive(dentry))
2689                 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2690         status = -ENOMEM;
2691         if (d_really_is_positive(dentry))
2692                 claim = NFS4_OPEN_CLAIM_FH;
2693         opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2694                         label, claim, GFP_KERNEL);
2695         if (opendata == NULL)
2696                 goto err_put_state_owner;
2697
2698         if (label) {
2699                 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2700                 if (IS_ERR(olabel)) {
2701                         status = PTR_ERR(olabel);
2702                         goto err_opendata_put;
2703                 }
2704         }
2705
2706         if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2707                 if (!opendata->f_attr.mdsthreshold) {
2708                         opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2709                         if (!opendata->f_attr.mdsthreshold)
2710                                 goto err_free_label;
2711                 }
2712                 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2713         }
2714         if (d_really_is_positive(dentry))
2715                 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2716
2717         status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2718         if (status != 0)
2719                 goto err_free_label;
2720         state = ctx->state;
2721
2722         if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2723             (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2724                 nfs4_exclusive_attrset(opendata, sattr, &label);
2725                 /*
2726                  * send create attributes which was not set by open
2727                  * with an extra setattr.
2728                  */
2729                 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2730                         nfs_fattr_init(opendata->o_res.f_attr);
2731                         status = nfs4_do_setattr(state->inode, cred,
2732                                         opendata->o_res.f_attr, sattr,
2733                                         ctx, label, olabel);
2734                         if (status == 0) {
2735                                 nfs_setattr_update_inode(state->inode, sattr,
2736                                                 opendata->o_res.f_attr);
2737                                 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2738                         }
2739                 }
2740         }
2741         if (opened && opendata->file_created)
2742                 *opened |= FILE_CREATED;
2743
2744         if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2745                 *ctx_th = opendata->f_attr.mdsthreshold;
2746                 opendata->f_attr.mdsthreshold = NULL;
2747         }
2748
2749         nfs4_label_free(olabel);
2750
2751         nfs4_opendata_put(opendata);
2752         nfs4_put_state_owner(sp);
2753         return 0;
2754 err_free_label:
2755         nfs4_label_free(olabel);
2756 err_opendata_put:
2757         nfs4_opendata_put(opendata);
2758 err_put_state_owner:
2759         nfs4_put_state_owner(sp);
2760 out_err:
2761         return status;
2762 }
2763
2764
2765 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2766                                         struct nfs_open_context *ctx,
2767                                         int flags,
2768                                         struct iattr *sattr,
2769                                         struct nfs4_label *label,
2770                                         int *opened)
2771 {
2772         struct nfs_server *server = NFS_SERVER(dir);
2773         struct nfs4_exception exception = { };
2774         struct nfs4_state *res;
2775         int status;
2776
2777         do {
2778                 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2779                 res = ctx->state;
2780                 trace_nfs4_open_file(ctx, flags, status);
2781                 if (status == 0)
2782                         break;
2783                 /* NOTE: BAD_SEQID means the server and client disagree about the
2784                  * book-keeping w.r.t. state-changing operations
2785                  * (OPEN/CLOSE/LOCK/LOCKU...)
2786                  * It is actually a sign of a bug on the client or on the server.
2787                  *
2788                  * If we receive a BAD_SEQID error in the particular case of
2789                  * doing an OPEN, we assume that nfs_increment_open_seqid() will
2790                  * have unhashed the old state_owner for us, and that we can
2791                  * therefore safely retry using a new one. We should still warn
2792                  * the user though...
2793                  */
2794                 if (status == -NFS4ERR_BAD_SEQID) {
2795                         pr_warn_ratelimited("NFS: v4 server %s "
2796                                         " returned a bad sequence-id error!\n",
2797                                         NFS_SERVER(dir)->nfs_client->cl_hostname);
2798                         exception.retry = 1;
2799                         continue;
2800                 }
2801                 /*
2802                  * BAD_STATEID on OPEN means that the server cancelled our
2803                  * state before it received the OPEN_CONFIRM.
2804                  * Recover by retrying the request as per the discussion
2805                  * on Page 181 of RFC3530.
2806                  */
2807                 if (status == -NFS4ERR_BAD_STATEID) {
2808                         exception.retry = 1;
2809                         continue;
2810                 }
2811                 if (status == -EAGAIN) {
2812                         /* We must have found a delegation */
2813                         exception.retry = 1;
2814                         continue;
2815                 }
2816                 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2817                         continue;
2818                 res = ERR_PTR(nfs4_handle_exception(server,
2819                                         status, &exception));
2820         } while (exception.retry);
2821         return res;
2822 }
2823
2824 static int _nfs4_do_setattr(struct inode *inode,
2825                             struct nfs_setattrargs *arg,
2826                             struct nfs_setattrres *res,
2827                             struct rpc_cred *cred,
2828                             struct nfs_open_context *ctx)
2829 {
2830         struct nfs_server *server = NFS_SERVER(inode);
2831         struct rpc_message msg = {
2832                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2833                 .rpc_argp       = arg,
2834                 .rpc_resp       = res,
2835                 .rpc_cred       = cred,
2836         };
2837         struct rpc_cred *delegation_cred = NULL;
2838         unsigned long timestamp = jiffies;
2839         fmode_t fmode;
2840         bool truncate;
2841         int status;
2842
2843         nfs_fattr_init(res->fattr);
2844
2845         /* Servers should only apply open mode checks for file size changes */
2846         truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2847         fmode = truncate ? FMODE_WRITE : FMODE_READ;
2848
2849         if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2850                 /* Use that stateid */
2851         } else if (truncate && ctx != NULL) {
2852                 struct nfs_lock_context *l_ctx;
2853                 if (!nfs4_valid_open_stateid(ctx->state))
2854                         return -EBADF;
2855                 l_ctx = nfs_get_lock_context(ctx);
2856                 if (IS_ERR(l_ctx))
2857                         return PTR_ERR(l_ctx);
2858                 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2859                                                 &arg->stateid, &delegation_cred);
2860                 nfs_put_lock_context(l_ctx);
2861                 if (status == -EIO)
2862                         return -EBADF;
2863         } else
2864                 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2865         if (delegation_cred)
2866                 msg.rpc_cred = delegation_cred;
2867
2868         status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2869
2870         put_rpccred(delegation_cred);
2871         if (status == 0 && ctx != NULL)
2872                 renew_lease(server, timestamp);
2873         trace_nfs4_setattr(inode, &arg->stateid, status);
2874         return status;
2875 }
2876
2877 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2878                            struct nfs_fattr *fattr, struct iattr *sattr,
2879                            struct nfs_open_context *ctx, struct nfs4_label *ilabel,
2880                            struct nfs4_label *olabel)
2881 {
2882         struct nfs_server *server = NFS_SERVER(inode);
2883         struct nfs4_state *state = ctx ? ctx->state : NULL;
2884         struct nfs_setattrargs  arg = {
2885                 .fh             = NFS_FH(inode),
2886                 .iap            = sattr,
2887                 .server         = server,
2888                 .bitmask = server->attr_bitmask,
2889                 .label          = ilabel,
2890         };
2891         struct nfs_setattrres  res = {
2892                 .fattr          = fattr,
2893                 .label          = olabel,
2894                 .server         = server,
2895         };
2896         struct nfs4_exception exception = {
2897                 .state = state,
2898                 .inode = inode,
2899                 .stateid = &arg.stateid,
2900         };
2901         int err;
2902
2903         arg.bitmask = nfs4_bitmask(server, ilabel);
2904         if (ilabel)
2905                 arg.bitmask = nfs4_bitmask(server, olabel);
2906
2907         do {
2908                 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
2909                 switch (err) {
2910                 case -NFS4ERR_OPENMODE:
2911                         if (!(sattr->ia_valid & ATTR_SIZE)) {
2912                                 pr_warn_once("NFSv4: server %s is incorrectly "
2913                                                 "applying open mode checks to "
2914                                                 "a SETATTR that is not "
2915                                                 "changing file size.\n",
2916                                                 server->nfs_client->cl_hostname);
2917                         }
2918                         if (state && !(state->state & FMODE_WRITE)) {
2919                                 err = -EBADF;
2920                                 if (sattr->ia_valid & ATTR_OPEN)
2921                                         err = -EACCES;
2922                                 goto out;
2923                         }
2924                 }
2925                 err = nfs4_handle_exception(server, err, &exception);
2926         } while (exception.retry);
2927 out:
2928         return err;
2929 }
2930
2931 static bool
2932 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2933 {
2934         if (inode == NULL || !nfs_have_layout(inode))
2935                 return false;
2936
2937         return pnfs_wait_on_layoutreturn(inode, task);
2938 }
2939
2940 struct nfs4_closedata {
2941         struct inode *inode;
2942         struct nfs4_state *state;
2943         struct nfs_closeargs arg;
2944         struct nfs_closeres res;
2945         struct {
2946                 struct nfs4_layoutreturn_args arg;
2947                 struct nfs4_layoutreturn_res res;
2948                 struct nfs4_xdr_opaque_data ld_private;
2949                 u32 roc_barrier;
2950                 bool roc;
2951         } lr;
2952         struct nfs_fattr fattr;
2953         unsigned long timestamp;
2954 };
2955
2956 static void nfs4_free_closedata(void *data)
2957 {
2958         struct nfs4_closedata *calldata = data;
2959         struct nfs4_state_owner *sp = calldata->state->owner;
2960         struct super_block *sb = calldata->state->inode->i_sb;
2961
2962         if (calldata->lr.roc)
2963                 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
2964                                 calldata->res.lr_ret);
2965         nfs4_put_open_state(calldata->state);
2966         nfs_free_seqid(calldata->arg.seqid);
2967         nfs4_put_state_owner(sp);
2968         nfs_sb_deactive(sb);
2969         kfree(calldata);
2970 }
2971
2972 static void nfs4_close_done(struct rpc_task *task, void *data)
2973 {
2974         struct nfs4_closedata *calldata = data;
2975         struct nfs4_state *state = calldata->state;
2976         struct nfs_server *server = NFS_SERVER(calldata->inode);
2977         nfs4_stateid *res_stateid = NULL;
2978
2979         dprintk("%s: begin!\n", __func__);
2980         if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2981                 return;
2982         trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
2983
2984         /* Handle Layoutreturn errors */
2985         if (calldata->arg.lr_args && task->tk_status != 0) {
2986                 switch (calldata->res.lr_ret) {
2987                 default:
2988                         calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
2989                         break;
2990                 case 0:
2991                         calldata->arg.lr_args = NULL;
2992                         calldata->res.lr_res = NULL;
2993                         break;
2994                 case -NFS4ERR_ADMIN_REVOKED:
2995                 case -NFS4ERR_DELEG_REVOKED:
2996                 case -NFS4ERR_EXPIRED:
2997                 case -NFS4ERR_BAD_STATEID:
2998                 case -NFS4ERR_OLD_STATEID:
2999                 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3000                 case -NFS4ERR_WRONG_CRED:
3001                         calldata->arg.lr_args = NULL;
3002                         calldata->res.lr_res = NULL;
3003                         calldata->res.lr_ret = 0;
3004                         rpc_restart_call_prepare(task);
3005                         return;
3006                 }
3007         }
3008
3009         /* hmm. we are done with the inode, and in the process of freeing
3010          * the state_owner. we keep this around to process errors
3011          */
3012         switch (task->tk_status) {
3013                 case 0:
3014                         res_stateid = &calldata->res.stateid;
3015                         renew_lease(server, calldata->timestamp);
3016                         break;
3017                 case -NFS4ERR_ACCESS:
3018                         if (calldata->arg.bitmask != NULL) {
3019                                 calldata->arg.bitmask = NULL;
3020                                 calldata->res.fattr = NULL;
3021                                 task->tk_status = 0;
3022                                 rpc_restart_call_prepare(task);
3023                                 goto out_release;
3024
3025                         }
3026                         break;
3027                 case -NFS4ERR_ADMIN_REVOKED:
3028                 case -NFS4ERR_STALE_STATEID:
3029                 case -NFS4ERR_EXPIRED:
3030                         nfs4_free_revoked_stateid(server,
3031                                         &calldata->arg.stateid,
3032                                         task->tk_msg.rpc_cred);
3033                 case -NFS4ERR_OLD_STATEID:
3034                 case -NFS4ERR_BAD_STATEID:
3035                         if (!nfs4_stateid_match(&calldata->arg.stateid,
3036                                                 &state->open_stateid)) {
3037                                 rpc_restart_call_prepare(task);
3038                                 goto out_release;
3039                         }
3040                         if (calldata->arg.fmode == 0)
3041                                 break;
3042                 default:
3043                         if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
3044                                 rpc_restart_call_prepare(task);
3045                                 goto out_release;
3046                         }
3047         }
3048         nfs_clear_open_stateid(state, &calldata->arg.stateid,
3049                         res_stateid, calldata->arg.fmode);
3050 out_release:
3051         nfs_release_seqid(calldata->arg.seqid);
3052         nfs_refresh_inode(calldata->inode, &calldata->fattr);
3053         dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3054 }
3055
3056 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3057 {
3058         struct nfs4_closedata *calldata = data;
3059         struct nfs4_state *state = calldata->state;
3060         struct inode *inode = calldata->inode;
3061         bool is_rdonly, is_wronly, is_rdwr;
3062         int call_close = 0;
3063
3064         dprintk("%s: begin!\n", __func__);
3065         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3066                 goto out_wait;
3067
3068         task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3069         spin_lock(&state->owner->so_lock);
3070         is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3071         is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3072         is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3073         nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
3074         /* Calculate the change in open mode */
3075         calldata->arg.fmode = 0;
3076         if (state->n_rdwr == 0) {
3077                 if (state->n_rdonly == 0)
3078                         call_close |= is_rdonly;
3079                 else if (is_rdonly)
3080                         calldata->arg.fmode |= FMODE_READ;
3081                 if (state->n_wronly == 0)
3082                         call_close |= is_wronly;
3083                 else if (is_wronly)
3084                         calldata->arg.fmode |= FMODE_WRITE;
3085                 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3086                         call_close |= is_rdwr;
3087         } else if (is_rdwr)
3088                 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3089
3090         if (!nfs4_valid_open_stateid(state) ||
3091             test_bit(NFS_OPEN_STATE, &state->flags) == 0)
3092                 call_close = 0;
3093         spin_unlock(&state->owner->so_lock);
3094
3095         if (!call_close) {
3096                 /* Note: exit _without_ calling nfs4_close_done */
3097                 goto out_no_action;
3098         }
3099
3100         if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3101                 nfs_release_seqid(calldata->arg.seqid);
3102                 goto out_wait;
3103         }
3104
3105         if (calldata->arg.fmode == 0)
3106                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3107
3108         if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3109                 /* Close-to-open cache consistency revalidation */
3110                 if (!nfs4_have_delegation(inode, FMODE_READ))
3111                         calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3112                 else
3113                         calldata->arg.bitmask = NULL;
3114         }
3115
3116         calldata->arg.share_access =
3117                 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3118                                 calldata->arg.fmode, 0);
3119
3120         if (calldata->res.fattr == NULL)
3121                 calldata->arg.bitmask = NULL;
3122         else if (calldata->arg.bitmask == NULL)
3123                 calldata->res.fattr = NULL;
3124         calldata->timestamp = jiffies;
3125         if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3126                                 &calldata->arg.seq_args,
3127                                 &calldata->res.seq_res,
3128                                 task) != 0)
3129                 nfs_release_seqid(calldata->arg.seqid);
3130         dprintk("%s: done!\n", __func__);
3131         return;
3132 out_no_action:
3133         task->tk_action = NULL;
3134 out_wait:
3135         nfs4_sequence_done(task, &calldata->res.seq_res);
3136 }
3137
3138 static const struct rpc_call_ops nfs4_close_ops = {
3139         .rpc_call_prepare = nfs4_close_prepare,
3140         .rpc_call_done = nfs4_close_done,
3141         .rpc_release = nfs4_free_closedata,
3142 };
3143
3144 /* 
3145  * It is possible for data to be read/written from a mem-mapped file 
3146  * after the sys_close call (which hits the vfs layer as a flush).
3147  * This means that we can't safely call nfsv4 close on a file until 
3148  * the inode is cleared. This in turn means that we are not good
3149  * NFSv4 citizens - we do not indicate to the server to update the file's 
3150  * share state even when we are done with one of the three share 
3151  * stateid's in the inode.
3152  *
3153  * NOTE: Caller must be holding the sp->so_owner semaphore!
3154  */
3155 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3156 {
3157         struct nfs_server *server = NFS_SERVER(state->inode);
3158         struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3159         struct nfs4_closedata *calldata;
3160         struct nfs4_state_owner *sp = state->owner;
3161         struct rpc_task *task;
3162         struct rpc_message msg = {
3163                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3164                 .rpc_cred = state->owner->so_cred,
3165         };
3166         struct rpc_task_setup task_setup_data = {
3167                 .rpc_client = server->client,
3168                 .rpc_message = &msg,
3169                 .callback_ops = &nfs4_close_ops,
3170                 .workqueue = nfsiod_workqueue,
3171                 .flags = RPC_TASK_ASYNC,
3172         };
3173         int status = -ENOMEM;
3174
3175         nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3176                 &task_setup_data.rpc_client, &msg);
3177
3178         calldata = kzalloc(sizeof(*calldata), gfp_mask);
3179         if (calldata == NULL)
3180                 goto out;
3181         nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
3182         calldata->inode = state->inode;
3183         calldata->state = state;
3184         calldata->arg.fh = NFS_FH(state->inode);
3185         /* Serialization for the sequence id */
3186         alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3187         calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3188         if (IS_ERR(calldata->arg.seqid))
3189                 goto out_free_calldata;
3190         nfs_fattr_init(&calldata->fattr);
3191         calldata->arg.fmode = 0;
3192         calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3193         calldata->res.fattr = &calldata->fattr;
3194         calldata->res.seqid = calldata->arg.seqid;
3195         calldata->res.server = server;
3196         calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3197         calldata->lr.roc = pnfs_roc(state->inode,
3198                         &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3199         if (calldata->lr.roc) {
3200                 calldata->arg.lr_args = &calldata->lr.arg;
3201                 calldata->res.lr_res = &calldata->lr.res;
3202         }
3203         nfs_sb_active(calldata->inode->i_sb);
3204
3205         msg.rpc_argp = &calldata->arg;
3206         msg.rpc_resp = &calldata->res;
3207         task_setup_data.callback_data = calldata;
3208         task = rpc_run_task(&task_setup_data);
3209         if (IS_ERR(task))
3210                 return PTR_ERR(task);
3211         status = 0;
3212         if (wait)
3213                 status = rpc_wait_for_completion_task(task);
3214         rpc_put_task(task);
3215         return status;
3216 out_free_calldata:
3217         kfree(calldata);
3218 out:
3219         nfs4_put_open_state(state);
3220         nfs4_put_state_owner(sp);
3221         return status;
3222 }
3223
3224 static struct inode *
3225 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3226                 int open_flags, struct iattr *attr, int *opened)
3227 {
3228         struct nfs4_state *state;
3229         struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3230
3231         label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3232
3233         /* Protect against concurrent sillydeletes */
3234         state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3235
3236         nfs4_label_release_security(label);
3237
3238         if (IS_ERR(state))
3239                 return ERR_CAST(state);
3240         return state->inode;
3241 }
3242
3243 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3244 {
3245         if (ctx->state == NULL)
3246                 return;
3247         if (is_sync)
3248                 nfs4_close_sync(ctx->state, ctx->mode);
3249         else
3250                 nfs4_close_state(ctx->state, ctx->mode);
3251 }
3252
3253 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3254 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3255 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3256
3257 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3258 {
3259         u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3260         struct nfs4_server_caps_arg args = {
3261                 .fhandle = fhandle,
3262                 .bitmask = bitmask,
3263         };
3264         struct nfs4_server_caps_res res = {};
3265         struct rpc_message msg = {
3266                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3267                 .rpc_argp = &args,
3268                 .rpc_resp = &res,
3269         };
3270         int status;
3271
3272         bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3273                      FATTR4_WORD0_FH_EXPIRE_TYPE |
3274                      FATTR4_WORD0_LINK_SUPPORT |
3275                      FATTR4_WORD0_SYMLINK_SUPPORT |
3276                      FATTR4_WORD0_ACLSUPPORT;
3277         if (minorversion)
3278                 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3279
3280         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3281         if (status == 0) {
3282                 /* Sanity check the server answers */
3283                 switch (minorversion) {
3284                 case 0:
3285                         res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3286                         res.attr_bitmask[2] = 0;
3287                         break;
3288                 case 1:
3289                         res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3290                         break;
3291                 case 2:
3292                         res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3293                 }
3294                 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3295                 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3296                                 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3297                                 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3298                                 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3299                                 NFS_CAP_CTIME|NFS_CAP_MTIME|
3300                                 NFS_CAP_SECURITY_LABEL);
3301                 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3302                                 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3303                         server->caps |= NFS_CAP_ACLS;
3304                 if (res.has_links != 0)
3305                         server->caps |= NFS_CAP_HARDLINKS;
3306                 if (res.has_symlinks != 0)
3307                         server->caps |= NFS_CAP_SYMLINKS;
3308                 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3309                         server->caps |= NFS_CAP_FILEID;
3310                 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3311                         server->caps |= NFS_CAP_MODE;
3312                 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3313                         server->caps |= NFS_CAP_NLINK;
3314                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3315                         server->caps |= NFS_CAP_OWNER;
3316                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3317                         server->caps |= NFS_CAP_OWNER_GROUP;
3318                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3319                         server->caps |= NFS_CAP_ATIME;
3320                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3321                         server->caps |= NFS_CAP_CTIME;
3322                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3323                         server->caps |= NFS_CAP_MTIME;
3324 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3325                 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3326                         server->caps |= NFS_CAP_SECURITY_LABEL;
3327 #endif
3328                 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3329                                 sizeof(server->attr_bitmask));
3330                 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3331
3332                 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3333                 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3334                 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3335                 server->cache_consistency_bitmask[2] = 0;
3336                 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3337                         sizeof(server->exclcreat_bitmask));
3338                 server->acl_bitmask = res.acl_bitmask;
3339                 server->fh_expire_type = res.fh_expire_type;
3340         }
3341
3342         return status;
3343 }
3344
3345 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3346 {
3347         struct nfs4_exception exception = { };
3348         int err;
3349         do {
3350                 err = nfs4_handle_exception(server,
3351                                 _nfs4_server_capabilities(server, fhandle),
3352                                 &exception);
3353         } while (exception.retry);
3354         return err;
3355 }
3356
3357 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3358                 struct nfs_fsinfo *info)
3359 {
3360         u32 bitmask[3];
3361         struct nfs4_lookup_root_arg args = {
3362                 .bitmask = bitmask,
3363         };
3364         struct nfs4_lookup_res res = {
3365                 .server = server,
3366                 .fattr = info->fattr,
3367                 .fh = fhandle,
3368         };
3369         struct rpc_message msg = {
3370                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3371                 .rpc_argp = &args,
3372                 .rpc_resp = &res,
3373         };
3374
3375         bitmask[0] = nfs4_fattr_bitmap[0];
3376         bitmask[1] = nfs4_fattr_bitmap[1];
3377         /*
3378          * Process the label in the upcoming getfattr
3379          */
3380         bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3381
3382         nfs_fattr_init(info->fattr);
3383         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3384 }
3385
3386 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3387                 struct nfs_fsinfo *info)
3388 {
3389         struct nfs4_exception exception = { };
3390         int err;
3391         do {
3392                 err = _nfs4_lookup_root(server, fhandle, info);
3393                 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3394                 switch (err) {
3395                 case 0:
3396                 case -NFS4ERR_WRONGSEC:
3397                         goto out;
3398                 default:
3399                         err = nfs4_handle_exception(server, err, &exception);
3400                 }
3401         } while (exception.retry);
3402 out:
3403         return err;
3404 }
3405
3406 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3407                                 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3408 {
3409         struct rpc_auth_create_args auth_args = {
3410                 .pseudoflavor = flavor,
3411         };
3412         struct rpc_auth *auth;
3413
3414         auth = rpcauth_create(&auth_args, server->client);
3415         if (IS_ERR(auth))
3416                 return -EACCES;
3417         return nfs4_lookup_root(server, fhandle, info);
3418 }
3419
3420 /*
3421  * Retry pseudoroot lookup with various security flavors.  We do this when:
3422  *
3423  *   NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3424  *   NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3425  *
3426  * Returns zero on success, or a negative NFS4ERR value, or a
3427  * negative errno value.
3428  */
3429 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3430                               struct nfs_fsinfo *info)
3431 {
3432         /* Per 3530bis 15.33.5 */
3433         static const rpc_authflavor_t flav_array[] = {
3434                 RPC_AUTH_GSS_KRB5P,
3435                 RPC_AUTH_GSS_KRB5I,
3436                 RPC_AUTH_GSS_KRB5,
3437                 RPC_AUTH_UNIX,                  /* courtesy */
3438                 RPC_AUTH_NULL,
3439         };
3440         int status = -EPERM;
3441         size_t i;
3442
3443         if (server->auth_info.flavor_len > 0) {
3444                 /* try each flavor specified by user */
3445                 for (i = 0; i < server->auth_info.flavor_len; i++) {
3446                         status = nfs4_lookup_root_sec(server, fhandle, info,
3447                                                 server->auth_info.flavors[i]);
3448                         if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3449                                 continue;
3450                         break;
3451                 }
3452         } else {
3453                 /* no flavors specified by user, try default list */
3454                 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3455                         status = nfs4_lookup_root_sec(server, fhandle, info,
3456                                                       flav_array[i]);
3457                         if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3458                                 continue;
3459                         break;
3460                 }
3461         }
3462
3463         /*
3464          * -EACCESS could mean that the user doesn't have correct permissions
3465          * to access the mount.  It could also mean that we tried to mount
3466          * with a gss auth flavor, but rpc.gssd isn't running.  Either way,
3467          * existing mount programs don't handle -EACCES very well so it should
3468          * be mapped to -EPERM instead.
3469          */
3470         if (status == -EACCES)
3471                 status = -EPERM;
3472         return status;
3473 }
3474
3475 /**
3476  * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3477  * @server: initialized nfs_server handle
3478  * @fhandle: we fill in the pseudo-fs root file handle
3479  * @info: we fill in an FSINFO struct
3480  * @auth_probe: probe the auth flavours
3481  *
3482  * Returns zero on success, or a negative errno.
3483  */
3484 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3485                          struct nfs_fsinfo *info,
3486                          bool auth_probe)
3487 {
3488         int status = 0;
3489
3490         if (!auth_probe)
3491                 status = nfs4_lookup_root(server, fhandle, info);
3492
3493         if (auth_probe || status == NFS4ERR_WRONGSEC)
3494                 status = server->nfs_client->cl_mvops->find_root_sec(server,
3495                                 fhandle, info);
3496
3497         if (status == 0)
3498                 status = nfs4_server_capabilities(server, fhandle);
3499         if (status == 0)
3500                 status = nfs4_do_fsinfo(server, fhandle, info);
3501
3502         return nfs4_map_errors(status);
3503 }
3504
3505 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3506                               struct nfs_fsinfo *info)
3507 {
3508         int error;
3509         struct nfs_fattr *fattr = info->fattr;
3510         struct nfs4_label *label = NULL;
3511
3512         error = nfs4_server_capabilities(server, mntfh);
3513         if (error < 0) {
3514                 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3515                 return error;
3516         }
3517
3518         label = nfs4_label_alloc(server, GFP_KERNEL);
3519         if (IS_ERR(label))
3520                 return PTR_ERR(label);
3521
3522         error = nfs4_proc_getattr(server, mntfh, fattr, label);
3523         if (error < 0) {
3524                 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3525                 goto err_free_label;
3526         }
3527
3528         if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3529             !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3530                 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3531
3532 err_free_label:
3533         nfs4_label_free(label);
3534
3535         return error;
3536 }
3537
3538 /*
3539  * Get locations and (maybe) other attributes of a referral.
3540  * Note that we'll actually follow the referral later when
3541  * we detect fsid mismatch in inode revalidation
3542  */
3543 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3544                              const struct qstr *name, struct nfs_fattr *fattr,
3545                              struct nfs_fh *fhandle)
3546 {
3547         int status = -ENOMEM;
3548         struct page *page = NULL;
3549         struct nfs4_fs_locations *locations = NULL;
3550
3551         page = alloc_page(GFP_KERNEL);
3552         if (page == NULL)
3553                 goto out;
3554         locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3555         if (locations == NULL)
3556                 goto out;
3557
3558         status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3559         if (status != 0)
3560                 goto out;
3561
3562         /*
3563          * If the fsid didn't change, this is a migration event, not a
3564          * referral.  Cause us to drop into the exception handler, which
3565          * will kick off migration recovery.
3566          */
3567         if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3568                 dprintk("%s: server did not return a different fsid for"
3569                         " a referral at %s\n", __func__, name->name);
3570                 status = -NFS4ERR_MOVED;
3571                 goto out;
3572         }
3573         /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3574         nfs_fixup_referral_attributes(&locations->fattr);
3575
3576         /* replace the lookup nfs_fattr with the locations nfs_fattr */
3577         memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3578         memset(fhandle, 0, sizeof(struct nfs_fh));
3579 out:
3580         if (page)
3581                 __free_page(page);
3582         kfree(locations);
3583         return status;
3584 }
3585
3586 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3587                                 struct nfs_fattr *fattr, struct nfs4_label *label)
3588 {
3589         struct nfs4_getattr_arg args = {
3590                 .fh = fhandle,
3591                 .bitmask = server->attr_bitmask,
3592         };
3593         struct nfs4_getattr_res res = {
3594                 .fattr = fattr,
3595                 .label = label,
3596                 .server = server,
3597         };
3598         struct rpc_message msg = {
3599                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3600                 .rpc_argp = &args,
3601                 .rpc_resp = &res,
3602         };
3603
3604         args.bitmask = nfs4_bitmask(server, label);
3605
3606         nfs_fattr_init(fattr);
3607         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3608 }
3609
3610 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3611                                 struct nfs_fattr *fattr, struct nfs4_label *label)
3612 {
3613         struct nfs4_exception exception = { };
3614         int err;
3615         do {
3616                 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3617                 trace_nfs4_getattr(server, fhandle, fattr, err);
3618                 err = nfs4_handle_exception(server, err,
3619                                 &exception);
3620         } while (exception.retry);
3621         return err;
3622 }
3623
3624 /* 
3625  * The file is not closed if it is opened due to the a request to change
3626  * the size of the file. The open call will not be needed once the
3627  * VFS layer lookup-intents are implemented.
3628  *
3629  * Close is called when the inode is destroyed.
3630  * If we haven't opened the file for O_WRONLY, we
3631  * need to in the size_change case to obtain a stateid.
3632  *
3633  * Got race?
3634  * Because OPEN is always done by name in nfsv4, it is
3635  * possible that we opened a different file by the same
3636  * name.  We can recognize this race condition, but we
3637  * can't do anything about it besides returning an error.
3638  *
3639  * This will be fixed with VFS changes (lookup-intent).
3640  */
3641 static int
3642 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3643                   struct iattr *sattr)
3644 {
3645         struct inode *inode = d_inode(dentry);
3646         struct rpc_cred *cred = NULL;
3647         struct nfs_open_context *ctx = NULL;
3648         struct nfs4_label *label = NULL;
3649         int status;
3650
3651         if (pnfs_ld_layoutret_on_setattr(inode) &&
3652             sattr->ia_valid & ATTR_SIZE &&
3653             sattr->ia_size < i_size_read(inode))
3654                 pnfs_commit_and_return_layout(inode);
3655
3656         nfs_fattr_init(fattr);
3657         
3658         /* Deal with open(O_TRUNC) */
3659         if (sattr->ia_valid & ATTR_OPEN)
3660                 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3661
3662         /* Optimization: if the end result is no change, don't RPC */
3663         if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3664                 return 0;
3665
3666         /* Search for an existing open(O_WRITE) file */
3667         if (sattr->ia_valid & ATTR_FILE) {
3668
3669                 ctx = nfs_file_open_context(sattr->ia_file);
3670                 if (ctx)
3671                         cred = ctx->cred;
3672         }
3673
3674         label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3675         if (IS_ERR(label))
3676                 return PTR_ERR(label);
3677
3678         status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
3679         if (status == 0) {
3680                 nfs_setattr_update_inode(inode, sattr, fattr);
3681                 nfs_setsecurity(inode, fattr, label);
3682         }
3683         nfs4_label_free(label);
3684         return status;
3685 }
3686
3687 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3688                 const struct qstr *name, struct nfs_fh *fhandle,
3689                 struct nfs_fattr *fattr, struct nfs4_label *label)
3690 {
3691         struct nfs_server *server = NFS_SERVER(dir);
3692         int                    status;
3693         struct nfs4_lookup_arg args = {
3694                 .bitmask = server->attr_bitmask,
3695                 .dir_fh = NFS_FH(dir),
3696                 .name = name,
3697         };
3698         struct nfs4_lookup_res res = {
3699                 .server = server,
3700                 .fattr = fattr,
3701                 .label = label,
3702                 .fh = fhandle,
3703         };
3704         struct rpc_message msg = {
3705                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3706                 .rpc_argp = &args,
3707                 .rpc_resp = &res,
3708         };
3709
3710         args.bitmask = nfs4_bitmask(server, label);
3711
3712         nfs_fattr_init(fattr);
3713
3714         dprintk("NFS call  lookup %s\n", name->name);
3715         status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3716         dprintk("NFS reply lookup: %d\n", status);
3717         return status;
3718 }
3719
3720 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3721 {
3722         fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3723                 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3724         fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3725         fattr->nlink = 2;
3726 }
3727
3728 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3729                                    const struct qstr *name, struct nfs_fh *fhandle,
3730                                    struct nfs_fattr *fattr, struct nfs4_label *label)
3731 {
3732         struct nfs4_exception exception = { };
3733         struct rpc_clnt *client = *clnt;
3734         int err;
3735         do {
3736                 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3737                 trace_nfs4_lookup(dir, name, err);
3738                 switch (err) {
3739                 case -NFS4ERR_BADNAME:
3740                         err = -ENOENT;
3741                         goto out;
3742                 case -NFS4ERR_MOVED:
3743                         err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3744                         if (err == -NFS4ERR_MOVED)
3745                                 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3746                         goto out;
3747                 case -NFS4ERR_WRONGSEC:
3748                         err = -EPERM;
3749                         if (client != *clnt)
3750                                 goto out;
3751                         client = nfs4_negotiate_security(client, dir, name);
3752                         if (IS_ERR(client))
3753                                 return PTR_ERR(client);
3754
3755                         exception.retry = 1;
3756                         break;
3757                 default:
3758                         err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3759                 }
3760         } while (exception.retry);
3761
3762 out:
3763         if (err == 0)
3764                 *clnt = client;
3765         else if (client != *clnt)
3766                 rpc_shutdown_client(client);
3767
3768         return err;
3769 }
3770
3771 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
3772                             struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3773                             struct nfs4_label *label)
3774 {
3775         int status;
3776         struct rpc_clnt *client = NFS_CLIENT(dir);
3777
3778         status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3779         if (client != NFS_CLIENT(dir)) {
3780                 rpc_shutdown_client(client);
3781                 nfs_fixup_secinfo_attributes(fattr);
3782         }
3783         return status;
3784 }
3785
3786 struct rpc_clnt *
3787 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
3788                             struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3789 {
3790         struct rpc_clnt *client = NFS_CLIENT(dir);
3791         int status;
3792
3793         status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3794         if (status < 0)
3795                 return ERR_PTR(status);
3796         return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3797 }
3798
3799 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3800 {
3801         struct nfs_server *server = NFS_SERVER(inode);
3802         struct nfs4_accessargs args = {
3803                 .fh = NFS_FH(inode),
3804                 .bitmask = server->cache_consistency_bitmask,
3805         };
3806         struct nfs4_accessres res = {
3807                 .server = server,
3808         };
3809         struct rpc_message msg = {
3810                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3811                 .rpc_argp = &args,
3812                 .rpc_resp = &res,
3813                 .rpc_cred = entry->cred,
3814         };
3815         int mode = entry->mask;
3816         int status = 0;
3817
3818         /*
3819          * Determine which access bits we want to ask for...
3820          */
3821         if (mode & MAY_READ)
3822                 args.access |= NFS4_ACCESS_READ;
3823         if (S_ISDIR(inode->i_mode)) {
3824                 if (mode & MAY_WRITE)
3825                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3826                 if (mode & MAY_EXEC)
3827                         args.access |= NFS4_ACCESS_LOOKUP;
3828         } else {
3829                 if (mode & MAY_WRITE)
3830                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3831                 if (mode & MAY_EXEC)
3832                         args.access |= NFS4_ACCESS_EXECUTE;
3833         }
3834
3835         res.fattr = nfs_alloc_fattr();
3836         if (res.fattr == NULL)
3837                 return -ENOMEM;
3838
3839         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3840         if (!status) {
3841                 nfs_access_set_mask(entry, res.access);
3842                 nfs_refresh_inode(inode, res.fattr);
3843         }
3844         nfs_free_fattr(res.fattr);
3845         return status;
3846 }
3847
3848 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3849 {
3850         struct nfs4_exception exception = { };
3851         int err;
3852         do {
3853                 err = _nfs4_proc_access(inode, entry);
3854                 trace_nfs4_access(inode, err);
3855                 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3856                                 &exception);
3857         } while (exception.retry);
3858         return err;
3859 }
3860
3861 /*
3862  * TODO: For the time being, we don't try to get any attributes
3863  * along with any of the zero-copy operations READ, READDIR,
3864  * READLINK, WRITE.
3865  *
3866  * In the case of the first three, we want to put the GETATTR
3867  * after the read-type operation -- this is because it is hard
3868  * to predict the length of a GETATTR response in v4, and thus
3869  * align the READ data correctly.  This means that the GETATTR
3870  * may end up partially falling into the page cache, and we should
3871  * shift it into the 'tail' of the xdr_buf before processing.
3872  * To do this efficiently, we need to know the total length
3873  * of data received, which doesn't seem to be available outside
3874  * of the RPC layer.
3875  *
3876  * In the case of WRITE, we also want to put the GETATTR after
3877  * the operation -- in this case because we want to make sure
3878  * we get the post-operation mtime and size.
3879  *
3880  * Both of these changes to the XDR layer would in fact be quite
3881  * minor, but I decided to leave them for a subsequent patch.
3882  */
3883 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3884                 unsigned int pgbase, unsigned int pglen)
3885 {
3886         struct nfs4_readlink args = {
3887                 .fh       = NFS_FH(inode),
3888                 .pgbase   = pgbase,
3889                 .pglen    = pglen,
3890                 .pages    = &page,
3891         };
3892         struct nfs4_readlink_res res;
3893         struct rpc_message msg = {
3894                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3895                 .rpc_argp = &args,
3896                 .rpc_resp = &res,
3897         };
3898
3899         return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3900 }
3901
3902 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3903                 unsigned int pgbase, unsigned int pglen)
3904 {
3905         struct nfs4_exception exception = { };
3906         int err;
3907         do {
3908                 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3909                 trace_nfs4_readlink(inode, err);
3910                 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3911                                 &exception);
3912         } while (exception.retry);
3913         return err;
3914 }
3915
3916 /*
3917  * This is just for mknod.  open(O_CREAT) will always do ->open_context().
3918  */
3919 static int
3920 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3921                  int flags)
3922 {
3923         struct nfs_server *server = NFS_SERVER(dir);
3924         struct nfs4_label l, *ilabel = NULL;
3925         struct nfs_open_context *ctx;
3926         struct nfs4_state *state;
3927         int status = 0;
3928
3929         ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
3930         if (IS_ERR(ctx))
3931                 return PTR_ERR(ctx);
3932
3933         ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3934
3935         if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
3936                 sattr->ia_mode &= ~current_umask();
3937         state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
3938         if (IS_ERR(state)) {
3939                 status = PTR_ERR(state);
3940                 goto out;
3941         }
3942 out:
3943         nfs4_label_release_security(ilabel);
3944         put_nfs_open_context(ctx);
3945         return status;
3946 }
3947
3948 static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
3949 {
3950         struct nfs_server *server = NFS_SERVER(dir);
3951         struct nfs_removeargs args = {
3952                 .fh = NFS_FH(dir),
3953                 .name = *name,
3954         };
3955         struct nfs_removeres res = {
3956                 .server = server,
3957         };
3958         struct rpc_message msg = {
3959                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3960                 .rpc_argp = &args,
3961                 .rpc_resp = &res,
3962         };
3963         unsigned long timestamp = jiffies;
3964         int status;
3965
3966         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3967         if (status == 0)
3968                 update_changeattr(dir, &res.cinfo, timestamp);
3969         return status;
3970 }
3971
3972 static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
3973 {
3974         struct nfs4_exception exception = { };
3975         int err;
3976         do {
3977                 err = _nfs4_proc_remove(dir, name);
3978                 trace_nfs4_remove(dir, name, err);
3979                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3980                                 &exception);
3981         } while (exception.retry);
3982         return err;
3983 }
3984
3985 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3986 {
3987         struct nfs_server *server = NFS_SERVER(dir);
3988         struct nfs_removeargs *args = msg->rpc_argp;
3989         struct nfs_removeres *res = msg->rpc_resp;
3990
3991         res->server = server;
3992         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3993         nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
3994
3995         nfs_fattr_init(res->dir_attr);
3996 }
3997
3998 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3999 {
4000         nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4001                         &data->args.seq_args,
4002                         &data->res.seq_res,
4003                         task);
4004 }
4005
4006 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4007 {
4008         struct nfs_unlinkdata *data = task->tk_calldata;
4009         struct nfs_removeres *res = &data->res;
4010
4011         if (!nfs4_sequence_done(task, &res->seq_res))
4012                 return 0;
4013         if (nfs4_async_handle_error(task, res->server, NULL,
4014                                     &data->timeout) == -EAGAIN)
4015                 return 0;
4016         if (task->tk_status == 0)
4017                 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
4018         return 1;
4019 }
4020
4021 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4022 {
4023         struct nfs_server *server = NFS_SERVER(dir);
4024         struct nfs_renameargs *arg = msg->rpc_argp;
4025         struct nfs_renameres *res = msg->rpc_resp;
4026
4027         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4028         res->server = server;
4029         nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
4030 }
4031
4032 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4033 {
4034         nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4035                         &data->args.seq_args,
4036                         &data->res.seq_res,
4037                         task);
4038 }
4039
4040 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4041                                  struct inode *new_dir)
4042 {
4043         struct nfs_renamedata *data = task->tk_calldata;
4044         struct nfs_renameres *res = &data->res;
4045
4046         if (!nfs4_sequence_done(task, &res->seq_res))
4047                 return 0;
4048         if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4049                 return 0;
4050
4051         if (task->tk_status == 0) {
4052                 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
4053                 if (new_dir != old_dir)
4054                         update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
4055         }
4056         return 1;
4057 }
4058
4059 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4060 {
4061         struct nfs_server *server = NFS_SERVER(inode);
4062         struct nfs4_link_arg arg = {
4063                 .fh     = NFS_FH(inode),
4064                 .dir_fh = NFS_FH(dir),
4065                 .name   = name,
4066                 .bitmask = server->attr_bitmask,
4067         };
4068         struct nfs4_link_res res = {
4069                 .server = server,
4070                 .label = NULL,
4071         };
4072         struct rpc_message msg = {
4073                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4074                 .rpc_argp = &arg,
4075                 .rpc_resp = &res,
4076         };
4077         int status = -ENOMEM;
4078
4079         res.fattr = nfs_alloc_fattr();
4080         if (res.fattr == NULL)
4081                 goto out;
4082
4083         res.label = nfs4_label_alloc(server, GFP_KERNEL);
4084         if (IS_ERR(res.label)) {
4085                 status = PTR_ERR(res.label);
4086                 goto out;
4087         }
4088         arg.bitmask = nfs4_bitmask(server, res.label);
4089
4090         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4091         if (!status) {
4092                 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
4093                 status = nfs_post_op_update_inode(inode, res.fattr);
4094                 if (!status)
4095                         nfs_setsecurity(inode, res.fattr, res.label);
4096         }
4097
4098
4099         nfs4_label_free(res.label);
4100
4101 out:
4102         nfs_free_fattr(res.fattr);
4103         return status;
4104 }
4105
4106 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4107 {
4108         struct nfs4_exception exception = { };
4109         int err;
4110         do {
4111                 err = nfs4_handle_exception(NFS_SERVER(inode),
4112                                 _nfs4_proc_link(inode, dir, name),
4113                                 &exception);
4114         } while (exception.retry);
4115         return err;
4116 }
4117
4118 struct nfs4_createdata {
4119         struct rpc_message msg;
4120         struct nfs4_create_arg arg;
4121         struct nfs4_create_res res;
4122         struct nfs_fh fh;
4123         struct nfs_fattr fattr;
4124         struct nfs4_label *label;
4125 };
4126
4127 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4128                 const struct qstr *name, struct iattr *sattr, u32 ftype)
4129 {
4130         struct nfs4_createdata *data;
4131
4132         data = kzalloc(sizeof(*data), GFP_KERNEL);
4133         if (data != NULL) {
4134                 struct nfs_server *server = NFS_SERVER(dir);
4135
4136                 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4137                 if (IS_ERR(data->label))
4138                         goto out_free;
4139
4140                 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4141                 data->msg.rpc_argp = &data->arg;
4142                 data->msg.rpc_resp = &data->res;
4143                 data->arg.dir_fh = NFS_FH(dir);
4144                 data->arg.server = server;
4145                 data->arg.name = name;
4146                 data->arg.attrs = sattr;
4147                 data->arg.ftype = ftype;
4148                 data->arg.bitmask = nfs4_bitmask(server, data->label);
4149                 data->arg.umask = current_umask();
4150                 data->res.server = server;
4151                 data->res.fh = &data->fh;
4152                 data->res.fattr = &data->fattr;
4153                 data->res.label = data->label;
4154                 nfs_fattr_init(data->res.fattr);
4155         }
4156         return data;
4157 out_free:
4158         kfree(data);
4159         return NULL;
4160 }
4161
4162 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4163 {
4164         int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4165                                     &data->arg.seq_args, &data->res.seq_res, 1);
4166         if (status == 0) {
4167                 update_changeattr(dir, &data->res.dir_cinfo,
4168                                 data->res.fattr->time_start);
4169                 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4170         }
4171         return status;
4172 }
4173
4174 static void nfs4_free_createdata(struct nfs4_createdata *data)
4175 {
4176         nfs4_label_free(data->label);
4177         kfree(data);
4178 }
4179
4180 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4181                 struct page *page, unsigned int len, struct iattr *sattr,
4182                 struct nfs4_label *label)
4183 {
4184         struct nfs4_createdata *data;
4185         int status = -ENAMETOOLONG;
4186
4187         if (len > NFS4_MAXPATHLEN)
4188                 goto out;
4189
4190         status = -ENOMEM;
4191         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4192         if (data == NULL)
4193                 goto out;
4194
4195         data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4196         data->arg.u.symlink.pages = &page;
4197         data->arg.u.symlink.len = len;
4198         data->arg.label = label;
4199         
4200         status = nfs4_do_create(dir, dentry, data);
4201
4202         nfs4_free_createdata(data);
4203 out:
4204         return status;
4205 }
4206
4207 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4208                 struct page *page, unsigned int len, struct iattr *sattr)
4209 {
4210         struct nfs4_exception exception = { };
4211         struct nfs4_label l, *label = NULL;
4212         int err;
4213
4214         label = nfs4_label_init_security(dir, dentry, sattr, &l);
4215
4216         do {
4217                 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4218                 trace_nfs4_symlink(dir, &dentry->d_name, err);
4219                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4220                                 &exception);
4221         } while (exception.retry);
4222
4223         nfs4_label_release_security(label);
4224         return err;
4225 }
4226
4227 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4228                 struct iattr *sattr, struct nfs4_label *label)
4229 {
4230         struct nfs4_createdata *data;
4231         int status = -ENOMEM;
4232
4233         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4234         if (data == NULL)
4235                 goto out;
4236
4237         data->arg.label = label;
4238         status = nfs4_do_create(dir, dentry, data);
4239
4240         nfs4_free_createdata(data);
4241 out:
4242         return status;
4243 }
4244
4245 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4246                 struct iattr *sattr)
4247 {
4248         struct nfs_server *server = NFS_SERVER(dir);
4249         struct nfs4_exception exception = { };
4250         struct nfs4_label l, *label = NULL;
4251         int err;
4252
4253         label = nfs4_label_init_security(dir, dentry, sattr, &l);
4254
4255         if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4256                 sattr->ia_mode &= ~current_umask();
4257         do {
4258                 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4259                 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4260                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4261                                 &exception);
4262         } while (exception.retry);
4263         nfs4_label_release_security(label);
4264
4265         return err;
4266 }
4267
4268 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4269                 u64 cookie, struct page **pages, unsigned int count, int plus)
4270 {
4271         struct inode            *dir = d_inode(dentry);
4272         struct nfs4_readdir_arg args = {
4273                 .fh = NFS_FH(dir),
4274                 .pages = pages,
4275                 .pgbase = 0,
4276                 .count = count,
4277                 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4278                 .plus = plus,
4279         };
4280         struct nfs4_readdir_res res;
4281         struct rpc_message msg = {
4282                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4283                 .rpc_argp = &args,
4284                 .rpc_resp = &res,
4285                 .rpc_cred = cred,
4286         };
4287         int                     status;
4288
4289         dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4290                         dentry,
4291                         (unsigned long long)cookie);
4292         nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4293         res.pgbase = args.pgbase;
4294         status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4295         if (status >= 0) {
4296                 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4297                 status += args.pgbase;
4298         }
4299
4300         nfs_invalidate_atime(dir);
4301
4302         dprintk("%s: returns %d\n", __func__, status);
4303         return status;
4304 }
4305
4306 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4307                 u64 cookie, struct page **pages, unsigned int count, int plus)
4308 {
4309         struct nfs4_exception exception = { };
4310         int err;
4311         do {
4312                 err = _nfs4_proc_readdir(dentry, cred, cookie,
4313                                 pages, count, plus);
4314                 trace_nfs4_readdir(d_inode(dentry), err);
4315                 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4316                                 &exception);
4317         } while (exception.retry);
4318         return err;
4319 }
4320
4321 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4322                 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4323 {
4324         struct nfs4_createdata *data;
4325         int mode = sattr->ia_mode;
4326         int status = -ENOMEM;
4327
4328         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4329         if (data == NULL)
4330                 goto out;
4331
4332         if (S_ISFIFO(mode))
4333                 data->arg.ftype = NF4FIFO;
4334         else if (S_ISBLK(mode)) {
4335                 data->arg.ftype = NF4BLK;
4336                 data->arg.u.device.specdata1 = MAJOR(rdev);
4337                 data->arg.u.device.specdata2 = MINOR(rdev);
4338         }
4339         else if (S_ISCHR(mode)) {
4340                 data->arg.ftype = NF4CHR;
4341                 data->arg.u.device.specdata1 = MAJOR(rdev);
4342                 data->arg.u.device.specdata2 = MINOR(rdev);
4343         } else if (!S_ISSOCK(mode)) {
4344                 status = -EINVAL;
4345                 goto out_free;
4346         }
4347
4348         data->arg.label = label;
4349         status = nfs4_do_create(dir, dentry, data);
4350 out_free:
4351         nfs4_free_createdata(data);
4352 out:
4353         return status;
4354 }
4355
4356 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4357                 struct iattr *sattr, dev_t rdev)
4358 {
4359         struct nfs_server *server = NFS_SERVER(dir);
4360         struct nfs4_exception exception = { };
4361         struct nfs4_label l, *label = NULL;
4362         int err;
4363
4364         label = nfs4_label_init_security(dir, dentry, sattr, &l);
4365
4366         if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4367                 sattr->ia_mode &= ~current_umask();
4368         do {
4369                 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4370                 trace_nfs4_mknod(dir, &dentry->d_name, err);
4371                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4372                                 &exception);
4373         } while (exception.retry);
4374
4375         nfs4_label_release_security(label);
4376
4377         return err;
4378 }
4379
4380 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4381                  struct nfs_fsstat *fsstat)
4382 {
4383         struct nfs4_statfs_arg args = {
4384                 .fh = fhandle,
4385                 .bitmask = server->attr_bitmask,
4386         };
4387         struct nfs4_statfs_res res = {
4388                 .fsstat = fsstat,
4389         };
4390         struct rpc_message msg = {
4391                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4392                 .rpc_argp = &args,
4393                 .rpc_resp = &res,
4394         };
4395
4396         nfs_fattr_init(fsstat->fattr);
4397         return  nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4398 }
4399
4400 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4401 {
4402         struct nfs4_exception exception = { };
4403         int err;
4404         do {
4405                 err = nfs4_handle_exception(server,
4406                                 _nfs4_proc_statfs(server, fhandle, fsstat),
4407                                 &exception);
4408         } while (exception.retry);
4409         return err;
4410 }
4411
4412 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4413                 struct nfs_fsinfo *fsinfo)
4414 {
4415         struct nfs4_fsinfo_arg args = {
4416                 .fh = fhandle,
4417                 .bitmask = server->attr_bitmask,
4418         };
4419         struct nfs4_fsinfo_res res = {
4420                 .fsinfo = fsinfo,
4421         };
4422         struct rpc_message msg = {
4423                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4424                 .rpc_argp = &args,
4425                 .rpc_resp = &res,
4426         };
4427
4428         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4429 }
4430
4431 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4432 {
4433         struct nfs4_exception exception = { };
4434         unsigned long now = jiffies;
4435         int err;
4436
4437         do {
4438                 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4439                 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4440                 if (err == 0) {
4441                         nfs4_set_lease_period(server->nfs_client,
4442                                         fsinfo->lease_time * HZ,
4443                                         now);
4444                         break;
4445                 }
4446                 err = nfs4_handle_exception(server, err, &exception);
4447         } while (exception.retry);
4448         return err;
4449 }
4450
4451 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4452 {
4453         int error;
4454
4455         nfs_fattr_init(fsinfo->fattr);
4456         error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4457         if (error == 0) {
4458                 /* block layout checks this! */
4459                 server->pnfs_blksize = fsinfo->blksize;
4460                 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4461         }
4462
4463         return error;
4464 }
4465
4466 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4467                 struct nfs_pathconf *pathconf)
4468 {
4469         struct nfs4_pathconf_arg args = {
4470                 .fh = fhandle,
4471                 .bitmask = server->attr_bitmask,
4472         };
4473         struct nfs4_pathconf_res res = {
4474                 .pathconf = pathconf,
4475         };
4476         struct rpc_message msg = {
4477                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4478                 .rpc_argp = &args,
4479                 .rpc_resp = &res,
4480         };
4481
4482         /* None of the pathconf attributes are mandatory to implement */
4483         if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4484                 memset(pathconf, 0, sizeof(*pathconf));
4485                 return 0;
4486         }
4487
4488         nfs_fattr_init(pathconf->fattr);
4489         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4490 }
4491
4492 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4493                 struct nfs_pathconf *pathconf)
4494 {
4495         struct nfs4_exception exception = { };
4496         int err;
4497
4498         do {
4499                 err = nfs4_handle_exception(server,
4500                                 _nfs4_proc_pathconf(server, fhandle, pathconf),
4501                                 &exception);
4502         } while (exception.retry);
4503         return err;
4504 }
4505
4506 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4507                 const struct nfs_open_context *ctx,
4508                 const struct nfs_lock_context *l_ctx,
4509                 fmode_t fmode)
4510 {
4511         return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
4512 }
4513 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4514
4515 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4516                 const struct nfs_open_context *ctx,
4517                 const struct nfs_lock_context *l_ctx,
4518                 fmode_t fmode)
4519 {
4520         nfs4_stateid current_stateid;
4521
4522         /* If the current stateid represents a lost lock, then exit */
4523         if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4524                 return true;
4525         return nfs4_stateid_match(stateid, &current_stateid);
4526 }
4527
4528 static bool nfs4_error_stateid_expired(int err)
4529 {
4530         switch (err) {
4531         case -NFS4ERR_DELEG_REVOKED:
4532         case -NFS4ERR_ADMIN_REVOKED:
4533         case -NFS4ERR_BAD_STATEID:
4534         case -NFS4ERR_STALE_STATEID:
4535         case -NFS4ERR_OLD_STATEID:
4536         case -NFS4ERR_OPENMODE:
4537         case -NFS4ERR_EXPIRED:
4538                 return true;
4539         }
4540         return false;
4541 }
4542
4543 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4544 {
4545         struct nfs_server *server = NFS_SERVER(hdr->inode);
4546
4547         trace_nfs4_read(hdr, task->tk_status);
4548         if (task->tk_status < 0) {
4549                 struct nfs4_exception exception = {
4550                         .inode = hdr->inode,
4551                         .state = hdr->args.context->state,
4552                         .stateid = &hdr->args.stateid,
4553                 };
4554                 task->tk_status = nfs4_async_handle_exception(task,
4555                                 server, task->tk_status, &exception);
4556                 if (exception.retry) {
4557                         rpc_restart_call_prepare(task);
4558                         return -EAGAIN;
4559                 }
4560         }
4561
4562         if (task->tk_status > 0)
4563                 renew_lease(server, hdr->timestamp);
4564         return 0;
4565 }
4566
4567 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4568                 struct nfs_pgio_args *args)
4569 {
4570
4571         if (!nfs4_error_stateid_expired(task->tk_status) ||
4572                 nfs4_stateid_is_current(&args->stateid,
4573                                 args->context,
4574                                 args->lock_context,
4575                                 FMODE_READ))
4576                 return false;
4577         rpc_restart_call_prepare(task);
4578         return true;
4579 }
4580
4581 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4582 {
4583
4584         dprintk("--> %s\n", __func__);
4585
4586         if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4587                 return -EAGAIN;
4588         if (nfs4_read_stateid_changed(task, &hdr->args))
4589                 return -EAGAIN;
4590         if (task->tk_status > 0)
4591                 nfs_invalidate_atime(hdr->inode);
4592         return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4593                                     nfs4_read_done_cb(task, hdr);
4594 }
4595
4596 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4597                                  struct rpc_message *msg)
4598 {
4599         hdr->timestamp   = jiffies;
4600         if (!hdr->pgio_done_cb)
4601                 hdr->pgio_done_cb = nfs4_read_done_cb;
4602         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4603         nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4604 }
4605
4606 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4607                                       struct nfs_pgio_header *hdr)
4608 {
4609         if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
4610                         &hdr->args.seq_args,
4611                         &hdr->res.seq_res,
4612                         task))
4613                 return 0;
4614         if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4615                                 hdr->args.lock_context,
4616                                 hdr->rw_ops->rw_mode) == -EIO)
4617                 return -EIO;
4618         if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4619                 return -EIO;
4620         return 0;
4621 }
4622
4623 static int nfs4_write_done_cb(struct rpc_task *task,
4624                               struct nfs_pgio_header *hdr)
4625 {
4626         struct inode *inode = hdr->inode;
4627
4628         trace_nfs4_write(hdr, task->tk_status);
4629         if (task->tk_status < 0) {
4630                 struct nfs4_exception exception = {
4631                         .inode = hdr->inode,
4632                         .state = hdr->args.context->state,
4633                         .stateid = &hdr->args.stateid,
4634                 };
4635                 task->tk_status = nfs4_async_handle_exception(task,
4636                                 NFS_SERVER(inode), task->tk_status,
4637                                 &exception);
4638                 if (exception.retry) {
4639                         rpc_restart_call_prepare(task);
4640                         return -EAGAIN;
4641                 }
4642         }
4643         if (task->tk_status >= 0) {
4644                 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4645                 nfs_writeback_update_inode(hdr);
4646         }
4647         return 0;
4648 }
4649
4650 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4651                 struct nfs_pgio_args *args)
4652 {
4653
4654         if (!nfs4_error_stateid_expired(task->tk_status) ||
4655                 nfs4_stateid_is_current(&args->stateid,
4656                                 args->context,
4657                                 args->lock_context,
4658                                 FMODE_WRITE))
4659                 return false;
4660         rpc_restart_call_prepare(task);
4661         return true;
4662 }
4663
4664 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4665 {
4666         if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4667                 return -EAGAIN;
4668         if (nfs4_write_stateid_changed(task, &hdr->args))
4669                 return -EAGAIN;
4670         return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4671                 nfs4_write_done_cb(task, hdr);
4672 }
4673
4674 static
4675 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4676 {
4677         /* Don't request attributes for pNFS or O_DIRECT writes */
4678         if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4679                 return false;
4680         /* Otherwise, request attributes if and only if we don't hold
4681          * a delegation
4682          */
4683         return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4684 }
4685
4686 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4687                                   struct rpc_message *msg)
4688 {
4689         struct nfs_server *server = NFS_SERVER(hdr->inode);
4690
4691         if (!nfs4_write_need_cache_consistency_data(hdr)) {
4692                 hdr->args.bitmask = NULL;
4693                 hdr->res.fattr = NULL;
4694         } else
4695                 hdr->args.bitmask = server->cache_consistency_bitmask;
4696
4697         if (!hdr->pgio_done_cb)
4698                 hdr->pgio_done_cb = nfs4_write_done_cb;
4699         hdr->res.server = server;
4700         hdr->timestamp   = jiffies;
4701
4702         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4703         nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4704 }
4705
4706 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4707 {
4708         nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
4709                         &data->args.seq_args,
4710                         &data->res.seq_res,
4711                         task);
4712 }
4713
4714 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4715 {
4716         struct inode *inode = data->inode;
4717
4718         trace_nfs4_commit(data, task->tk_status);
4719         if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4720                                     NULL, NULL) == -EAGAIN) {
4721                 rpc_restart_call_prepare(task);
4722                 return -EAGAIN;
4723         }
4724         return 0;
4725 }
4726
4727 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4728 {
4729         if (!nfs4_sequence_done(task, &data->res.seq_res))
4730                 return -EAGAIN;
4731         return data->commit_done_cb(task, data);
4732 }
4733
4734 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4735 {
4736         struct nfs_server *server = NFS_SERVER(data->inode);
4737
4738         if (data->commit_done_cb == NULL)
4739                 data->commit_done_cb = nfs4_commit_done_cb;
4740         data->res.server = server;
4741         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4742         nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4743 }
4744
4745 struct nfs4_renewdata {
4746         struct nfs_client       *client;
4747         unsigned long           timestamp;
4748 };
4749
4750 /*
4751  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4752  * standalone procedure for queueing an asynchronous RENEW.
4753  */
4754 static void nfs4_renew_release(void *calldata)
4755 {
4756         struct nfs4_renewdata *data = calldata;
4757         struct nfs_client *clp = data->client;
4758
4759         if (atomic_read(&clp->cl_count) > 1)
4760                 nfs4_schedule_state_renewal(clp);
4761         nfs_put_client(clp);
4762         kfree(data);
4763 }
4764
4765 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4766 {
4767         struct nfs4_renewdata *data = calldata;
4768         struct nfs_client *clp = data->client;
4769         unsigned long timestamp = data->timestamp;
4770
4771         trace_nfs4_renew_async(clp, task->tk_status);
4772         switch (task->tk_status) {
4773         case 0:
4774                 break;
4775         case -NFS4ERR_LEASE_MOVED:
4776                 nfs4_schedule_lease_moved_recovery(clp);
4777                 break;
4778         default:
4779                 /* Unless we're shutting down, schedule state recovery! */
4780                 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4781                         return;
4782                 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4783                         nfs4_schedule_lease_recovery(clp);
4784                         return;
4785                 }
4786                 nfs4_schedule_path_down_recovery(clp);
4787         }
4788         do_renew_lease(clp, timestamp);
4789 }
4790
4791 static const struct rpc_call_ops nfs4_renew_ops = {
4792         .rpc_call_done = nfs4_renew_done,
4793         .rpc_release = nfs4_renew_release,
4794 };
4795
4796 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4797 {
4798         struct rpc_message msg = {
4799                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4800                 .rpc_argp       = clp,
4801                 .rpc_cred       = cred,
4802         };
4803         struct nfs4_renewdata *data;
4804
4805         if (renew_flags == 0)
4806                 return 0;
4807         if (!atomic_inc_not_zero(&clp->cl_count))
4808                 return -EIO;
4809         data = kmalloc(sizeof(*data), GFP_NOFS);
4810         if (data == NULL)
4811                 return -ENOMEM;
4812         data->client = clp;
4813         data->timestamp = jiffies;
4814         return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4815                         &nfs4_renew_ops, data);
4816 }
4817
4818 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4819 {
4820         struct rpc_message msg = {
4821                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4822                 .rpc_argp       = clp,
4823                 .rpc_cred       = cred,
4824         };
4825         unsigned long now = jiffies;
4826         int status;
4827
4828         status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4829         if (status < 0)
4830                 return status;
4831         do_renew_lease(clp, now);
4832         return 0;
4833 }
4834
4835 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4836 {
4837         return server->caps & NFS_CAP_ACLS;
4838 }
4839
4840 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4841  * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4842  * the stack.
4843  */
4844 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4845
4846 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4847                 struct page **pages)
4848 {
4849         struct page *newpage, **spages;
4850         int rc = 0;
4851         size_t len;
4852         spages = pages;
4853
4854         do {
4855                 len = min_t(size_t, PAGE_SIZE, buflen);
4856                 newpage = alloc_page(GFP_KERNEL);
4857
4858                 if (newpage == NULL)
4859                         goto unwind;
4860                 memcpy(page_address(newpage), buf, len);
4861                 buf += len;
4862                 buflen -= len;
4863                 *pages++ = newpage;
4864                 rc++;
4865         } while (buflen != 0);
4866
4867         return rc;
4868
4869 unwind:
4870         for(; rc > 0; rc--)
4871                 __free_page(spages[rc-1]);
4872         return -ENOMEM;
4873 }
4874
4875 struct nfs4_cached_acl {
4876         int cached;
4877         size_t len;
4878         char data[0];
4879 };
4880
4881 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4882 {
4883         struct nfs_inode *nfsi = NFS_I(inode);
4884
4885         spin_lock(&inode->i_lock);
4886         kfree(nfsi->nfs4_acl);
4887         nfsi->nfs4_acl = acl;
4888         spin_unlock(&inode->i_lock);
4889 }
4890
4891 static void nfs4_zap_acl_attr(struct inode *inode)
4892 {
4893         nfs4_set_cached_acl(inode, NULL);
4894 }
4895
4896 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4897 {
4898         struct nfs_inode *nfsi = NFS_I(inode);
4899         struct nfs4_cached_acl *acl;
4900         int ret = -ENOENT;
4901
4902         spin_lock(&inode->i_lock);
4903         acl = nfsi->nfs4_acl;
4904         if (acl == NULL)
4905                 goto out;
4906         if (buf == NULL) /* user is just asking for length */
4907                 goto out_len;
4908         if (acl->cached == 0)
4909                 goto out;
4910         ret = -ERANGE; /* see getxattr(2) man page */
4911         if (acl->len > buflen)
4912                 goto out;
4913         memcpy(buf, acl->data, acl->len);
4914 out_len:
4915         ret = acl->len;
4916 out:
4917         spin_unlock(&inode->i_lock);
4918         return ret;
4919 }
4920
4921 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4922 {
4923         struct nfs4_cached_acl *acl;
4924         size_t buflen = sizeof(*acl) + acl_len;
4925
4926         if (buflen <= PAGE_SIZE) {
4927                 acl = kmalloc(buflen, GFP_KERNEL);
4928                 if (acl == NULL)
4929                         goto out;
4930                 acl->cached = 1;
4931                 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4932         } else {
4933                 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4934                 if (acl == NULL)
4935                         goto out;
4936                 acl->cached = 0;
4937         }
4938         acl->len = acl_len;
4939 out:
4940         nfs4_set_cached_acl(inode, acl);
4941 }
4942
4943 /*
4944  * The getxattr API returns the required buffer length when called with a
4945  * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4946  * the required buf.  On a NULL buf, we send a page of data to the server
4947  * guessing that the ACL request can be serviced by a page. If so, we cache
4948  * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4949  * the cache. If not so, we throw away the page, and cache the required
4950  * length. The next getxattr call will then produce another round trip to
4951  * the server, this time with the input buf of the required size.
4952  */
4953 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4954 {
4955         struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
4956         struct nfs_getaclargs args = {
4957                 .fh = NFS_FH(inode),
4958                 .acl_pages = pages,
4959                 .acl_len = buflen,
4960         };
4961         struct nfs_getaclres res = {
4962                 .acl_len = buflen,
4963         };
4964         struct rpc_message msg = {
4965                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4966                 .rpc_argp = &args,
4967                 .rpc_resp = &res,
4968         };
4969         unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
4970         int ret = -ENOMEM, i;
4971
4972         if (npages > ARRAY_SIZE(pages))
4973                 return -ERANGE;
4974
4975         for (i = 0; i < npages; i++) {
4976                 pages[i] = alloc_page(GFP_KERNEL);
4977                 if (!pages[i])
4978                         goto out_free;
4979         }
4980
4981         /* for decoding across pages */
4982         res.acl_scratch = alloc_page(GFP_KERNEL);
4983         if (!res.acl_scratch)
4984                 goto out_free;
4985
4986         args.acl_len = npages * PAGE_SIZE;
4987
4988         dprintk("%s  buf %p buflen %zu npages %d args.acl_len %zu\n",
4989                 __func__, buf, buflen, npages, args.acl_len);
4990         ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4991                              &msg, &args.seq_args, &res.seq_res, 0);
4992         if (ret)
4993                 goto out_free;
4994
4995         /* Handle the case where the passed-in buffer is too short */
4996         if (res.acl_flags & NFS4_ACL_TRUNC) {
4997                 /* Did the user only issue a request for the acl length? */
4998                 if (buf == NULL)
4999                         goto out_ok;
5000                 ret = -ERANGE;
5001                 goto out_free;
5002         }
5003         nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5004         if (buf) {
5005                 if (res.acl_len > buflen) {
5006                         ret = -ERANGE;
5007                         goto out_free;
5008                 }
5009                 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5010         }
5011 out_ok:
5012         ret = res.acl_len;
5013 out_free:
5014         for (i = 0; i < npages; i++)
5015                 if (pages[i])
5016                         __free_page(pages[i]);
5017         if (res.acl_scratch)
5018                 __free_page(res.acl_scratch);
5019         return ret;
5020 }
5021
5022 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5023 {
5024         struct nfs4_exception exception = { };
5025         ssize_t ret;
5026         do {
5027                 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5028                 trace_nfs4_get_acl(inode, ret);
5029                 if (ret >= 0)
5030                         break;
5031                 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5032         } while (exception.retry);
5033         return ret;
5034 }
5035
5036 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5037 {
5038         struct nfs_server *server = NFS_SERVER(inode);
5039         int ret;
5040
5041         if (!nfs4_server_supports_acls(server))
5042                 return -EOPNOTSUPP;
5043         ret = nfs_revalidate_inode(server, inode);
5044         if (ret < 0)
5045                 return ret;
5046         if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5047                 nfs_zap_acl_cache(inode);
5048         ret = nfs4_read_cached_acl(inode, buf, buflen);
5049         if (ret != -ENOENT)
5050                 /* -ENOENT is returned if there is no ACL or if there is an ACL
5051                  * but no cached acl data, just the acl length */
5052                 return ret;
5053         return nfs4_get_acl_uncached(inode, buf, buflen);
5054 }
5055
5056 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5057 {
5058         struct nfs_server *server = NFS_SERVER(inode);
5059         struct page *pages[NFS4ACL_MAXPAGES];
5060         struct nfs_setaclargs arg = {
5061                 .fh             = NFS_FH(inode),
5062                 .acl_pages      = pages,
5063                 .acl_len        = buflen,
5064         };
5065         struct nfs_setaclres res;
5066         struct rpc_message msg = {
5067                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5068                 .rpc_argp       = &arg,
5069                 .rpc_resp       = &res,
5070         };
5071         unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5072         int ret, i;
5073
5074         if (!nfs4_server_supports_acls(server))
5075                 return -EOPNOTSUPP;
5076         if (npages > ARRAY_SIZE(pages))
5077                 return -ERANGE;
5078         i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5079         if (i < 0)
5080                 return i;
5081         nfs4_inode_return_delegation(inode);
5082         ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5083
5084         /*
5085          * Free each page after tx, so the only ref left is
5086          * held by the network stack
5087          */
5088         for (; i > 0; i--)
5089                 put_page(pages[i-1]);
5090
5091         /*
5092          * Acl update can result in inode attribute update.
5093          * so mark the attribute cache invalid.
5094          */
5095         spin_lock(&inode->i_lock);
5096         NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5097         spin_unlock(&inode->i_lock);
5098         nfs_access_zap_cache(inode);
5099         nfs_zap_acl_cache(inode);
5100         return ret;
5101 }
5102
5103 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5104 {
5105         struct nfs4_exception exception = { };
5106         int err;
5107         do {
5108                 err = __nfs4_proc_set_acl(inode, buf, buflen);
5109                 trace_nfs4_set_acl(inode, err);
5110                 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5111                                 &exception);
5112         } while (exception.retry);
5113         return err;
5114 }
5115
5116 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5117 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5118                                         size_t buflen)
5119 {
5120         struct nfs_server *server = NFS_SERVER(inode);
5121         struct nfs_fattr fattr;
5122         struct nfs4_label label = {0, 0, buflen, buf};
5123
5124         u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5125         struct nfs4_getattr_arg arg = {
5126                 .fh             = NFS_FH(inode),
5127                 .bitmask        = bitmask,
5128         };
5129         struct nfs4_getattr_res res = {
5130                 .fattr          = &fattr,
5131                 .label          = &label,
5132                 .server         = server,
5133         };
5134         struct rpc_message msg = {
5135                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5136                 .rpc_argp       = &arg,
5137                 .rpc_resp       = &res,
5138         };
5139         int ret;
5140
5141         nfs_fattr_init(&fattr);
5142
5143         ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5144         if (ret)
5145                 return ret;
5146         if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5147                 return -ENOENT;
5148         if (buflen < label.len)
5149                 return -ERANGE;
5150         return 0;
5151 }
5152
5153 static int nfs4_get_security_label(struct inode *inode, void *buf,
5154                                         size_t buflen)
5155 {
5156         struct nfs4_exception exception = { };
5157         int err;
5158
5159         if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5160                 return -EOPNOTSUPP;
5161
5162         do {
5163                 err = _nfs4_get_security_label(inode, buf, buflen);
5164                 trace_nfs4_get_security_label(inode, err);
5165                 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5166                                 &exception);
5167         } while (exception.retry);
5168         return err;
5169 }
5170
5171 static int _nfs4_do_set_security_label(struct inode *inode,
5172                 struct nfs4_label *ilabel,
5173                 struct nfs_fattr *fattr,
5174                 struct nfs4_label *olabel)
5175 {
5176
5177         struct iattr sattr = {0};
5178         struct nfs_server *server = NFS_SERVER(inode);
5179         const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5180         struct nfs_setattrargs arg = {
5181                 .fh             = NFS_FH(inode),
5182                 .iap            = &sattr,
5183                 .server         = server,
5184                 .bitmask        = bitmask,
5185                 .label          = ilabel,
5186         };
5187         struct nfs_setattrres res = {
5188                 .fattr          = fattr,
5189                 .label          = olabel,
5190                 .server         = server,
5191         };
5192         struct rpc_message msg = {
5193                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5194                 .rpc_argp       = &arg,
5195                 .rpc_resp       = &res,
5196         };
5197         int status;
5198
5199         nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5200
5201         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5202         if (status)
5203                 dprintk("%s failed: %d\n", __func__, status);
5204
5205         return status;
5206 }
5207
5208 static int nfs4_do_set_security_label(struct inode *inode,
5209                 struct nfs4_label *ilabel,
5210                 struct nfs_fattr *fattr,
5211                 struct nfs4_label *olabel)
5212 {
5213         struct nfs4_exception exception = { };
5214         int err;
5215
5216         do {
5217                 err = _nfs4_do_set_security_label(inode, ilabel,
5218                                 fattr, olabel);
5219                 trace_nfs4_set_security_label(inode, err);
5220                 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5221                                 &exception);
5222         } while (exception.retry);
5223         return err;
5224 }
5225
5226 static int
5227 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5228 {
5229         struct nfs4_label ilabel, *olabel = NULL;
5230         struct nfs_fattr fattr;
5231         struct rpc_cred *cred;
5232         int status;
5233
5234         if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5235                 return -EOPNOTSUPP;
5236
5237         nfs_fattr_init(&fattr);
5238
5239         ilabel.pi = 0;
5240         ilabel.lfs = 0;
5241         ilabel.label = (char *)buf;
5242         ilabel.len = buflen;
5243
5244         cred = rpc_lookup_cred();
5245         if (IS_ERR(cred))
5246                 return PTR_ERR(cred);
5247
5248         olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5249         if (IS_ERR(olabel)) {
5250                 status = -PTR_ERR(olabel);
5251                 goto out;
5252         }
5253
5254         status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5255         if (status == 0)
5256                 nfs_setsecurity(inode, &fattr, olabel);
5257
5258         nfs4_label_free(olabel);
5259 out:
5260         put_rpccred(cred);
5261         return status;
5262 }
5263 #endif  /* CONFIG_NFS_V4_SECURITY_LABEL */
5264
5265
5266 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5267                                     nfs4_verifier *bootverf)
5268 {
5269         __be32 verf[2];
5270
5271         if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5272                 /* An impossible timestamp guarantees this value
5273                  * will never match a generated boot time. */
5274                 verf[0] = cpu_to_be32(U32_MAX);
5275                 verf[1] = cpu_to_be32(U32_MAX);
5276         } else {
5277                 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5278                 u64 ns = ktime_to_ns(nn->boot_time);
5279
5280                 verf[0] = cpu_to_be32(ns >> 32);
5281                 verf[1] = cpu_to_be32(ns);
5282         }
5283         memcpy(bootverf->data, verf, sizeof(bootverf->data));
5284 }
5285
5286 static int
5287 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5288 {
5289         size_t len;
5290         char *str;
5291
5292         if (clp->cl_owner_id != NULL)
5293                 return 0;
5294
5295         rcu_read_lock();
5296         len = 14 + strlen(clp->cl_ipaddr) + 1 +
5297                 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5298                 1 +
5299                 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5300                 1;
5301         rcu_read_unlock();
5302
5303         if (len > NFS4_OPAQUE_LIMIT + 1)
5304                 return -EINVAL;
5305
5306         /*
5307          * Since this string is allocated at mount time, and held until the
5308          * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5309          * about a memory-reclaim deadlock.
5310          */
5311         str = kmalloc(len, GFP_KERNEL);
5312         if (!str)
5313                 return -ENOMEM;
5314
5315         rcu_read_lock();
5316         scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5317                         clp->cl_ipaddr,
5318                         rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5319                         rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
5320         rcu_read_unlock();
5321
5322         clp->cl_owner_id = str;
5323         return 0;
5324 }
5325
5326 static int
5327 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5328 {
5329         size_t len;
5330         char *str;
5331
5332         len = 10 + 10 + 1 + 10 + 1 +
5333                 strlen(nfs4_client_id_uniquifier) + 1 +
5334                 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5335
5336         if (len > NFS4_OPAQUE_LIMIT + 1)
5337                 return -EINVAL;
5338
5339         /*
5340          * Since this string is allocated at mount time, and held until the
5341          * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5342          * about a memory-reclaim deadlock.
5343          */
5344         str = kmalloc(len, GFP_KERNEL);
5345         if (!str)
5346                 return -ENOMEM;
5347
5348         scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5349                         clp->rpc_ops->version, clp->cl_minorversion,
5350                         nfs4_client_id_uniquifier,
5351                         clp->cl_rpcclient->cl_nodename);
5352         clp->cl_owner_id = str;
5353         return 0;
5354 }
5355
5356 static int
5357 nfs4_init_uniform_client_string(struct nfs_client *clp)
5358 {
5359         size_t len;
5360         char *str;
5361
5362         if (clp->cl_owner_id != NULL)
5363                 return 0;
5364
5365         if (nfs4_client_id_uniquifier[0] != '\0')
5366                 return nfs4_init_uniquifier_client_string(clp);
5367
5368         len = 10 + 10 + 1 + 10 + 1 +
5369                 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5370
5371         if (len > NFS4_OPAQUE_LIMIT + 1)
5372                 return -EINVAL;
5373
5374         /*
5375          * Since this string is allocated at mount time, and held until the
5376          * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5377          * about a memory-reclaim deadlock.
5378          */
5379         str = kmalloc(len, GFP_KERNEL);
5380         if (!str)
5381                 return -ENOMEM;
5382
5383         scnprintf(str, len, "Linux NFSv%u.%u %s",
5384                         clp->rpc_ops->version, clp->cl_minorversion,
5385                         clp->cl_rpcclient->cl_nodename);
5386         clp->cl_owner_id = str;
5387         return 0;
5388 }
5389
5390 /*
5391  * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5392  * services.  Advertise one based on the address family of the
5393  * clientaddr.
5394  */
5395 static unsigned int
5396 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5397 {
5398         if (strchr(clp->cl_ipaddr, ':') != NULL)
5399                 return scnprintf(buf, len, "tcp6");
5400         else
5401                 return scnprintf(buf, len, "tcp");
5402 }
5403
5404 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5405 {
5406         struct nfs4_setclientid *sc = calldata;
5407
5408         if (task->tk_status == 0)
5409                 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5410 }
5411
5412 static const struct rpc_call_ops nfs4_setclientid_ops = {
5413         .rpc_call_done = nfs4_setclientid_done,
5414 };
5415
5416 /**
5417  * nfs4_proc_setclientid - Negotiate client ID
5418  * @clp: state data structure
5419  * @program: RPC program for NFSv4 callback service
5420  * @port: IP port number for NFS4 callback service
5421  * @cred: RPC credential to use for this call
5422  * @res: where to place the result
5423  *
5424  * Returns zero, a negative errno, or a negative NFS4ERR status code.
5425  */
5426 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5427                 unsigned short port, struct rpc_cred *cred,
5428                 struct nfs4_setclientid_res *res)
5429 {
5430         nfs4_verifier sc_verifier;
5431         struct nfs4_setclientid setclientid = {
5432                 .sc_verifier = &sc_verifier,
5433                 .sc_prog = program,
5434                 .sc_clnt = clp,
5435         };
5436         struct rpc_message msg = {
5437                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5438                 .rpc_argp = &setclientid,
5439                 .rpc_resp = res,
5440                 .rpc_cred = cred,
5441         };
5442         struct rpc_task *task;
5443         struct rpc_task_setup task_setup_data = {
5444                 .rpc_client = clp->cl_rpcclient,
5445                 .rpc_message = &msg,
5446                 .callback_ops = &nfs4_setclientid_ops,
5447                 .callback_data = &setclientid,
5448                 .flags = RPC_TASK_TIMEOUT,
5449         };
5450         int status;
5451
5452         /* nfs_client_id4 */
5453         nfs4_init_boot_verifier(clp, &sc_verifier);
5454
5455         if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5456                 status = nfs4_init_uniform_client_string(clp);
5457         else
5458                 status = nfs4_init_nonuniform_client_string(clp);
5459
5460         if (status)
5461                 goto out;
5462
5463         /* cb_client4 */
5464         setclientid.sc_netid_len =
5465                                 nfs4_init_callback_netid(clp,
5466                                                 setclientid.sc_netid,
5467                                                 sizeof(setclientid.sc_netid));
5468         setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5469                                 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5470                                 clp->cl_ipaddr, port >> 8, port & 255);
5471
5472         dprintk("NFS call  setclientid auth=%s, '%s'\n",
5473                 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5474                 clp->cl_owner_id);
5475         task = rpc_run_task(&task_setup_data);
5476         if (IS_ERR(task)) {
5477                 status = PTR_ERR(task);
5478                 goto out;
5479         }
5480         status = task->tk_status;
5481         if (setclientid.sc_cred) {
5482                 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5483                 put_rpccred(setclientid.sc_cred);
5484         }
5485         rpc_put_task(task);
5486 out:
5487         trace_nfs4_setclientid(clp, status);
5488         dprintk("NFS reply setclientid: %d\n", status);
5489         return status;
5490 }
5491
5492 /**
5493  * nfs4_proc_setclientid_confirm - Confirm client ID
5494  * @clp: state data structure
5495  * @res: result of a previous SETCLIENTID
5496  * @cred: RPC credential to use for this call
5497  *
5498  * Returns zero, a negative errno, or a negative NFS4ERR status code.
5499  */
5500 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5501                 struct nfs4_setclientid_res *arg,
5502                 struct rpc_cred *cred)
5503 {
5504         struct rpc_message msg = {
5505                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5506                 .rpc_argp = arg,
5507                 .rpc_cred = cred,
5508         };
5509         int status;
5510
5511         dprintk("NFS call  setclientid_confirm auth=%s, (client ID %llx)\n",
5512                 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5513                 clp->cl_clientid);
5514         status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5515         trace_nfs4_setclientid_confirm(clp, status);
5516         dprintk("NFS reply setclientid_confirm: %d\n", status);
5517         return status;
5518 }
5519
5520 struct nfs4_delegreturndata {
5521         struct nfs4_delegreturnargs args;
5522         struct nfs4_delegreturnres res;
5523         struct nfs_fh fh;
5524         nfs4_stateid stateid;
5525         unsigned long timestamp;
5526         struct {
5527                 struct nfs4_layoutreturn_args arg;
5528                 struct nfs4_layoutreturn_res res;
5529                 struct nfs4_xdr_opaque_data ld_private;
5530                 u32 roc_barrier;
5531                 bool roc;
5532         } lr;
5533         struct nfs_fattr fattr;
5534         int rpc_status;
5535         struct inode *inode;
5536 };
5537
5538 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5539 {
5540         struct nfs4_delegreturndata *data = calldata;
5541
5542         if (!nfs4_sequence_done(task, &data->res.seq_res))
5543                 return;
5544
5545         trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5546
5547         /* Handle Layoutreturn errors */
5548         if (data->args.lr_args && task->tk_status != 0) {
5549                 switch(data->res.lr_ret) {
5550                 default:
5551                         data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5552                         break;
5553                 case 0:
5554                         data->args.lr_args = NULL;
5555                         data->res.lr_res = NULL;
5556                         break;
5557                 case -NFS4ERR_ADMIN_REVOKED:
5558                 case -NFS4ERR_DELEG_REVOKED:
5559                 case -NFS4ERR_EXPIRED:
5560                 case -NFS4ERR_BAD_STATEID:
5561                 case -NFS4ERR_OLD_STATEID:
5562                 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5563                 case -NFS4ERR_WRONG_CRED:
5564                         data->args.lr_args = NULL;
5565                         data->res.lr_res = NULL;
5566                         data->res.lr_ret = 0;
5567                         rpc_restart_call_prepare(task);
5568                         return;
5569                 }
5570         }
5571
5572         switch (task->tk_status) {
5573         case 0:
5574                 renew_lease(data->res.server, data->timestamp);
5575                 break;
5576         case -NFS4ERR_ADMIN_REVOKED:
5577         case -NFS4ERR_DELEG_REVOKED:
5578         case -NFS4ERR_EXPIRED:
5579                 nfs4_free_revoked_stateid(data->res.server,
5580                                 data->args.stateid,
5581                                 task->tk_msg.rpc_cred);
5582         case -NFS4ERR_BAD_STATEID:
5583         case -NFS4ERR_OLD_STATEID:
5584         case -NFS4ERR_STALE_STATEID:
5585                 task->tk_status = 0;
5586                 break;
5587         case -NFS4ERR_ACCESS:
5588                 if (data->args.bitmask) {
5589                         data->args.bitmask = NULL;
5590                         data->res.fattr = NULL;
5591                         task->tk_status = 0;
5592                         rpc_restart_call_prepare(task);
5593                         return;
5594                 }
5595         default:
5596                 if (nfs4_async_handle_error(task, data->res.server,
5597                                             NULL, NULL) == -EAGAIN) {
5598                         rpc_restart_call_prepare(task);
5599                         return;
5600                 }
5601         }
5602         data->rpc_status = task->tk_status;
5603 }
5604
5605 static void nfs4_delegreturn_release(void *calldata)
5606 {
5607         struct nfs4_delegreturndata *data = calldata;
5608         struct inode *inode = data->inode;
5609
5610         if (inode) {
5611                 if (data->lr.roc)
5612                         pnfs_roc_release(&data->lr.arg, &data->lr.res,
5613                                         data->res.lr_ret);
5614                 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5615                 nfs_iput_and_deactive(inode);
5616         }
5617         kfree(calldata);
5618 }
5619
5620 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5621 {
5622         struct nfs4_delegreturndata *d_data;
5623
5624         d_data = (struct nfs4_delegreturndata *)data;
5625
5626         if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
5627                 return;
5628
5629         nfs4_setup_sequence(d_data->res.server->nfs_client,
5630                         &d_data->args.seq_args,
5631                         &d_data->res.seq_res,
5632                         task);
5633 }
5634
5635 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5636         .rpc_call_prepare = nfs4_delegreturn_prepare,
5637         .rpc_call_done = nfs4_delegreturn_done,
5638         .rpc_release = nfs4_delegreturn_release,
5639 };
5640
5641 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5642 {
5643         struct nfs4_delegreturndata *data;
5644         struct nfs_server *server = NFS_SERVER(inode);
5645         struct rpc_task *task;
5646         struct rpc_message msg = {
5647                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5648                 .rpc_cred = cred,
5649         };
5650         struct rpc_task_setup task_setup_data = {
5651                 .rpc_client = server->client,
5652                 .rpc_message = &msg,
5653                 .callback_ops = &nfs4_delegreturn_ops,
5654                 .flags = RPC_TASK_ASYNC,
5655         };
5656         int status = 0;
5657
5658         data = kzalloc(sizeof(*data), GFP_NOFS);
5659         if (data == NULL)
5660                 return -ENOMEM;
5661         nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5662
5663         nfs4_state_protect(server->nfs_client,
5664                         NFS_SP4_MACH_CRED_CLEANUP,
5665                         &task_setup_data.rpc_client, &msg);
5666
5667         data->args.fhandle = &data->fh;
5668         data->args.stateid = &data->stateid;
5669         data->args.bitmask = server->cache_consistency_bitmask;
5670         nfs_copy_fh(&data->fh, NFS_FH(inode));
5671         nfs4_stateid_copy(&data->stateid, stateid);
5672         data->res.fattr = &data->fattr;
5673         data->res.server = server;
5674         data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5675         data->lr.arg.ld_private = &data->lr.ld_private;
5676         nfs_fattr_init(data->res.fattr);
5677         data->timestamp = jiffies;
5678         data->rpc_status = 0;
5679         data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
5680         data->inode = nfs_igrab_and_active(inode);
5681         if (data->inode) {
5682                 if (data->lr.roc) {
5683                         data->args.lr_args = &data->lr.arg;
5684                         data->res.lr_res = &data->lr.res;
5685                 }
5686         } else if (data->lr.roc) {
5687                 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5688                 data->lr.roc = false;
5689         }
5690
5691         task_setup_data.callback_data = data;
5692         msg.rpc_argp = &data->args;
5693         msg.rpc_resp = &data->res;
5694         task = rpc_run_task(&task_setup_data);
5695         if (IS_ERR(task))
5696                 return PTR_ERR(task);
5697         if (!issync)
5698                 goto out;
5699         status = rpc_wait_for_completion_task(task);
5700         if (status != 0)
5701                 goto out;
5702         status = data->rpc_status;
5703 out:
5704         rpc_put_task(task);
5705         return status;
5706 }
5707
5708 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5709 {
5710         struct nfs_server *server = NFS_SERVER(inode);
5711         struct nfs4_exception exception = { };
5712         int err;
5713         do {
5714                 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5715                 trace_nfs4_delegreturn(inode, stateid, err);
5716                 switch (err) {
5717                         case -NFS4ERR_STALE_STATEID:
5718                         case -NFS4ERR_EXPIRED:
5719                         case 0:
5720                                 return 0;
5721                 }
5722                 err = nfs4_handle_exception(server, err, &exception);
5723         } while (exception.retry);
5724         return err;
5725 }
5726
5727 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5728 {
5729         struct inode *inode = state->inode;
5730         struct nfs_server *server = NFS_SERVER(inode);
5731         struct nfs_client *clp = server->nfs_client;
5732         struct nfs_lockt_args arg = {
5733                 .fh = NFS_FH(inode),
5734                 .fl = request,
5735         };
5736         struct nfs_lockt_res res = {
5737                 .denied = request,
5738         };
5739         struct rpc_message msg = {
5740                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5741                 .rpc_argp       = &arg,
5742                 .rpc_resp       = &res,
5743                 .rpc_cred       = state->owner->so_cred,
5744         };
5745         struct nfs4_lock_state *lsp;
5746         int status;
5747
5748         arg.lock_owner.clientid = clp->cl_clientid;
5749         status = nfs4_set_lock_state(state, request);
5750         if (status != 0)
5751                 goto out;
5752         lsp = request->fl_u.nfs4_fl.owner;
5753         arg.lock_owner.id = lsp->ls_seqid.owner_id;
5754         arg.lock_owner.s_dev = server->s_dev;
5755         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5756         switch (status) {
5757                 case 0:
5758                         request->fl_type = F_UNLCK;
5759                         break;
5760                 case -NFS4ERR_DENIED:
5761                         status = 0;
5762         }
5763         request->fl_ops->fl_release_private(request);
5764         request->fl_ops = NULL;
5765 out:
5766         return status;
5767 }
5768
5769 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5770 {
5771         struct nfs4_exception exception = { };
5772         int err;
5773
5774         do {
5775                 err = _nfs4_proc_getlk(state, cmd, request);
5776                 trace_nfs4_get_lock(request, state, cmd, err);
5777                 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5778                                 &exception);
5779         } while (exception.retry);
5780         return err;
5781 }
5782
5783 struct nfs4_unlockdata {
5784         struct nfs_locku_args arg;
5785         struct nfs_locku_res res;
5786         struct nfs4_lock_state *lsp;
5787         struct nfs_open_context *ctx;
5788         struct file_lock fl;
5789         struct nfs_server *server;
5790         unsigned long timestamp;
5791 };
5792
5793 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5794                 struct nfs_open_context *ctx,
5795                 struct nfs4_lock_state *lsp,
5796                 struct nfs_seqid *seqid)
5797 {
5798         struct nfs4_unlockdata *p;
5799         struct inode *inode = lsp->ls_state->inode;
5800
5801         p = kzalloc(sizeof(*p), GFP_NOFS);
5802         if (p == NULL)
5803                 return NULL;
5804         p->arg.fh = NFS_FH(inode);
5805         p->arg.fl = &p->fl;
5806         p->arg.seqid = seqid;
5807         p->res.seqid = seqid;
5808         p->lsp = lsp;
5809         atomic_inc(&lsp->ls_count);
5810         /* Ensure we don't close file until we're done freeing locks! */
5811         p->ctx = get_nfs_open_context(ctx);
5812         memcpy(&p->fl, fl, sizeof(p->fl));
5813         p->server = NFS_SERVER(inode);
5814         return p;
5815 }
5816
5817 static void nfs4_locku_release_calldata(void *data)
5818 {
5819         struct nfs4_unlockdata *calldata = data;
5820         nfs_free_seqid(calldata->arg.seqid);
5821         nfs4_put_lock_state(calldata->lsp);
5822         put_nfs_open_context(calldata->ctx);
5823         kfree(calldata);
5824 }
5825
5826 static void nfs4_locku_done(struct rpc_task *task, void *data)
5827 {
5828         struct nfs4_unlockdata *calldata = data;
5829
5830         if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5831                 return;
5832         switch (task->tk_status) {
5833                 case 0:
5834                         renew_lease(calldata->server, calldata->timestamp);
5835                         locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
5836                         if (nfs4_update_lock_stateid(calldata->lsp,
5837                                         &calldata->res.stateid))
5838                                 break;
5839                 case -NFS4ERR_ADMIN_REVOKED:
5840                 case -NFS4ERR_EXPIRED:
5841                         nfs4_free_revoked_stateid(calldata->server,
5842                                         &calldata->arg.stateid,
5843                                         task->tk_msg.rpc_cred);
5844                 case -NFS4ERR_BAD_STATEID:
5845                 case -NFS4ERR_OLD_STATEID:
5846                 case -NFS4ERR_STALE_STATEID:
5847                         if (!nfs4_stateid_match(&calldata->arg.stateid,
5848                                                 &calldata->lsp->ls_stateid))
5849                                 rpc_restart_call_prepare(task);
5850                         break;
5851                 default:
5852                         if (nfs4_async_handle_error(task, calldata->server,
5853                                                     NULL, NULL) == -EAGAIN)
5854                                 rpc_restart_call_prepare(task);
5855         }
5856         nfs_release_seqid(calldata->arg.seqid);
5857 }
5858
5859 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5860 {
5861         struct nfs4_unlockdata *calldata = data;
5862
5863         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5864                 goto out_wait;
5865         nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
5866         if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5867                 /* Note: exit _without_ running nfs4_locku_done */
5868                 goto out_no_action;
5869         }
5870         calldata->timestamp = jiffies;
5871         if (nfs4_setup_sequence(calldata->server->nfs_client,
5872                                 &calldata->arg.seq_args,
5873                                 &calldata->res.seq_res,
5874                                 task) != 0)
5875                 nfs_release_seqid(calldata->arg.seqid);
5876         return;
5877 out_no_action:
5878         task->tk_action = NULL;
5879 out_wait:
5880         nfs4_sequence_done(task, &calldata->res.seq_res);
5881 }
5882
5883 static const struct rpc_call_ops nfs4_locku_ops = {
5884         .rpc_call_prepare = nfs4_locku_prepare,
5885         .rpc_call_done = nfs4_locku_done,
5886         .rpc_release = nfs4_locku_release_calldata,
5887 };
5888
5889 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5890                 struct nfs_open_context *ctx,
5891                 struct nfs4_lock_state *lsp,
5892                 struct nfs_seqid *seqid)
5893 {
5894         struct nfs4_unlockdata *data;
5895         struct rpc_message msg = {
5896                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5897                 .rpc_cred = ctx->cred,
5898         };
5899         struct rpc_task_setup task_setup_data = {
5900                 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5901                 .rpc_message = &msg,
5902                 .callback_ops = &nfs4_locku_ops,
5903                 .workqueue = nfsiod_workqueue,
5904                 .flags = RPC_TASK_ASYNC,
5905         };
5906
5907         nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5908                 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5909
5910         /* Ensure this is an unlock - when canceling a lock, the
5911          * canceled lock is passed in, and it won't be an unlock.
5912          */
5913         fl->fl_type = F_UNLCK;
5914
5915         data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5916         if (data == NULL) {
5917                 nfs_free_seqid(seqid);
5918                 return ERR_PTR(-ENOMEM);
5919         }
5920
5921         nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5922         msg.rpc_argp = &data->arg;
5923         msg.rpc_resp = &data->res;
5924         task_setup_data.callback_data = data;
5925         return rpc_run_task(&task_setup_data);
5926 }
5927
5928 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5929 {
5930         struct inode *inode = state->inode;
5931         struct nfs4_state_owner *sp = state->owner;
5932         struct nfs_inode *nfsi = NFS_I(inode);
5933         struct nfs_seqid *seqid;
5934         struct nfs4_lock_state *lsp;
5935         struct rpc_task *task;
5936         struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5937         int status = 0;
5938         unsigned char fl_flags = request->fl_flags;
5939
5940         status = nfs4_set_lock_state(state, request);
5941         /* Unlock _before_ we do the RPC call */
5942         request->fl_flags |= FL_EXISTS;
5943         /* Exclude nfs_delegation_claim_locks() */
5944         mutex_lock(&sp->so_delegreturn_mutex);
5945         /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5946         down_read(&nfsi->rwsem);
5947         if (locks_lock_inode_wait(inode, request) == -ENOENT) {
5948                 up_read(&nfsi->rwsem);
5949                 mutex_unlock(&sp->so_delegreturn_mutex);
5950                 goto out;
5951         }
5952         up_read(&nfsi->rwsem);
5953         mutex_unlock(&sp->so_delegreturn_mutex);
5954         if (status != 0)
5955                 goto out;
5956         /* Is this a delegated lock? */
5957         lsp = request->fl_u.nfs4_fl.owner;
5958         if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5959                 goto out;
5960         alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5961         seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5962         status = -ENOMEM;
5963         if (IS_ERR(seqid))
5964                 goto out;
5965         task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5966         status = PTR_ERR(task);
5967         if (IS_ERR(task))
5968                 goto out;
5969         status = rpc_wait_for_completion_task(task);
5970         rpc_put_task(task);
5971 out:
5972         request->fl_flags = fl_flags;
5973         trace_nfs4_unlock(request, state, F_SETLK, status);
5974         return status;
5975 }
5976
5977 struct nfs4_lockdata {
5978         struct nfs_lock_args arg;
5979         struct nfs_lock_res res;
5980         struct nfs4_lock_state *lsp;
5981         struct nfs_open_context *ctx;
5982         struct file_lock fl;
5983         unsigned long timestamp;
5984         int rpc_status;
5985         int cancelled;
5986         struct nfs_server *server;
5987 };
5988
5989 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5990                 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5991                 gfp_t gfp_mask)
5992 {
5993         struct nfs4_lockdata *p;
5994         struct inode *inode = lsp->ls_state->inode;
5995         struct nfs_server *server = NFS_SERVER(inode);
5996         struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5997
5998         p = kzalloc(sizeof(*p), gfp_mask);
5999         if (p == NULL)
6000                 return NULL;
6001
6002         p->arg.fh = NFS_FH(inode);
6003         p->arg.fl = &p->fl;
6004         p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6005         if (IS_ERR(p->arg.open_seqid))
6006                 goto out_free;
6007         alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6008         p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6009         if (IS_ERR(p->arg.lock_seqid))
6010                 goto out_free_seqid;
6011         p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6012         p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6013         p->arg.lock_owner.s_dev = server->s_dev;
6014         p->res.lock_seqid = p->arg.lock_seqid;
6015         p->lsp = lsp;
6016         p->server = server;
6017         atomic_inc(&lsp->ls_count);
6018         p->ctx = get_nfs_open_context(ctx);
6019         memcpy(&p->fl, fl, sizeof(p->fl));
6020         return p;
6021 out_free_seqid:
6022         nfs_free_seqid(p->arg.open_seqid);
6023 out_free:
6024         kfree(p);
6025         return NULL;
6026 }
6027
6028 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6029 {
6030         struct nfs4_lockdata *data = calldata;
6031         struct nfs4_state *state = data->lsp->ls_state;
6032
6033         dprintk("%s: begin!\n", __func__);
6034         if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6035                 goto out_wait;
6036         /* Do we need to do an open_to_lock_owner? */
6037         if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6038                 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6039                         goto out_release_lock_seqid;
6040                 }
6041                 nfs4_stateid_copy(&data->arg.open_stateid,
6042                                 &state->open_stateid);
6043                 data->arg.new_lock_owner = 1;
6044                 data->res.open_seqid = data->arg.open_seqid;
6045         } else {
6046                 data->arg.new_lock_owner = 0;
6047                 nfs4_stateid_copy(&data->arg.lock_stateid,
6048                                 &data->lsp->ls_stateid);
6049         }
6050         if (!nfs4_valid_open_stateid(state)) {
6051                 data->rpc_status = -EBADF;
6052                 task->tk_action = NULL;
6053                 goto out_release_open_seqid;
6054         }
6055         data->timestamp = jiffies;
6056         if (nfs4_setup_sequence(data->server->nfs_client,
6057                                 &data->arg.seq_args,
6058                                 &data->res.seq_res,
6059                                 task) == 0)
6060                 return;
6061 out_release_open_seqid:
6062         nfs_release_seqid(data->arg.open_seqid);
6063 out_release_lock_seqid:
6064         nfs_release_seqid(data->arg.lock_seqid);
6065 out_wait:
6066         nfs4_sequence_done(task, &data->res.seq_res);
6067         dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6068 }
6069
6070 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6071 {
6072         struct nfs4_lockdata *data = calldata;
6073         struct nfs4_lock_state *lsp = data->lsp;
6074
6075         dprintk("%s: begin!\n", __func__);
6076
6077         if (!nfs4_sequence_done(task, &data->res.seq_res))
6078                 return;
6079
6080         data->rpc_status = task->tk_status;
6081         switch (task->tk_status) {
6082         case 0:
6083                 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6084                                 data->timestamp);
6085                 if (data->arg.new_lock) {
6086                         data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6087                         if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
6088                                 rpc_restart_call_prepare(task);
6089                                 break;
6090                         }
6091                 }
6092                 if (data->arg.new_lock_owner != 0) {
6093                         nfs_confirm_seqid(&lsp->ls_seqid, 0);
6094                         nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6095                         set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6096                 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6097                         rpc_restart_call_prepare(task);
6098                 break;
6099         case -NFS4ERR_BAD_STATEID:
6100         case -NFS4ERR_OLD_STATEID:
6101         case -NFS4ERR_STALE_STATEID:
6102         case -NFS4ERR_EXPIRED:
6103                 if (data->arg.new_lock_owner != 0) {
6104                         if (!nfs4_stateid_match(&data->arg.open_stateid,
6105                                                 &lsp->ls_state->open_stateid))
6106                                 rpc_restart_call_prepare(task);
6107                 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6108                                                 &lsp->ls_stateid))
6109                                 rpc_restart_call_prepare(task);
6110         }
6111         dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6112 }
6113
6114 static void nfs4_lock_release(void *calldata)
6115 {
6116         struct nfs4_lockdata *data = calldata;
6117
6118         dprintk("%s: begin!\n", __func__);
6119         nfs_free_seqid(data->arg.open_seqid);
6120         if (data->cancelled != 0) {
6121                 struct rpc_task *task;
6122                 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6123                                 data->arg.lock_seqid);
6124                 if (!IS_ERR(task))
6125                         rpc_put_task_async(task);
6126                 dprintk("%s: cancelling lock!\n", __func__);
6127         } else
6128                 nfs_free_seqid(data->arg.lock_seqid);
6129         nfs4_put_lock_state(data->lsp);
6130         put_nfs_open_context(data->ctx);
6131         kfree(data);
6132         dprintk("%s: done!\n", __func__);
6133 }
6134
6135 static const struct rpc_call_ops nfs4_lock_ops = {
6136         .rpc_call_prepare = nfs4_lock_prepare,
6137         .rpc_call_done = nfs4_lock_done,
6138         .rpc_release = nfs4_lock_release,
6139 };
6140
6141 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6142 {
6143         switch (error) {
6144         case -NFS4ERR_ADMIN_REVOKED:
6145         case -NFS4ERR_EXPIRED:
6146         case -NFS4ERR_BAD_STATEID:
6147                 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6148                 if (new_lock_owner != 0 ||
6149                    test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6150                         nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6151                 break;
6152         case -NFS4ERR_STALE_STATEID:
6153                 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6154                 nfs4_schedule_lease_recovery(server->nfs_client);
6155         };
6156 }
6157
6158 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6159 {
6160         struct nfs4_lockdata *data;
6161         struct rpc_task *task;
6162         struct rpc_message msg = {
6163                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6164                 .rpc_cred = state->owner->so_cred,
6165         };
6166         struct rpc_task_setup task_setup_data = {
6167                 .rpc_client = NFS_CLIENT(state->inode),
6168                 .rpc_message = &msg,
6169                 .callback_ops = &nfs4_lock_ops,
6170                 .workqueue = nfsiod_workqueue,
6171                 .flags = RPC_TASK_ASYNC,
6172         };
6173         int ret;
6174
6175         dprintk("%s: begin!\n", __func__);
6176         data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6177                         fl->fl_u.nfs4_fl.owner,
6178                         recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6179         if (data == NULL)
6180                 return -ENOMEM;
6181         if (IS_SETLKW(cmd))
6182                 data->arg.block = 1;
6183         nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6184         msg.rpc_argp = &data->arg;
6185         msg.rpc_resp = &data->res;
6186         task_setup_data.callback_data = data;
6187         if (recovery_type > NFS_LOCK_NEW) {
6188                 if (recovery_type == NFS_LOCK_RECLAIM)
6189                         data->arg.reclaim = NFS_LOCK_RECLAIM;
6190                 nfs4_set_sequence_privileged(&data->arg.seq_args);
6191         } else
6192                 data->arg.new_lock = 1;
6193         task = rpc_run_task(&task_setup_data);
6194         if (IS_ERR(task))
6195                 return PTR_ERR(task);
6196         ret = rpc_wait_for_completion_task(task);
6197         if (ret == 0) {
6198                 ret = data->rpc_status;
6199                 if (ret)
6200                         nfs4_handle_setlk_error(data->server, data->lsp,
6201                                         data->arg.new_lock_owner, ret);
6202         } else
6203                 data->cancelled = 1;
6204         rpc_put_task(task);
6205         dprintk("%s: done, ret = %d!\n", __func__, ret);
6206         trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6207         return ret;
6208 }
6209
6210 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6211 {
6212         struct nfs_server *server = NFS_SERVER(state->inode);
6213         struct nfs4_exception exception = {
6214                 .inode = state->inode,
6215         };
6216         int err;
6217
6218         do {
6219                 /* Cache the lock if possible... */
6220                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6221                         return 0;
6222                 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6223                 if (err != -NFS4ERR_DELAY)
6224                         break;
6225                 nfs4_handle_exception(server, err, &exception);
6226         } while (exception.retry);
6227         return err;
6228 }
6229
6230 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6231 {
6232         struct nfs_server *server = NFS_SERVER(state->inode);
6233         struct nfs4_exception exception = {
6234                 .inode = state->inode,
6235         };
6236         int err;
6237
6238         err = nfs4_set_lock_state(state, request);
6239         if (err != 0)
6240                 return err;
6241         if (!recover_lost_locks) {
6242                 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6243                 return 0;
6244         }
6245         do {
6246                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6247                         return 0;
6248                 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6249                 switch (err) {
6250                 default:
6251                         goto out;
6252                 case -NFS4ERR_GRACE:
6253                 case -NFS4ERR_DELAY:
6254                         nfs4_handle_exception(server, err, &exception);
6255                         err = 0;
6256                 }
6257         } while (exception.retry);
6258 out:
6259         return err;
6260 }
6261
6262 #if defined(CONFIG_NFS_V4_1)
6263 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6264 {
6265         struct nfs4_lock_state *lsp;
6266         int status;
6267
6268         status = nfs4_set_lock_state(state, request);
6269         if (status != 0)
6270                 return status;
6271         lsp = request->fl_u.nfs4_fl.owner;
6272         if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6273             test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6274                 return 0;
6275         return nfs4_lock_expired(state, request);
6276 }
6277 #endif
6278
6279 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6280 {
6281         struct nfs_inode *nfsi = NFS_I(state->inode);
6282         struct nfs4_state_owner *sp = state->owner;
6283         unsigned char fl_flags = request->fl_flags;
6284         int status;
6285
6286         request->fl_flags |= FL_ACCESS;
6287         status = locks_lock_inode_wait(state->inode, request);
6288         if (status < 0)
6289                 goto out;
6290         mutex_lock(&sp->so_delegreturn_mutex);
6291         down_read(&nfsi->rwsem);
6292         if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6293                 /* Yes: cache locks! */
6294                 /* ...but avoid races with delegation recall... */
6295                 request->fl_flags = fl_flags & ~FL_SLEEP;
6296                 status = locks_lock_inode_wait(state->inode, request);
6297                 up_read(&nfsi->rwsem);
6298                 mutex_unlock(&sp->so_delegreturn_mutex);
6299                 goto out;
6300         }
6301         up_read(&nfsi->rwsem);
6302         mutex_unlock(&sp->so_delegreturn_mutex);
6303         status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6304 out:
6305         request->fl_flags = fl_flags;
6306         return status;
6307 }
6308
6309 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6310 {
6311         struct nfs4_exception exception = {
6312                 .state = state,
6313                 .inode = state->inode,
6314         };
6315         int err;
6316
6317         do {
6318                 err = _nfs4_proc_setlk(state, cmd, request);
6319                 if (err == -NFS4ERR_DENIED)
6320                         err = -EAGAIN;
6321                 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6322                                 err, &exception);
6323         } while (exception.retry);
6324         return err;
6325 }
6326
6327 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6328 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6329
6330 static int
6331 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6332                         struct file_lock *request)
6333 {
6334         int             status = -ERESTARTSYS;
6335         unsigned long   timeout = NFS4_LOCK_MINTIMEOUT;
6336
6337         while(!signalled()) {
6338                 status = nfs4_proc_setlk(state, cmd, request);
6339                 if ((status != -EAGAIN) || IS_SETLK(cmd))
6340                         break;
6341                 freezable_schedule_timeout_interruptible(timeout);
6342                 timeout *= 2;
6343                 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6344                 status = -ERESTARTSYS;
6345         }
6346         return status;
6347 }
6348
6349 #ifdef CONFIG_NFS_V4_1
6350 struct nfs4_lock_waiter {
6351         struct task_struct      *task;
6352         struct inode            *inode;
6353         struct nfs_lowner       *owner;
6354         bool                    notified;
6355 };
6356
6357 static int
6358 nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6359 {
6360         int ret;
6361         struct cb_notify_lock_args *cbnl = key;
6362         struct nfs4_lock_waiter *waiter = wait->private;
6363         struct nfs_lowner       *lowner = &cbnl->cbnl_owner,
6364                                 *wowner = waiter->owner;
6365
6366         /* Only wake if the callback was for the same owner */
6367         if (lowner->clientid != wowner->clientid ||
6368             lowner->id != wowner->id             ||
6369             lowner->s_dev != wowner->s_dev)
6370                 return 0;
6371
6372         /* Make sure it's for the right inode */
6373         if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6374                 return 0;
6375
6376         waiter->notified = true;
6377
6378         /* override "private" so we can use default_wake_function */
6379         wait->private = waiter->task;
6380         ret = autoremove_wake_function(wait, mode, flags, key);
6381         wait->private = waiter;
6382         return ret;
6383 }
6384
6385 static int
6386 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6387 {
6388         int status = -ERESTARTSYS;
6389         unsigned long flags;
6390         struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6391         struct nfs_server *server = NFS_SERVER(state->inode);
6392         struct nfs_client *clp = server->nfs_client;
6393         wait_queue_head_t *q = &clp->cl_lock_waitq;
6394         struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6395                                     .id = lsp->ls_seqid.owner_id,
6396                                     .s_dev = server->s_dev };
6397         struct nfs4_lock_waiter waiter = { .task  = current,
6398                                            .inode = state->inode,
6399                                            .owner = &owner,
6400                                            .notified = false };
6401         wait_queue_t wait;
6402
6403         /* Don't bother with waitqueue if we don't expect a callback */
6404         if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6405                 return nfs4_retry_setlk_simple(state, cmd, request);
6406
6407         init_wait(&wait);
6408         wait.private = &waiter;
6409         wait.func = nfs4_wake_lock_waiter;
6410         add_wait_queue(q, &wait);
6411
6412         while(!signalled()) {
6413                 status = nfs4_proc_setlk(state, cmd, request);
6414                 if ((status != -EAGAIN) || IS_SETLK(cmd))
6415                         break;
6416
6417                 status = -ERESTARTSYS;
6418                 spin_lock_irqsave(&q->lock, flags);
6419                 if (waiter.notified) {
6420                         spin_unlock_irqrestore(&q->lock, flags);
6421                         continue;
6422                 }
6423                 set_current_state(TASK_INTERRUPTIBLE);
6424                 spin_unlock_irqrestore(&q->lock, flags);
6425
6426                 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6427         }
6428
6429         finish_wait(q, &wait);
6430         return status;
6431 }
6432 #else /* !CONFIG_NFS_V4_1 */
6433 static inline int
6434 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6435 {
6436         return nfs4_retry_setlk_simple(state, cmd, request);
6437 }
6438 #endif
6439
6440 static int
6441 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6442 {
6443         struct nfs_open_context *ctx;
6444         struct nfs4_state *state;
6445         int status;
6446
6447         /* verify open state */
6448         ctx = nfs_file_open_context(filp);
6449         state = ctx->state;
6450
6451         if (request->fl_start < 0 || request->fl_end < 0)
6452                 return -EINVAL;
6453
6454         if (IS_GETLK(cmd)) {
6455                 if (state != NULL)
6456                         return nfs4_proc_getlk(state, F_GETLK, request);
6457                 return 0;
6458         }
6459
6460         if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6461                 return -EINVAL;
6462
6463         if (request->fl_type == F_UNLCK) {
6464                 if (state != NULL)
6465                         return nfs4_proc_unlck(state, cmd, request);
6466                 return 0;
6467         }
6468
6469         if (state == NULL)
6470                 return -ENOLCK;
6471
6472         if ((request->fl_flags & FL_POSIX) &&
6473             !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6474                 return -ENOLCK;
6475
6476         /*
6477          * Don't rely on the VFS having checked the file open mode,
6478          * since it won't do this for flock() locks.
6479          */
6480         switch (request->fl_type) {
6481         case F_RDLCK:
6482                 if (!(filp->f_mode & FMODE_READ))
6483                         return -EBADF;
6484                 break;
6485         case F_WRLCK:
6486                 if (!(filp->f_mode & FMODE_WRITE))
6487                         return -EBADF;
6488         }
6489
6490         status = nfs4_set_lock_state(state, request);
6491         if (status != 0)
6492                 return status;
6493
6494         return nfs4_retry_setlk(state, cmd, request);
6495 }
6496
6497 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6498 {
6499         struct nfs_server *server = NFS_SERVER(state->inode);
6500         int err;
6501
6502         err = nfs4_set_lock_state(state, fl);
6503         if (err != 0)
6504                 return err;
6505         err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6506         return nfs4_handle_delegation_recall_error(server, state, stateid, err);
6507 }
6508
6509 struct nfs_release_lockowner_data {
6510         struct nfs4_lock_state *lsp;
6511         struct nfs_server *server;
6512         struct nfs_release_lockowner_args args;
6513         struct nfs_release_lockowner_res res;
6514         unsigned long timestamp;
6515 };
6516
6517 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6518 {
6519         struct nfs_release_lockowner_data *data = calldata;
6520         struct nfs_server *server = data->server;
6521         nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6522                            &data->res.seq_res, task);
6523         data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6524         data->timestamp = jiffies;
6525 }
6526
6527 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6528 {
6529         struct nfs_release_lockowner_data *data = calldata;
6530         struct nfs_server *server = data->server;
6531
6532         nfs40_sequence_done(task, &data->res.seq_res);
6533
6534         switch (task->tk_status) {
6535         case 0:
6536                 renew_lease(server, data->timestamp);
6537                 break;
6538         case -NFS4ERR_STALE_CLIENTID:
6539         case -NFS4ERR_EXPIRED:
6540                 nfs4_schedule_lease_recovery(server->nfs_client);
6541                 break;
6542         case -NFS4ERR_LEASE_MOVED:
6543         case -NFS4ERR_DELAY:
6544                 if (nfs4_async_handle_error(task, server,
6545                                             NULL, NULL) == -EAGAIN)
6546                         rpc_restart_call_prepare(task);
6547         }
6548 }
6549
6550 static void nfs4_release_lockowner_release(void *calldata)
6551 {
6552         struct nfs_release_lockowner_data *data = calldata;
6553         nfs4_free_lock_state(data->server, data->lsp);
6554         kfree(calldata);
6555 }
6556
6557 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6558         .rpc_call_prepare = nfs4_release_lockowner_prepare,
6559         .rpc_call_done = nfs4_release_lockowner_done,
6560         .rpc_release = nfs4_release_lockowner_release,
6561 };
6562
6563 static void
6564 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6565 {
6566         struct nfs_release_lockowner_data *data;
6567         struct rpc_message msg = {
6568                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6569         };
6570
6571         if (server->nfs_client->cl_mvops->minor_version != 0)
6572                 return;
6573
6574         data = kmalloc(sizeof(*data), GFP_NOFS);
6575         if (!data)
6576                 return;
6577         data->lsp = lsp;
6578         data->server = server;
6579         data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6580         data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6581         data->args.lock_owner.s_dev = server->s_dev;
6582
6583         msg.rpc_argp = &data->args;
6584         msg.rpc_resp = &data->res;
6585         nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6586         rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6587 }
6588
6589 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6590
6591 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
6592                                    struct dentry *unused, struct inode *inode,
6593                                    const char *key, const void *buf,
6594                                    size_t buflen, int flags)
6595 {
6596         return nfs4_proc_set_acl(inode, buf, buflen);
6597 }
6598
6599 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
6600                                    struct dentry *unused, struct inode *inode,
6601                                    const char *key, void *buf, size_t buflen)
6602 {
6603         return nfs4_proc_get_acl(inode, buf, buflen);
6604 }
6605
6606 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
6607 {
6608         return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
6609 }
6610
6611 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6612
6613 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
6614                                      struct dentry *unused, struct inode *inode,
6615                                      const char *key, const void *buf,
6616                                      size_t buflen, int flags)
6617 {
6618         if (security_ismaclabel(key))
6619                 return nfs4_set_security_label(inode, buf, buflen);
6620
6621         return -EOPNOTSUPP;
6622 }
6623
6624 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
6625                                      struct dentry *unused, struct inode *inode,
6626                                      const char *key, void *buf, size_t buflen)
6627 {
6628         if (security_ismaclabel(key))
6629                 return nfs4_get_security_label(inode, buf, buflen);
6630         return -EOPNOTSUPP;
6631 }
6632
6633 static ssize_t
6634 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6635 {
6636         int len = 0;
6637
6638         if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6639                 len = security_inode_listsecurity(inode, list, list_len);
6640                 if (list_len && len > list_len)
6641                         return -ERANGE;
6642         }
6643         return len;
6644 }
6645
6646 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6647         .prefix = XATTR_SECURITY_PREFIX,
6648         .get    = nfs4_xattr_get_nfs4_label,
6649         .set    = nfs4_xattr_set_nfs4_label,
6650 };
6651
6652 #else
6653
6654 static ssize_t
6655 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6656 {
6657         return 0;
6658 }
6659
6660 #endif
6661
6662 /*
6663  * nfs_fhget will use either the mounted_on_fileid or the fileid
6664  */
6665 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6666 {
6667         if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6668                (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6669               (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6670               (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6671                 return;
6672
6673         fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6674                 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6675         fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6676         fattr->nlink = 2;
6677 }
6678
6679 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6680                                    const struct qstr *name,
6681                                    struct nfs4_fs_locations *fs_locations,
6682                                    struct page *page)
6683 {
6684         struct nfs_server *server = NFS_SERVER(dir);
6685         u32 bitmask[3] = {
6686                 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6687         };
6688         struct nfs4_fs_locations_arg args = {
6689                 .dir_fh = NFS_FH(dir),
6690                 .name = name,
6691                 .page = page,
6692                 .bitmask = bitmask,
6693         };
6694         struct nfs4_fs_locations_res res = {
6695                 .fs_locations = fs_locations,
6696         };
6697         struct rpc_message msg = {
6698                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6699                 .rpc_argp = &args,
6700                 .rpc_resp = &res,
6701         };
6702         int status;
6703
6704         dprintk("%s: start\n", __func__);
6705
6706         /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6707          * is not supported */
6708         if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6709                 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6710         else
6711                 bitmask[0] |= FATTR4_WORD0_FILEID;
6712
6713         nfs_fattr_init(&fs_locations->fattr);
6714         fs_locations->server = server;
6715         fs_locations->nlocations = 0;
6716         status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6717         dprintk("%s: returned status = %d\n", __func__, status);
6718         return status;
6719 }
6720
6721 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6722                            const struct qstr *name,
6723                            struct nfs4_fs_locations *fs_locations,
6724                            struct page *page)
6725 {
6726         struct nfs4_exception exception = { };
6727         int err;
6728         do {
6729                 err = _nfs4_proc_fs_locations(client, dir, name,
6730                                 fs_locations, page);
6731                 trace_nfs4_get_fs_locations(dir, name, err);
6732                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6733                                 &exception);
6734         } while (exception.retry);
6735         return err;
6736 }
6737
6738 /*
6739  * This operation also signals the server that this client is
6740  * performing migration recovery.  The server can stop returning
6741  * NFS4ERR_LEASE_MOVED to this client.  A RENEW operation is
6742  * appended to this compound to identify the client ID which is
6743  * performing recovery.
6744  */
6745 static int _nfs40_proc_get_locations(struct inode *inode,
6746                                      struct nfs4_fs_locations *locations,
6747                                      struct page *page, struct rpc_cred *cred)
6748 {
6749         struct nfs_server *server = NFS_SERVER(inode);
6750         struct rpc_clnt *clnt = server->client;
6751         u32 bitmask[2] = {
6752                 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6753         };
6754         struct nfs4_fs_locations_arg args = {
6755                 .clientid       = server->nfs_client->cl_clientid,
6756                 .fh             = NFS_FH(inode),
6757                 .page           = page,
6758                 .bitmask        = bitmask,
6759                 .migration      = 1,            /* skip LOOKUP */
6760                 .renew          = 1,            /* append RENEW */
6761         };
6762         struct nfs4_fs_locations_res res = {
6763                 .fs_locations   = locations,
6764                 .migration      = 1,
6765                 .renew          = 1,
6766         };
6767         struct rpc_message msg = {
6768                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6769                 .rpc_argp       = &args,
6770                 .rpc_resp       = &res,
6771                 .rpc_cred       = cred,
6772         };
6773         unsigned long now = jiffies;
6774         int status;
6775
6776         nfs_fattr_init(&locations->fattr);
6777         locations->server = server;
6778         locations->nlocations = 0;
6779
6780         nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6781         nfs4_set_sequence_privileged(&args.seq_args);
6782         status = nfs4_call_sync_sequence(clnt, server, &msg,
6783                                         &args.seq_args, &res.seq_res);
6784         if (status)
6785                 return status;
6786
6787         renew_lease(server, now);
6788         return 0;
6789 }
6790
6791 #ifdef CONFIG_NFS_V4_1
6792
6793 /*
6794  * This operation also signals the server that this client is
6795  * performing migration recovery.  The server can stop asserting
6796  * SEQ4_STATUS_LEASE_MOVED for this client.  The client ID
6797  * performing this operation is identified in the SEQUENCE
6798  * operation in this compound.
6799  *
6800  * When the client supports GETATTR(fs_locations_info), it can
6801  * be plumbed in here.
6802  */
6803 static int _nfs41_proc_get_locations(struct inode *inode,
6804                                      struct nfs4_fs_locations *locations,
6805                                      struct page *page, struct rpc_cred *cred)
6806 {
6807         struct nfs_server *server = NFS_SERVER(inode);
6808         struct rpc_clnt *clnt = server->client;
6809         u32 bitmask[2] = {
6810                 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6811         };
6812         struct nfs4_fs_locations_arg args = {
6813                 .fh             = NFS_FH(inode),
6814                 .page           = page,
6815                 .bitmask        = bitmask,
6816                 .migration      = 1,            /* skip LOOKUP */
6817         };
6818         struct nfs4_fs_locations_res res = {
6819                 .fs_locations   = locations,
6820                 .migration      = 1,
6821         };
6822         struct rpc_message msg = {
6823                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6824                 .rpc_argp       = &args,
6825                 .rpc_resp       = &res,
6826                 .rpc_cred       = cred,
6827         };
6828         int status;
6829
6830         nfs_fattr_init(&locations->fattr);
6831         locations->server = server;
6832         locations->nlocations = 0;
6833
6834         nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6835         nfs4_set_sequence_privileged(&args.seq_args);
6836         status = nfs4_call_sync_sequence(clnt, server, &msg,
6837                                         &args.seq_args, &res.seq_res);
6838         if (status == NFS4_OK &&
6839             res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6840                 status = -NFS4ERR_LEASE_MOVED;
6841         return status;
6842 }
6843
6844 #endif  /* CONFIG_NFS_V4_1 */
6845
6846 /**
6847  * nfs4_proc_get_locations - discover locations for a migrated FSID
6848  * @inode: inode on FSID that is migrating
6849  * @locations: result of query
6850  * @page: buffer
6851  * @cred: credential to use for this operation
6852  *
6853  * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6854  * operation failed, or a negative errno if a local error occurred.
6855  *
6856  * On success, "locations" is filled in, but if the server has
6857  * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6858  * asserted.
6859  *
6860  * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6861  * from this client that require migration recovery.
6862  */
6863 int nfs4_proc_get_locations(struct inode *inode,
6864                             struct nfs4_fs_locations *locations,
6865                             struct page *page, struct rpc_cred *cred)
6866 {
6867         struct nfs_server *server = NFS_SERVER(inode);
6868         struct nfs_client *clp = server->nfs_client;
6869         const struct nfs4_mig_recovery_ops *ops =
6870                                         clp->cl_mvops->mig_recovery_ops;
6871         struct nfs4_exception exception = { };
6872         int status;
6873
6874         dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6875                 (unsigned long long)server->fsid.major,
6876                 (unsigned long long)server->fsid.minor,
6877                 clp->cl_hostname);
6878         nfs_display_fhandle(NFS_FH(inode), __func__);
6879
6880         do {
6881                 status = ops->get_locations(inode, locations, page, cred);
6882                 if (status != -NFS4ERR_DELAY)
6883                         break;
6884                 nfs4_handle_exception(server, status, &exception);
6885         } while (exception.retry);
6886         return status;
6887 }
6888
6889 /*
6890  * This operation also signals the server that this client is
6891  * performing "lease moved" recovery.  The server can stop
6892  * returning NFS4ERR_LEASE_MOVED to this client.  A RENEW operation
6893  * is appended to this compound to identify the client ID which is
6894  * performing recovery.
6895  */
6896 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6897 {
6898         struct nfs_server *server = NFS_SERVER(inode);
6899         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6900         struct rpc_clnt *clnt = server->client;
6901         struct nfs4_fsid_present_arg args = {
6902                 .fh             = NFS_FH(inode),
6903                 .clientid       = clp->cl_clientid,
6904                 .renew          = 1,            /* append RENEW */
6905         };
6906         struct nfs4_fsid_present_res res = {
6907                 .renew          = 1,
6908         };
6909         struct rpc_message msg = {
6910                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6911                 .rpc_argp       = &args,
6912                 .rpc_resp       = &res,
6913                 .rpc_cred       = cred,
6914         };
6915         unsigned long now = jiffies;
6916         int status;
6917
6918         res.fh = nfs_alloc_fhandle();
6919         if (res.fh == NULL)
6920                 return -ENOMEM;
6921
6922         nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6923         nfs4_set_sequence_privileged(&args.seq_args);
6924         status = nfs4_call_sync_sequence(clnt, server, &msg,
6925                                                 &args.seq_args, &res.seq_res);
6926         nfs_free_fhandle(res.fh);
6927         if (status)
6928                 return status;
6929
6930         do_renew_lease(clp, now);
6931         return 0;
6932 }
6933
6934 #ifdef CONFIG_NFS_V4_1
6935
6936 /*
6937  * This operation also signals the server that this client is
6938  * performing "lease moved" recovery.  The server can stop asserting
6939  * SEQ4_STATUS_LEASE_MOVED for this client.  The client ID performing
6940  * this operation is identified in the SEQUENCE operation in this
6941  * compound.
6942  */
6943 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6944 {
6945         struct nfs_server *server = NFS_SERVER(inode);
6946         struct rpc_clnt *clnt = server->client;
6947         struct nfs4_fsid_present_arg args = {
6948                 .fh             = NFS_FH(inode),
6949         };
6950         struct nfs4_fsid_present_res res = {
6951         };
6952         struct rpc_message msg = {
6953                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6954                 .rpc_argp       = &args,
6955                 .rpc_resp       = &res,
6956                 .rpc_cred       = cred,
6957         };
6958         int status;
6959
6960         res.fh = nfs_alloc_fhandle();
6961         if (res.fh == NULL)
6962                 return -ENOMEM;
6963
6964         nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6965         nfs4_set_sequence_privileged(&args.seq_args);
6966         status = nfs4_call_sync_sequence(clnt, server, &msg,
6967                                                 &args.seq_args, &res.seq_res);
6968         nfs_free_fhandle(res.fh);
6969         if (status == NFS4_OK &&
6970             res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6971                 status = -NFS4ERR_LEASE_MOVED;
6972         return status;
6973 }
6974
6975 #endif  /* CONFIG_NFS_V4_1 */
6976
6977 /**
6978  * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6979  * @inode: inode on FSID to check
6980  * @cred: credential to use for this operation
6981  *
6982  * Server indicates whether the FSID is present, moved, or not
6983  * recognized.  This operation is necessary to clear a LEASE_MOVED
6984  * condition for this client ID.
6985  *
6986  * Returns NFS4_OK if the FSID is present on this server,
6987  * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6988  *  NFS4ERR code if some error occurred on the server, or a
6989  *  negative errno if a local failure occurred.
6990  */
6991 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6992 {
6993         struct nfs_server *server = NFS_SERVER(inode);
6994         struct nfs_client *clp = server->nfs_client;
6995         const struct nfs4_mig_recovery_ops *ops =
6996                                         clp->cl_mvops->mig_recovery_ops;
6997         struct nfs4_exception exception = { };
6998         int status;
6999
7000         dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7001                 (unsigned long long)server->fsid.major,
7002                 (unsigned long long)server->fsid.minor,
7003                 clp->cl_hostname);
7004         nfs_display_fhandle(NFS_FH(inode), __func__);
7005
7006         do {
7007                 status = ops->fsid_present(inode, cred);
7008                 if (status != -NFS4ERR_DELAY)
7009                         break;
7010                 nfs4_handle_exception(server, status, &exception);
7011         } while (exception.retry);
7012         return status;
7013 }
7014
7015 /**
7016  * If 'use_integrity' is true and the state managment nfs_client
7017  * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7018  * and the machine credential as per RFC3530bis and RFC5661 Security
7019  * Considerations sections. Otherwise, just use the user cred with the
7020  * filesystem's rpc_client.
7021  */
7022 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7023 {
7024         int status;
7025         struct nfs4_secinfo_arg args = {
7026                 .dir_fh = NFS_FH(dir),
7027                 .name   = name,
7028         };
7029         struct nfs4_secinfo_res res = {
7030                 .flavors     = flavors,
7031         };
7032         struct rpc_message msg = {
7033                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7034                 .rpc_argp = &args,
7035                 .rpc_resp = &res,
7036         };
7037         struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7038         struct rpc_cred *cred = NULL;
7039
7040         if (use_integrity) {
7041                 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7042                 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7043                 msg.rpc_cred = cred;
7044         }
7045
7046         dprintk("NFS call  secinfo %s\n", name->name);
7047
7048         nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7049                 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7050
7051         status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7052                                 &res.seq_res, 0);
7053         dprintk("NFS reply  secinfo: %d\n", status);
7054
7055         if (cred)
7056                 put_rpccred(cred);
7057
7058         return status;
7059 }
7060
7061 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7062                       struct nfs4_secinfo_flavors *flavors)
7063 {
7064         struct nfs4_exception exception = { };
7065         int err;
7066         do {
7067                 err = -NFS4ERR_WRONGSEC;
7068
7069                 /* try to use integrity protection with machine cred */
7070                 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7071                         err = _nfs4_proc_secinfo(dir, name, flavors, true);
7072
7073                 /*
7074                  * if unable to use integrity protection, or SECINFO with
7075                  * integrity protection returns NFS4ERR_WRONGSEC (which is
7076                  * disallowed by spec, but exists in deployed servers) use
7077                  * the current filesystem's rpc_client and the user cred.
7078                  */
7079                 if (err == -NFS4ERR_WRONGSEC)
7080                         err = _nfs4_proc_secinfo(dir, name, flavors, false);
7081
7082                 trace_nfs4_secinfo(dir, name, err);
7083                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7084                                 &exception);
7085         } while (exception.retry);
7086         return err;
7087 }
7088
7089 #ifdef CONFIG_NFS_V4_1
7090 /*
7091  * Check the exchange flags returned by the server for invalid flags, having
7092  * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7093  * DS flags set.
7094  */
7095 static int nfs4_check_cl_exchange_flags(u32 flags)
7096 {
7097         if (flags & ~EXCHGID4_FLAG_MASK_R)
7098                 goto out_inval;
7099         if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7100             (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7101                 goto out_inval;
7102         if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7103                 goto out_inval;
7104         return NFS_OK;
7105 out_inval:
7106         return -NFS4ERR_INVAL;
7107 }
7108
7109 static bool
7110 nfs41_same_server_scope(struct nfs41_server_scope *a,
7111                         struct nfs41_server_scope *b)
7112 {
7113         if (a->server_scope_sz != b->server_scope_sz)
7114                 return false;
7115         return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7116 }
7117
7118 static void
7119 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7120 {
7121 }
7122
7123 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7124         .rpc_call_done =  &nfs4_bind_one_conn_to_session_done,
7125 };
7126
7127 /*
7128  * nfs4_proc_bind_one_conn_to_session()
7129  *
7130  * The 4.1 client currently uses the same TCP connection for the
7131  * fore and backchannel.
7132  */
7133 static
7134 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7135                 struct rpc_xprt *xprt,
7136                 struct nfs_client *clp,
7137                 struct rpc_cred *cred)
7138 {
7139         int status;
7140         struct nfs41_bind_conn_to_session_args args = {
7141                 .client = clp,
7142                 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7143         };
7144         struct nfs41_bind_conn_to_session_res res;
7145         struct rpc_message msg = {
7146                 .rpc_proc =
7147                         &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7148                 .rpc_argp = &args,
7149                 .rpc_resp = &res,
7150                 .rpc_cred = cred,
7151         };
7152         struct rpc_task_setup task_setup_data = {
7153                 .rpc_client = clnt,
7154                 .rpc_xprt = xprt,
7155                 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7156                 .rpc_message = &msg,
7157                 .flags = RPC_TASK_TIMEOUT,
7158         };
7159         struct rpc_task *task;
7160
7161         dprintk("--> %s\n", __func__);
7162
7163         nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7164         if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7165                 args.dir = NFS4_CDFC4_FORE;
7166
7167         /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7168         if (xprt != rcu_access_pointer(clnt->cl_xprt))
7169                 args.dir = NFS4_CDFC4_FORE;
7170
7171         task = rpc_run_task(&task_setup_data);
7172         if (!IS_ERR(task)) {
7173                 status = task->tk_status;
7174                 rpc_put_task(task);
7175         } else
7176                 status = PTR_ERR(task);
7177         trace_nfs4_bind_conn_to_session(clp, status);
7178         if (status == 0) {
7179                 if (memcmp(res.sessionid.data,
7180                     clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7181                         dprintk("NFS: %s: Session ID mismatch\n", __func__);
7182                         status = -EIO;
7183                         goto out;
7184                 }
7185                 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7186                         dprintk("NFS: %s: Unexpected direction from server\n",
7187                                 __func__);
7188                         status = -EIO;
7189                         goto out;
7190                 }
7191                 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7192                         dprintk("NFS: %s: Server returned RDMA mode = true\n",
7193                                 __func__);
7194                         status = -EIO;
7195                         goto out;
7196                 }
7197         }
7198 out:
7199         dprintk("<-- %s status= %d\n", __func__, status);
7200         return status;
7201 }
7202
7203 struct rpc_bind_conn_calldata {
7204         struct nfs_client *clp;
7205         struct rpc_cred *cred;
7206 };
7207
7208 static int
7209 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7210                 struct rpc_xprt *xprt,
7211                 void *calldata)
7212 {
7213         struct rpc_bind_conn_calldata *p = calldata;
7214
7215         return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7216 }
7217
7218 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7219 {
7220         struct rpc_bind_conn_calldata data = {
7221                 .clp = clp,
7222                 .cred = cred,
7223         };
7224         return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7225                         nfs4_proc_bind_conn_to_session_callback, &data);
7226 }
7227
7228 /*
7229  * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7230  * and operations we'd like to see to enable certain features in the allow map
7231  */
7232 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7233         .how = SP4_MACH_CRED,
7234         .enforce.u.words = {
7235                 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7236                       1 << (OP_EXCHANGE_ID - 32) |
7237                       1 << (OP_CREATE_SESSION - 32) |
7238                       1 << (OP_DESTROY_SESSION - 32) |
7239                       1 << (OP_DESTROY_CLIENTID - 32)
7240         },
7241         .allow.u.words = {
7242                 [0] = 1 << (OP_CLOSE) |
7243                       1 << (OP_OPEN_DOWNGRADE) |
7244                       1 << (OP_LOCKU) |
7245                       1 << (OP_DELEGRETURN) |
7246                       1 << (OP_COMMIT),
7247                 [1] = 1 << (OP_SECINFO - 32) |
7248                       1 << (OP_SECINFO_NO_NAME - 32) |
7249                       1 << (OP_LAYOUTRETURN - 32) |
7250                       1 << (OP_TEST_STATEID - 32) |
7251                       1 << (OP_FREE_STATEID - 32) |
7252                       1 << (OP_WRITE - 32)
7253         }
7254 };
7255
7256 /*
7257  * Select the state protection mode for client `clp' given the server results
7258  * from exchange_id in `sp'.
7259  *
7260  * Returns 0 on success, negative errno otherwise.
7261  */
7262 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7263                                  struct nfs41_state_protection *sp)
7264 {
7265         static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7266                 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7267                       1 << (OP_EXCHANGE_ID - 32) |
7268                       1 << (OP_CREATE_SESSION - 32) |
7269                       1 << (OP_DESTROY_SESSION - 32) |
7270                       1 << (OP_DESTROY_CLIENTID - 32)
7271         };
7272         unsigned int i;
7273
7274         if (sp->how == SP4_MACH_CRED) {
7275                 /* Print state protect result */
7276                 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7277                 for (i = 0; i <= LAST_NFS4_OP; i++) {
7278                         if (test_bit(i, sp->enforce.u.longs))
7279                                 dfprintk(MOUNT, "  enforce op %d\n", i);
7280                         if (test_bit(i, sp->allow.u.longs))
7281                                 dfprintk(MOUNT, "  allow op %d\n", i);
7282                 }
7283
7284                 /* make sure nothing is on enforce list that isn't supported */
7285                 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7286                         if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7287                                 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7288                                 return -EINVAL;
7289                         }
7290                 }
7291
7292                 /*
7293                  * Minimal mode - state operations are allowed to use machine
7294                  * credential.  Note this already happens by default, so the
7295                  * client doesn't have to do anything more than the negotiation.
7296                  *
7297                  * NOTE: we don't care if EXCHANGE_ID is in the list -
7298                  *       we're already using the machine cred for exchange_id
7299                  *       and will never use a different cred.
7300                  */
7301                 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7302                     test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7303                     test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7304                     test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7305                         dfprintk(MOUNT, "sp4_mach_cred:\n");
7306                         dfprintk(MOUNT, "  minimal mode enabled\n");
7307                         set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7308                 } else {
7309                         dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7310                         return -EINVAL;
7311                 }
7312
7313                 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7314                     test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7315                     test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7316                     test_bit(OP_LOCKU, sp->allow.u.longs)) {
7317                         dfprintk(MOUNT, "  cleanup mode enabled\n");
7318                         set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7319                 }
7320
7321                 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7322                         dfprintk(MOUNT, "  pnfs cleanup mode enabled\n");
7323                         set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7324                                 &clp->cl_sp4_flags);
7325                 }
7326
7327                 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7328                     test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7329                         dfprintk(MOUNT, "  secinfo mode enabled\n");
7330                         set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7331                 }
7332
7333                 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7334                     test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7335                         dfprintk(MOUNT, "  stateid mode enabled\n");
7336                         set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7337                 }
7338
7339                 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7340                         dfprintk(MOUNT, "  write mode enabled\n");
7341                         set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7342                 }
7343
7344                 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7345                         dfprintk(MOUNT, "  commit mode enabled\n");
7346                         set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7347                 }
7348         }
7349
7350         return 0;
7351 }
7352
7353 struct nfs41_exchange_id_data {
7354         struct nfs41_exchange_id_res res;
7355         struct nfs41_exchange_id_args args;
7356         struct rpc_xprt *xprt;
7357         int rpc_status;
7358 };
7359
7360 static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7361 {
7362         struct nfs41_exchange_id_data *cdata =
7363                                         (struct nfs41_exchange_id_data *)data;
7364         struct nfs_client *clp = cdata->args.client;
7365         int status = task->tk_status;
7366
7367         trace_nfs4_exchange_id(clp, status);
7368
7369         if (status == 0)
7370                 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
7371
7372         if (cdata->xprt && status == 0) {
7373                 status = nfs4_detect_session_trunking(clp, &cdata->res,
7374                                                       cdata->xprt);
7375                 goto out;
7376         }
7377
7378         if (status  == 0)
7379                 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7380
7381         if (status == 0) {
7382                 clp->cl_clientid = cdata->res.clientid;
7383                 clp->cl_exchange_flags = cdata->res.flags;
7384                 /* Client ID is not confirmed */
7385                 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7386                         clear_bit(NFS4_SESSION_ESTABLISHED,
7387                         &clp->cl_session->session_state);
7388                         clp->cl_seqid = cdata->res.seqid;
7389                 }
7390
7391                 kfree(clp->cl_serverowner);
7392                 clp->cl_serverowner = cdata->res.server_owner;
7393                 cdata->res.server_owner = NULL;
7394
7395                 /* use the most recent implementation id */
7396                 kfree(clp->cl_implid);
7397                 clp->cl_implid = cdata->res.impl_id;
7398                 cdata->res.impl_id = NULL;
7399
7400                 if (clp->cl_serverscope != NULL &&
7401                     !nfs41_same_server_scope(clp->cl_serverscope,
7402                                         cdata->res.server_scope)) {
7403                         dprintk("%s: server_scope mismatch detected\n",
7404                                 __func__);
7405                         set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7406                         kfree(clp->cl_serverscope);
7407                         clp->cl_serverscope = NULL;
7408                 }
7409
7410                 if (clp->cl_serverscope == NULL) {
7411                         clp->cl_serverscope = cdata->res.server_scope;
7412                         cdata->res.server_scope = NULL;
7413                 }
7414                 /* Save the EXCHANGE_ID verifier session trunk tests */
7415                 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7416                        sizeof(clp->cl_confirm.data));
7417         }
7418 out:
7419         cdata->rpc_status = status;
7420         return;
7421 }
7422
7423 static void nfs4_exchange_id_release(void *data)
7424 {
7425         struct nfs41_exchange_id_data *cdata =
7426                                         (struct nfs41_exchange_id_data *)data;
7427
7428         if (cdata->xprt) {
7429                 xprt_put(cdata->xprt);
7430                 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7431         }
7432         nfs_put_client(cdata->args.client);
7433         kfree(cdata->res.impl_id);
7434         kfree(cdata->res.server_scope);
7435         kfree(cdata->res.server_owner);
7436         kfree(cdata);
7437 }
7438
7439 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7440         .rpc_call_done = nfs4_exchange_id_done,
7441         .rpc_release = nfs4_exchange_id_release,
7442 };
7443
7444 /*
7445  * _nfs4_proc_exchange_id()
7446  *
7447  * Wrapper for EXCHANGE_ID operation.
7448  */
7449 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7450                         u32 sp4_how, struct rpc_xprt *xprt)
7451 {
7452         nfs4_verifier verifier;
7453         struct rpc_message msg = {
7454                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7455                 .rpc_cred = cred,
7456         };
7457         struct rpc_task_setup task_setup_data = {
7458                 .rpc_client = clp->cl_rpcclient,
7459                 .callback_ops = &nfs4_exchange_id_call_ops,
7460                 .rpc_message = &msg,
7461                 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7462         };
7463         struct nfs41_exchange_id_data *calldata;
7464         struct rpc_task *task;
7465         int status = -EIO;
7466
7467         if (!atomic_inc_not_zero(&clp->cl_count))
7468                 goto out;
7469
7470         status = -ENOMEM;
7471         calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7472         if (!calldata)
7473                 goto out;
7474
7475         if (!xprt)
7476                 nfs4_init_boot_verifier(clp, &verifier);
7477
7478         status = nfs4_init_uniform_client_string(clp);
7479         if (status)
7480                 goto out_calldata;
7481
7482         dprintk("NFS call  exchange_id auth=%s, '%s'\n",
7483                 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7484                 clp->cl_owner_id);
7485
7486         calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7487                                                 GFP_NOFS);
7488         status = -ENOMEM;
7489         if (unlikely(calldata->res.server_owner == NULL))
7490                 goto out_calldata;
7491
7492         calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7493                                         GFP_NOFS);
7494         if (unlikely(calldata->res.server_scope == NULL))
7495                 goto out_server_owner;
7496
7497         calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7498         if (unlikely(calldata->res.impl_id == NULL))
7499                 goto out_server_scope;
7500
7501         switch (sp4_how) {
7502         case SP4_NONE:
7503                 calldata->args.state_protect.how = SP4_NONE;
7504                 break;
7505
7506         case SP4_MACH_CRED:
7507                 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
7508                 break;
7509
7510         default:
7511                 /* unsupported! */
7512                 WARN_ON_ONCE(1);
7513                 status = -EINVAL;
7514                 goto out_impl_id;
7515         }
7516         if (xprt) {
7517                 calldata->xprt = xprt;
7518                 task_setup_data.rpc_xprt = xprt;
7519                 task_setup_data.flags =
7520                                 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7521                 calldata->args.verifier = &clp->cl_confirm;
7522         } else {
7523                 calldata->args.verifier = &verifier;
7524         }
7525         calldata->args.client = clp;
7526 #ifdef CONFIG_NFS_V4_1_MIGRATION
7527         calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7528         EXCHGID4_FLAG_BIND_PRINC_STATEID |
7529         EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7530 #else
7531         calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7532         EXCHGID4_FLAG_BIND_PRINC_STATEID,
7533 #endif
7534         msg.rpc_argp = &calldata->args;
7535         msg.rpc_resp = &calldata->res;
7536         task_setup_data.callback_data = calldata;
7537
7538         task = rpc_run_task(&task_setup_data);
7539         if (IS_ERR(task))
7540                 return PTR_ERR(task);
7541
7542         if (!xprt) {
7543                 status = rpc_wait_for_completion_task(task);
7544                 if (!status)
7545                         status = calldata->rpc_status;
7546         } else  /* session trunking test */
7547                 status = calldata->rpc_status;
7548
7549         rpc_put_task(task);
7550 out:
7551         if (clp->cl_implid != NULL)
7552                 dprintk("NFS reply exchange_id: Server Implementation ID: "
7553                         "domain: %s, name: %s, date: %llu,%u\n",
7554                         clp->cl_implid->domain, clp->cl_implid->name,
7555                         clp->cl_implid->date.seconds,
7556                         clp->cl_implid->date.nseconds);
7557         dprintk("NFS reply exchange_id: %d\n", status);
7558         return status;
7559
7560 out_impl_id:
7561         kfree(calldata->res.impl_id);
7562 out_server_scope:
7563         kfree(calldata->res.server_scope);
7564 out_server_owner:
7565         kfree(calldata->res.server_owner);
7566 out_calldata:
7567         kfree(calldata);
7568         nfs_put_client(clp);
7569         goto out;
7570 }
7571
7572 /*
7573  * nfs4_proc_exchange_id()
7574  *
7575  * Returns zero, a negative errno, or a negative NFS4ERR status code.
7576  *
7577  * Since the clientid has expired, all compounds using sessions
7578  * associated with the stale clientid will be returning
7579  * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7580  * be in some phase of session reset.
7581  *
7582  * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7583  */
7584 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7585 {
7586         rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7587         int status;
7588
7589         /* try SP4_MACH_CRED if krb5i/p */
7590         if (authflavor == RPC_AUTH_GSS_KRB5I ||
7591             authflavor == RPC_AUTH_GSS_KRB5P) {
7592                 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
7593                 if (!status)
7594                         return 0;
7595         }
7596
7597         /* try SP4_NONE */
7598         return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
7599 }
7600
7601 /**
7602  * nfs4_test_session_trunk
7603  *
7604  * This is an add_xprt_test() test function called from
7605  * rpc_clnt_setup_test_and_add_xprt.
7606  *
7607  * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7608  * and is dereferrenced in nfs4_exchange_id_release
7609  *
7610  * Upon success, add the new transport to the rpc_clnt
7611  *
7612  * @clnt: struct rpc_clnt to get new transport
7613  * @xprt: the rpc_xprt to test
7614  * @data: call data for _nfs4_proc_exchange_id.
7615  */
7616 int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7617                             void *data)
7618 {
7619         struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7620         u32 sp4_how;
7621
7622         dprintk("--> %s try %s\n", __func__,
7623                 xprt->address_strings[RPC_DISPLAY_ADDR]);
7624
7625         sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7626
7627         /* Test connection for session trunking. Async exchange_id call */
7628         return  _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7629 }
7630 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7631
7632 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7633                 struct rpc_cred *cred)
7634 {
7635         struct rpc_message msg = {
7636                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7637                 .rpc_argp = clp,
7638                 .rpc_cred = cred,
7639         };
7640         int status;
7641
7642         status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7643         trace_nfs4_destroy_clientid(clp, status);
7644         if (status)
7645                 dprintk("NFS: Got error %d from the server %s on "
7646                         "DESTROY_CLIENTID.", status, clp->cl_hostname);
7647         return status;
7648 }
7649
7650 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7651                 struct rpc_cred *cred)
7652 {
7653         unsigned int loop;
7654         int ret;
7655
7656         for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7657                 ret = _nfs4_proc_destroy_clientid(clp, cred);
7658                 switch (ret) {
7659                 case -NFS4ERR_DELAY:
7660                 case -NFS4ERR_CLIENTID_BUSY:
7661                         ssleep(1);
7662                         break;
7663                 default:
7664                         return ret;
7665                 }
7666         }
7667         return 0;
7668 }
7669
7670 int nfs4_destroy_clientid(struct nfs_client *clp)
7671 {
7672         struct rpc_cred *cred;
7673         int ret = 0;
7674
7675         if (clp->cl_mvops->minor_version < 1)
7676                 goto out;
7677         if (clp->cl_exchange_flags == 0)
7678                 goto out;
7679         if (clp->cl_preserve_clid)
7680                 goto out;
7681         cred = nfs4_get_clid_cred(clp);
7682         ret = nfs4_proc_destroy_clientid(clp, cred);
7683         if (cred)
7684                 put_rpccred(cred);
7685         switch (ret) {
7686         case 0:
7687         case -NFS4ERR_STALE_CLIENTID:
7688                 clp->cl_exchange_flags = 0;
7689         }
7690 out:
7691         return ret;
7692 }
7693
7694 struct nfs4_get_lease_time_data {
7695         struct nfs4_get_lease_time_args *args;
7696         struct nfs4_get_lease_time_res *res;
7697         struct nfs_client *clp;
7698 };
7699
7700 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7701                                         void *calldata)
7702 {
7703         struct nfs4_get_lease_time_data *data =
7704                         (struct nfs4_get_lease_time_data *)calldata;
7705
7706         dprintk("--> %s\n", __func__);
7707         /* just setup sequence, do not trigger session recovery
7708            since we're invoked within one */
7709         nfs4_setup_sequence(data->clp,
7710                         &data->args->la_seq_args,
7711                         &data->res->lr_seq_res,
7712                         task);
7713         dprintk("<-- %s\n", __func__);
7714 }
7715
7716 /*
7717  * Called from nfs4_state_manager thread for session setup, so don't recover
7718  * from sequence operation or clientid errors.
7719  */
7720 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7721 {
7722         struct nfs4_get_lease_time_data *data =
7723                         (struct nfs4_get_lease_time_data *)calldata;
7724
7725         dprintk("--> %s\n", __func__);
7726         if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7727                 return;
7728         switch (task->tk_status) {
7729         case -NFS4ERR_DELAY:
7730         case -NFS4ERR_GRACE:
7731                 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7732                 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7733                 task->tk_status = 0;
7734                 /* fall through */
7735         case -NFS4ERR_RETRY_UNCACHED_REP:
7736                 rpc_restart_call_prepare(task);
7737                 return;
7738         }
7739         dprintk("<-- %s\n", __func__);
7740 }
7741
7742 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7743         .rpc_call_prepare = nfs4_get_lease_time_prepare,
7744         .rpc_call_done = nfs4_get_lease_time_done,
7745 };
7746
7747 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7748 {
7749         struct rpc_task *task;
7750         struct nfs4_get_lease_time_args args;
7751         struct nfs4_get_lease_time_res res = {
7752                 .lr_fsinfo = fsinfo,
7753         };
7754         struct nfs4_get_lease_time_data data = {
7755                 .args = &args,
7756                 .res = &res,
7757                 .clp = clp,
7758         };
7759         struct rpc_message msg = {
7760                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7761                 .rpc_argp = &args,
7762                 .rpc_resp = &res,
7763         };
7764         struct rpc_task_setup task_setup = {
7765                 .rpc_client = clp->cl_rpcclient,
7766                 .rpc_message = &msg,
7767                 .callback_ops = &nfs4_get_lease_time_ops,
7768                 .callback_data = &data,
7769                 .flags = RPC_TASK_TIMEOUT,
7770         };
7771         int status;
7772
7773         nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7774         nfs4_set_sequence_privileged(&args.la_seq_args);
7775         dprintk("--> %s\n", __func__);
7776         task = rpc_run_task(&task_setup);
7777
7778         if (IS_ERR(task))
7779                 status = PTR_ERR(task);
7780         else {
7781                 status = task->tk_status;
7782                 rpc_put_task(task);
7783         }
7784         dprintk("<-- %s return %d\n", __func__, status);
7785
7786         return status;
7787 }
7788
7789 /*
7790  * Initialize the values to be used by the client in CREATE_SESSION
7791  * If nfs4_init_session set the fore channel request and response sizes,
7792  * use them.
7793  *
7794  * Set the back channel max_resp_sz_cached to zero to force the client to
7795  * always set csa_cachethis to FALSE because the current implementation
7796  * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7797  */
7798 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7799                                     struct rpc_clnt *clnt)
7800 {
7801         unsigned int max_rqst_sz, max_resp_sz;
7802         unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
7803
7804         max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7805         max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7806
7807         /* Fore channel attributes */
7808         args->fc_attrs.max_rqst_sz = max_rqst_sz;
7809         args->fc_attrs.max_resp_sz = max_resp_sz;
7810         args->fc_attrs.max_ops = NFS4_MAX_OPS;
7811         args->fc_attrs.max_reqs = max_session_slots;
7812
7813         dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7814                 "max_ops=%u max_reqs=%u\n",
7815                 __func__,
7816                 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7817                 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
7818
7819         /* Back channel attributes */
7820         args->bc_attrs.max_rqst_sz = max_bc_payload;
7821         args->bc_attrs.max_resp_sz = max_bc_payload;
7822         args->bc_attrs.max_resp_sz_cached = 0;
7823         args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7824         args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
7825
7826         dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7827                 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7828                 __func__,
7829                 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7830                 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7831                 args->bc_attrs.max_reqs);
7832 }
7833
7834 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7835                 struct nfs41_create_session_res *res)
7836 {
7837         struct nfs4_channel_attrs *sent = &args->fc_attrs;
7838         struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
7839
7840         if (rcvd->max_resp_sz > sent->max_resp_sz)
7841                 return -EINVAL;
7842         /*
7843          * Our requested max_ops is the minimum we need; we're not
7844          * prepared to break up compounds into smaller pieces than that.
7845          * So, no point even trying to continue if the server won't
7846          * cooperate:
7847          */
7848         if (rcvd->max_ops < sent->max_ops)
7849                 return -EINVAL;
7850         if (rcvd->max_reqs == 0)
7851                 return -EINVAL;
7852         if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7853                 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7854         return 0;
7855 }
7856
7857 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7858                 struct nfs41_create_session_res *res)
7859 {
7860         struct nfs4_channel_attrs *sent = &args->bc_attrs;
7861         struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
7862
7863         if (!(res->flags & SESSION4_BACK_CHAN))
7864                 goto out;
7865         if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7866                 return -EINVAL;
7867         if (rcvd->max_resp_sz < sent->max_resp_sz)
7868                 return -EINVAL;
7869         if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7870                 return -EINVAL;
7871         if (rcvd->max_ops > sent->max_ops)
7872                 return -EINVAL;
7873         if (rcvd->max_reqs > sent->max_reqs)
7874                 return -EINVAL;
7875 out:
7876         return 0;
7877 }
7878
7879 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7880                                      struct nfs41_create_session_res *res)
7881 {
7882         int ret;
7883
7884         ret = nfs4_verify_fore_channel_attrs(args, res);
7885         if (ret)
7886                 return ret;
7887         return nfs4_verify_back_channel_attrs(args, res);
7888 }
7889
7890 static void nfs4_update_session(struct nfs4_session *session,
7891                 struct nfs41_create_session_res *res)
7892 {
7893         nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
7894         /* Mark client id and session as being confirmed */
7895         session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7896         set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
7897         session->flags = res->flags;
7898         memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
7899         if (res->flags & SESSION4_BACK_CHAN)
7900                 memcpy(&session->bc_attrs, &res->bc_attrs,
7901                                 sizeof(session->bc_attrs));
7902 }
7903
7904 static int _nfs4_proc_create_session(struct nfs_client *clp,
7905                 struct rpc_cred *cred)
7906 {
7907         struct nfs4_session *session = clp->cl_session;
7908         struct nfs41_create_session_args args = {
7909                 .client = clp,
7910                 .clientid = clp->cl_clientid,
7911                 .seqid = clp->cl_seqid,
7912                 .cb_program = NFS4_CALLBACK,
7913         };
7914         struct nfs41_create_session_res res;
7915
7916         struct rpc_message msg = {
7917                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7918                 .rpc_argp = &args,
7919                 .rpc_resp = &res,
7920                 .rpc_cred = cred,
7921         };
7922         int status;
7923
7924         nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
7925         args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
7926
7927         status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7928         trace_nfs4_create_session(clp, status);
7929
7930         switch (status) {
7931         case -NFS4ERR_STALE_CLIENTID:
7932         case -NFS4ERR_DELAY:
7933         case -ETIMEDOUT:
7934         case -EACCES:
7935         case -EAGAIN:
7936                 goto out;
7937         };
7938
7939         clp->cl_seqid++;
7940         if (!status) {
7941                 /* Verify the session's negotiated channel_attrs values */
7942                 status = nfs4_verify_channel_attrs(&args, &res);
7943                 /* Increment the clientid slot sequence id */
7944                 if (status)
7945                         goto out;
7946                 nfs4_update_session(session, &res);
7947         }
7948 out:
7949         return status;
7950 }
7951
7952 /*
7953  * Issues a CREATE_SESSION operation to the server.
7954  * It is the responsibility of the caller to verify the session is
7955  * expired before calling this routine.
7956  */
7957 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
7958 {
7959         int status;
7960         unsigned *ptr;
7961         struct nfs4_session *session = clp->cl_session;
7962
7963         dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7964
7965         status = _nfs4_proc_create_session(clp, cred);
7966         if (status)
7967                 goto out;
7968
7969         /* Init or reset the session slot tables */
7970         status = nfs4_setup_session_slot_tables(session);
7971         dprintk("slot table setup returned %d\n", status);
7972         if (status)
7973                 goto out;
7974
7975         ptr = (unsigned *)&session->sess_id.data[0];
7976         dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7977                 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
7978 out:
7979         dprintk("<-- %s\n", __func__);
7980         return status;
7981 }
7982
7983 /*
7984  * Issue the over-the-wire RPC DESTROY_SESSION.
7985  * The caller must serialize access to this routine.
7986  */
7987 int nfs4_proc_destroy_session(struct nfs4_session *session,
7988                 struct rpc_cred *cred)
7989 {
7990         struct rpc_message msg = {
7991                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7992                 .rpc_argp = session,
7993                 .rpc_cred = cred,
7994         };
7995         int status = 0;
7996
7997         dprintk("--> nfs4_proc_destroy_session\n");
7998
7999         /* session is still being setup */
8000         if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8001                 return 0;
8002
8003         status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8004         trace_nfs4_destroy_session(session->clp, status);
8005
8006         if (status)
8007                 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8008                         "Session has been destroyed regardless...\n", status);
8009
8010         dprintk("<-- nfs4_proc_destroy_session\n");
8011         return status;
8012 }
8013
8014 /*
8015  * Renew the cl_session lease.
8016  */
8017 struct nfs4_sequence_data {
8018         struct nfs_client *clp;
8019         struct nfs4_sequence_args args;
8020         struct nfs4_sequence_res res;
8021 };
8022
8023 static void nfs41_sequence_release(void *data)
8024 {
8025         struct nfs4_sequence_data *calldata = data;
8026         struct nfs_client *clp = calldata->clp;
8027
8028         if (atomic_read(&clp->cl_count) > 1)
8029                 nfs4_schedule_state_renewal(clp);
8030         nfs_put_client(clp);
8031         kfree(calldata);
8032 }
8033
8034 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8035 {
8036         switch(task->tk_status) {
8037         case -NFS4ERR_DELAY:
8038                 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8039                 return -EAGAIN;
8040         default:
8041                 nfs4_schedule_lease_recovery(clp);
8042         }
8043         return 0;
8044 }
8045
8046 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8047 {
8048         struct nfs4_sequence_data *calldata = data;
8049         struct nfs_client *clp = calldata->clp;
8050
8051         if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8052                 return;
8053
8054         trace_nfs4_sequence(clp, task->tk_status);
8055         if (task->tk_status < 0) {
8056                 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8057                 if (atomic_read(&clp->cl_count) == 1)
8058                         goto out;
8059
8060                 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8061                         rpc_restart_call_prepare(task);
8062                         return;
8063                 }
8064         }
8065         dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8066 out:
8067         dprintk("<-- %s\n", __func__);
8068 }
8069
8070 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8071 {
8072         struct nfs4_sequence_data *calldata = data;
8073         struct nfs_client *clp = calldata->clp;
8074         struct nfs4_sequence_args *args;
8075         struct nfs4_sequence_res *res;
8076
8077         args = task->tk_msg.rpc_argp;
8078         res = task->tk_msg.rpc_resp;
8079
8080         nfs4_setup_sequence(clp, args, res, task);
8081 }
8082
8083 static const struct rpc_call_ops nfs41_sequence_ops = {
8084         .rpc_call_done = nfs41_sequence_call_done,
8085         .rpc_call_prepare = nfs41_sequence_prepare,
8086         .rpc_release = nfs41_sequence_release,
8087 };
8088
8089 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8090                 struct rpc_cred *cred,
8091                 bool is_privileged)
8092 {
8093         struct nfs4_sequence_data *calldata;
8094         struct rpc_message msg = {
8095                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8096                 .rpc_cred = cred,
8097         };
8098         struct rpc_task_setup task_setup_data = {
8099                 .rpc_client = clp->cl_rpcclient,
8100                 .rpc_message = &msg,
8101                 .callback_ops = &nfs41_sequence_ops,
8102                 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8103         };
8104
8105         if (!atomic_inc_not_zero(&clp->cl_count))
8106                 return ERR_PTR(-EIO);
8107         calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8108         if (calldata == NULL) {
8109                 nfs_put_client(clp);
8110                 return ERR_PTR(-ENOMEM);
8111         }
8112         nfs4_init_sequence(&calldata->args, &calldata->res, 0);
8113         if (is_privileged)
8114                 nfs4_set_sequence_privileged(&calldata->args);
8115         msg.rpc_argp = &calldata->args;
8116         msg.rpc_resp = &calldata->res;
8117         calldata->clp = clp;
8118         task_setup_data.callback_data = calldata;
8119
8120         return rpc_run_task(&task_setup_data);
8121 }
8122
8123 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
8124 {
8125         struct rpc_task *task;
8126         int ret = 0;
8127
8128         if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8129                 return -EAGAIN;
8130         task = _nfs41_proc_sequence(clp, cred, false);
8131         if (IS_ERR(task))
8132                 ret = PTR_ERR(task);
8133         else
8134                 rpc_put_task_async(task);
8135         dprintk("<-- %s status=%d\n", __func__, ret);
8136         return ret;
8137 }
8138
8139 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8140 {
8141         struct rpc_task *task;
8142         int ret;
8143
8144         task = _nfs41_proc_sequence(clp, cred, true);
8145         if (IS_ERR(task)) {
8146                 ret = PTR_ERR(task);
8147                 goto out;
8148         }
8149         ret = rpc_wait_for_completion_task(task);
8150         if (!ret)
8151                 ret = task->tk_status;
8152         rpc_put_task(task);
8153 out:
8154         dprintk("<-- %s status=%d\n", __func__, ret);
8155         return ret;
8156 }
8157
8158 struct nfs4_reclaim_complete_data {
8159         struct nfs_client *clp;
8160         struct nfs41_reclaim_complete_args arg;
8161         struct nfs41_reclaim_complete_res res;
8162 };
8163
8164 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8165 {
8166         struct nfs4_reclaim_complete_data *calldata = data;
8167
8168         nfs4_setup_sequence(calldata->clp,
8169                         &calldata->arg.seq_args,
8170                         &calldata->res.seq_res,
8171                         task);
8172 }
8173
8174 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8175 {
8176         switch(task->tk_status) {
8177         case 0:
8178         case -NFS4ERR_COMPLETE_ALREADY:
8179         case -NFS4ERR_WRONG_CRED: /* What to do here? */
8180                 break;
8181         case -NFS4ERR_DELAY:
8182                 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8183                 /* fall through */
8184         case -NFS4ERR_RETRY_UNCACHED_REP:
8185                 return -EAGAIN;
8186         default:
8187                 nfs4_schedule_lease_recovery(clp);
8188         }
8189         return 0;
8190 }
8191
8192 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8193 {
8194         struct nfs4_reclaim_complete_data *calldata = data;
8195         struct nfs_client *clp = calldata->clp;
8196         struct nfs4_sequence_res *res = &calldata->res.seq_res;
8197
8198         dprintk("--> %s\n", __func__);
8199         if (!nfs41_sequence_done(task, res))
8200                 return;
8201
8202         trace_nfs4_reclaim_complete(clp, task->tk_status);
8203         if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8204                 rpc_restart_call_prepare(task);
8205                 return;
8206         }
8207         dprintk("<-- %s\n", __func__);
8208 }
8209
8210 static void nfs4_free_reclaim_complete_data(void *data)
8211 {
8212         struct nfs4_reclaim_complete_data *calldata = data;
8213
8214         kfree(calldata);
8215 }
8216
8217 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8218         .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8219         .rpc_call_done = nfs4_reclaim_complete_done,
8220         .rpc_release = nfs4_free_reclaim_complete_data,
8221 };
8222
8223 /*
8224  * Issue a global reclaim complete.
8225  */
8226 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8227                 struct rpc_cred *cred)
8228 {
8229         struct nfs4_reclaim_complete_data *calldata;
8230         struct rpc_task *task;
8231         struct rpc_message msg = {
8232                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8233                 .rpc_cred = cred,
8234         };
8235         struct rpc_task_setup task_setup_data = {
8236                 .rpc_client = clp->cl_rpcclient,
8237                 .rpc_message = &msg,
8238                 .callback_ops = &nfs4_reclaim_complete_call_ops,
8239                 .flags = RPC_TASK_ASYNC,
8240         };
8241         int status = -ENOMEM;
8242
8243         dprintk("--> %s\n", __func__);
8244         calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8245         if (calldata == NULL)
8246                 goto out;
8247         calldata->clp = clp;
8248         calldata->arg.one_fs = 0;
8249
8250         nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
8251         nfs4_set_sequence_privileged(&calldata->arg.seq_args);
8252         msg.rpc_argp = &calldata->arg;
8253         msg.rpc_resp = &calldata->res;
8254         task_setup_data.callback_data = calldata;
8255         task = rpc_run_task(&task_setup_data);
8256         if (IS_ERR(task)) {
8257                 status = PTR_ERR(task);
8258                 goto out;
8259         }
8260         status = rpc_wait_for_completion_task(task);
8261         if (status == 0)
8262                 status = task->tk_status;
8263         rpc_put_task(task);
8264         return 0;
8265 out:
8266         dprintk("<-- %s status=%d\n", __func__, status);
8267         return status;
8268 }
8269
8270 static void
8271 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8272 {
8273         struct nfs4_layoutget *lgp = calldata;
8274         struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8275
8276         dprintk("--> %s\n", __func__);
8277         nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
8278                                 &lgp->res.seq_res, task);
8279         dprintk("<-- %s\n", __func__);
8280 }
8281
8282 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8283 {
8284         struct nfs4_layoutget *lgp = calldata;
8285
8286         dprintk("--> %s\n", __func__);
8287         nfs41_sequence_process(task, &lgp->res.seq_res);
8288         dprintk("<-- %s\n", __func__);
8289 }
8290
8291 static int
8292 nfs4_layoutget_handle_exception(struct rpc_task *task,
8293                 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8294 {
8295         struct inode *inode = lgp->args.inode;
8296         struct nfs_server *server = NFS_SERVER(inode);
8297         struct pnfs_layout_hdr *lo;
8298         int nfs4err = task->tk_status;
8299         int err, status = 0;
8300         LIST_HEAD(head);
8301
8302         dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8303
8304         switch (nfs4err) {
8305         case 0:
8306                 goto out;
8307
8308         /*
8309          * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8310          * on the file. set tk_status to -ENODATA to tell upper layer to
8311          * retry go inband.
8312          */
8313         case -NFS4ERR_LAYOUTUNAVAILABLE:
8314                 status = -ENODATA;
8315                 goto out;
8316         /*
8317          * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8318          * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8319          */
8320         case -NFS4ERR_BADLAYOUT:
8321                 status = -EOVERFLOW;
8322                 goto out;
8323         /*
8324          * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8325          * (or clients) writing to the same RAID stripe except when
8326          * the minlength argument is 0 (see RFC5661 section 18.43.3).
8327          *
8328          * Treat it like we would RECALLCONFLICT -- we retry for a little
8329          * while, and then eventually give up.
8330          */
8331         case -NFS4ERR_LAYOUTTRYLATER:
8332                 if (lgp->args.minlength == 0) {
8333                         status = -EOVERFLOW;
8334                         goto out;
8335                 }
8336                 status = -EBUSY;
8337                 break;
8338         case -NFS4ERR_RECALLCONFLICT:
8339                 status = -ERECALLCONFLICT;
8340                 break;
8341         case -NFS4ERR_DELEG_REVOKED:
8342         case -NFS4ERR_ADMIN_REVOKED:
8343         case -NFS4ERR_EXPIRED:
8344         case -NFS4ERR_BAD_STATEID:
8345                 exception->timeout = 0;
8346                 spin_lock(&inode->i_lock);
8347                 lo = NFS_I(inode)->layout;
8348                 /* If the open stateid was bad, then recover it. */
8349                 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8350                     nfs4_stateid_match_other(&lgp->args.stateid,
8351                                         &lgp->args.ctx->state->stateid)) {
8352                         spin_unlock(&inode->i_lock);
8353                         exception->state = lgp->args.ctx->state;
8354                         exception->stateid = &lgp->args.stateid;
8355                         break;
8356                 }
8357
8358                 /*
8359                  * Mark the bad layout state as invalid, then retry
8360                  */
8361                 pnfs_mark_layout_stateid_invalid(lo, &head);
8362                 spin_unlock(&inode->i_lock);
8363                 pnfs_free_lseg_list(&head);
8364                 status = -EAGAIN;
8365                 goto out;
8366         }
8367
8368         nfs4_sequence_free_slot(&lgp->res.seq_res);
8369         err = nfs4_handle_exception(server, nfs4err, exception);
8370         if (!status) {
8371                 if (exception->retry)
8372                         status = -EAGAIN;
8373                 else
8374                         status = err;
8375         }
8376 out:
8377         dprintk("<-- %s\n", __func__);
8378         return status;
8379 }
8380
8381 static size_t max_response_pages(struct nfs_server *server)
8382 {
8383         u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8384         return nfs_page_array_len(0, max_resp_sz);
8385 }
8386
8387 static void nfs4_free_pages(struct page **pages, size_t size)
8388 {
8389         int i;
8390
8391         if (!pages)
8392                 return;
8393
8394         for (i = 0; i < size; i++) {
8395                 if (!pages[i])
8396                         break;
8397                 __free_page(pages[i]);
8398         }
8399         kfree(pages);
8400 }
8401
8402 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8403 {
8404         struct page **pages;
8405         int i;
8406
8407         pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8408         if (!pages) {
8409                 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8410                 return NULL;
8411         }
8412
8413         for (i = 0; i < size; i++) {
8414                 pages[i] = alloc_page(gfp_flags);
8415                 if (!pages[i]) {
8416                         dprintk("%s: failed to allocate page\n", __func__);
8417                         nfs4_free_pages(pages, size);
8418                         return NULL;
8419                 }
8420         }
8421
8422         return pages;
8423 }
8424
8425 static void nfs4_layoutget_release(void *calldata)
8426 {
8427         struct nfs4_layoutget *lgp = calldata;
8428         struct inode *inode = lgp->args.inode;
8429         struct nfs_server *server = NFS_SERVER(inode);
8430         size_t max_pages = max_response_pages(server);
8431
8432         dprintk("--> %s\n", __func__);
8433         nfs4_free_pages(lgp->args.layout.pages, max_pages);
8434         pnfs_put_layout_hdr(NFS_I(inode)->layout);
8435         put_nfs_open_context(lgp->args.ctx);
8436         kfree(calldata);
8437         dprintk("<-- %s\n", __func__);
8438 }
8439
8440 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8441         .rpc_call_prepare = nfs4_layoutget_prepare,
8442         .rpc_call_done = nfs4_layoutget_done,
8443         .rpc_release = nfs4_layoutget_release,
8444 };
8445
8446 struct pnfs_layout_segment *
8447 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
8448 {
8449         struct inode *inode = lgp->args.inode;
8450         struct nfs_server *server = NFS_SERVER(inode);
8451         size_t max_pages = max_response_pages(server);
8452         struct rpc_task *task;
8453         struct rpc_message msg = {
8454                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8455                 .rpc_argp = &lgp->args,
8456                 .rpc_resp = &lgp->res,
8457                 .rpc_cred = lgp->cred,
8458         };
8459         struct rpc_task_setup task_setup_data = {
8460                 .rpc_client = server->client,
8461                 .rpc_message = &msg,
8462                 .callback_ops = &nfs4_layoutget_call_ops,
8463                 .callback_data = lgp,
8464                 .flags = RPC_TASK_ASYNC,
8465         };
8466         struct pnfs_layout_segment *lseg = NULL;
8467         struct nfs4_exception exception = {
8468                 .inode = inode,
8469                 .timeout = *timeout,
8470         };
8471         int status = 0;
8472
8473         dprintk("--> %s\n", __func__);
8474
8475         /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8476         pnfs_get_layout_hdr(NFS_I(inode)->layout);
8477
8478         lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8479         if (!lgp->args.layout.pages) {
8480                 nfs4_layoutget_release(lgp);
8481                 return ERR_PTR(-ENOMEM);
8482         }
8483         lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8484
8485         lgp->res.layoutp = &lgp->args.layout;
8486         lgp->res.seq_res.sr_slot = NULL;
8487         nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
8488
8489         task = rpc_run_task(&task_setup_data);
8490         if (IS_ERR(task))
8491                 return ERR_CAST(task);
8492         status = rpc_wait_for_completion_task(task);
8493         if (status == 0) {
8494                 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8495                 *timeout = exception.timeout;
8496         }
8497
8498         trace_nfs4_layoutget(lgp->args.ctx,
8499                         &lgp->args.range,
8500                         &lgp->res.range,
8501                         &lgp->res.stateid,
8502                         status);
8503
8504         /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8505         if (status == 0 && lgp->res.layoutp->len)
8506                 lseg = pnfs_layout_process(lgp);
8507         nfs4_sequence_free_slot(&lgp->res.seq_res);
8508         rpc_put_task(task);
8509         dprintk("<-- %s status=%d\n", __func__, status);
8510         if (status)
8511                 return ERR_PTR(status);
8512         return lseg;
8513 }
8514
8515 static void
8516 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8517 {
8518         struct nfs4_layoutreturn *lrp = calldata;
8519
8520         dprintk("--> %s\n", __func__);
8521         nfs4_setup_sequence(lrp->clp,
8522                         &lrp->args.seq_args,
8523                         &lrp->res.seq_res,
8524                         task);
8525 }
8526
8527 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8528 {
8529         struct nfs4_layoutreturn *lrp = calldata;
8530         struct nfs_server *server;
8531
8532         dprintk("--> %s\n", __func__);
8533
8534         if (!nfs41_sequence_process(task, &lrp->res.seq_res))
8535                 return;
8536
8537         server = NFS_SERVER(lrp->args.inode);
8538         switch (task->tk_status) {
8539         default:
8540                 task->tk_status = 0;
8541         case 0:
8542                 break;
8543         case -NFS4ERR_DELAY:
8544                 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8545                         break;
8546                 nfs4_sequence_free_slot(&lrp->res.seq_res);
8547                 rpc_restart_call_prepare(task);
8548                 return;
8549         }
8550         dprintk("<-- %s\n", __func__);
8551 }
8552
8553 static void nfs4_layoutreturn_release(void *calldata)
8554 {
8555         struct nfs4_layoutreturn *lrp = calldata;
8556         struct pnfs_layout_hdr *lo = lrp->args.layout;
8557
8558         dprintk("--> %s\n", __func__);
8559         pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
8560                         lrp->res.lrs_present ? &lrp->res.stateid : NULL);
8561         nfs4_sequence_free_slot(&lrp->res.seq_res);
8562         if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8563                 lrp->ld_private.ops->free(&lrp->ld_private);
8564         pnfs_put_layout_hdr(lrp->args.layout);
8565         nfs_iput_and_deactive(lrp->inode);
8566         kfree(calldata);
8567         dprintk("<-- %s\n", __func__);
8568 }
8569
8570 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8571         .rpc_call_prepare = nfs4_layoutreturn_prepare,
8572         .rpc_call_done = nfs4_layoutreturn_done,
8573         .rpc_release = nfs4_layoutreturn_release,
8574 };
8575
8576 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
8577 {
8578         struct rpc_task *task;
8579         struct rpc_message msg = {
8580                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8581                 .rpc_argp = &lrp->args,
8582                 .rpc_resp = &lrp->res,
8583                 .rpc_cred = lrp->cred,
8584         };
8585         struct rpc_task_setup task_setup_data = {
8586                 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8587                 .rpc_message = &msg,
8588                 .callback_ops = &nfs4_layoutreturn_call_ops,
8589                 .callback_data = lrp,
8590         };
8591         int status = 0;
8592
8593         nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8594                         NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8595                         &task_setup_data.rpc_client, &msg);
8596
8597         dprintk("--> %s\n", __func__);
8598         if (!sync) {
8599                 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8600                 if (!lrp->inode) {
8601                         nfs4_layoutreturn_release(lrp);
8602                         return -EAGAIN;
8603                 }
8604                 task_setup_data.flags |= RPC_TASK_ASYNC;
8605         }
8606         nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
8607         task = rpc_run_task(&task_setup_data);
8608         if (IS_ERR(task))
8609                 return PTR_ERR(task);
8610         if (sync)
8611                 status = task->tk_status;
8612         trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
8613         dprintk("<-- %s status=%d\n", __func__, status);
8614         rpc_put_task(task);
8615         return status;
8616 }
8617
8618 static int
8619 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8620                 struct pnfs_device *pdev,
8621                 struct rpc_cred *cred)
8622 {
8623         struct nfs4_getdeviceinfo_args args = {
8624                 .pdev = pdev,
8625                 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8626                         NOTIFY_DEVICEID4_DELETE,
8627         };
8628         struct nfs4_getdeviceinfo_res res = {
8629                 .pdev = pdev,
8630         };
8631         struct rpc_message msg = {
8632                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8633                 .rpc_argp = &args,
8634                 .rpc_resp = &res,
8635                 .rpc_cred = cred,
8636         };
8637         int status;
8638
8639         dprintk("--> %s\n", __func__);
8640         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8641         if (res.notification & ~args.notify_types)
8642                 dprintk("%s: unsupported notification\n", __func__);
8643         if (res.notification != args.notify_types)
8644                 pdev->nocache = 1;
8645
8646         dprintk("<-- %s status=%d\n", __func__, status);
8647
8648         return status;
8649 }
8650
8651 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8652                 struct pnfs_device *pdev,
8653                 struct rpc_cred *cred)
8654 {
8655         struct nfs4_exception exception = { };
8656         int err;
8657
8658         do {
8659                 err = nfs4_handle_exception(server,
8660                                         _nfs4_proc_getdeviceinfo(server, pdev, cred),
8661                                         &exception);
8662         } while (exception.retry);
8663         return err;
8664 }
8665 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8666
8667 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8668 {
8669         struct nfs4_layoutcommit_data *data = calldata;
8670         struct nfs_server *server = NFS_SERVER(data->args.inode);
8671
8672         nfs4_setup_sequence(server->nfs_client,
8673                         &data->args.seq_args,
8674                         &data->res.seq_res,
8675                         task);
8676 }
8677
8678 static void
8679 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8680 {
8681         struct nfs4_layoutcommit_data *data = calldata;
8682         struct nfs_server *server = NFS_SERVER(data->args.inode);
8683
8684         if (!nfs41_sequence_done(task, &data->res.seq_res))
8685                 return;
8686
8687         switch (task->tk_status) { /* Just ignore these failures */
8688         case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8689         case -NFS4ERR_BADIOMODE:     /* no IOMODE_RW layout for range */
8690         case -NFS4ERR_BADLAYOUT:     /* no layout */
8691         case -NFS4ERR_GRACE:        /* loca_recalim always false */
8692                 task->tk_status = 0;
8693         case 0:
8694                 break;
8695         default:
8696                 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8697                         rpc_restart_call_prepare(task);
8698                         return;
8699                 }
8700         }
8701 }
8702
8703 static void nfs4_layoutcommit_release(void *calldata)
8704 {
8705         struct nfs4_layoutcommit_data *data = calldata;
8706
8707         pnfs_cleanup_layoutcommit(data);
8708         nfs_post_op_update_inode_force_wcc(data->args.inode,
8709                                            data->res.fattr);
8710         put_rpccred(data->cred);
8711         nfs_iput_and_deactive(data->inode);
8712         kfree(data);
8713 }
8714
8715 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8716         .rpc_call_prepare = nfs4_layoutcommit_prepare,
8717         .rpc_call_done = nfs4_layoutcommit_done,
8718         .rpc_release = nfs4_layoutcommit_release,
8719 };
8720
8721 int
8722 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8723 {
8724         struct rpc_message msg = {
8725                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8726                 .rpc_argp = &data->args,
8727                 .rpc_resp = &data->res,
8728                 .rpc_cred = data->cred,
8729         };
8730         struct rpc_task_setup task_setup_data = {
8731                 .task = &data->task,
8732                 .rpc_client = NFS_CLIENT(data->args.inode),
8733                 .rpc_message = &msg,
8734                 .callback_ops = &nfs4_layoutcommit_ops,
8735                 .callback_data = data,
8736         };
8737         struct rpc_task *task;
8738         int status = 0;
8739
8740         dprintk("NFS: initiating layoutcommit call. sync %d "
8741                 "lbw: %llu inode %lu\n", sync,
8742                 data->args.lastbytewritten,
8743                 data->args.inode->i_ino);
8744
8745         if (!sync) {
8746                 data->inode = nfs_igrab_and_active(data->args.inode);
8747                 if (data->inode == NULL) {
8748                         nfs4_layoutcommit_release(data);
8749                         return -EAGAIN;
8750                 }
8751                 task_setup_data.flags = RPC_TASK_ASYNC;
8752         }
8753         nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8754         task = rpc_run_task(&task_setup_data);
8755         if (IS_ERR(task))
8756                 return PTR_ERR(task);
8757         if (sync)
8758                 status = task->tk_status;
8759         trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
8760         dprintk("%s: status %d\n", __func__, status);
8761         rpc_put_task(task);
8762         return status;
8763 }
8764
8765 /**
8766  * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8767  * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8768  */
8769 static int
8770 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8771                     struct nfs_fsinfo *info,
8772                     struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8773 {
8774         struct nfs41_secinfo_no_name_args args = {
8775                 .style = SECINFO_STYLE_CURRENT_FH,
8776         };
8777         struct nfs4_secinfo_res res = {
8778                 .flavors = flavors,
8779         };
8780         struct rpc_message msg = {
8781                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8782                 .rpc_argp = &args,
8783                 .rpc_resp = &res,
8784         };
8785         struct rpc_clnt *clnt = server->client;
8786         struct rpc_cred *cred = NULL;
8787         int status;
8788
8789         if (use_integrity) {
8790                 clnt = server->nfs_client->cl_rpcclient;
8791                 cred = nfs4_get_clid_cred(server->nfs_client);
8792                 msg.rpc_cred = cred;
8793         }
8794
8795         dprintk("--> %s\n", __func__);
8796         status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8797                                 &res.seq_res, 0);
8798         dprintk("<-- %s status=%d\n", __func__, status);
8799
8800         if (cred)
8801                 put_rpccred(cred);
8802
8803         return status;
8804 }
8805
8806 static int
8807 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8808                            struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8809 {
8810         struct nfs4_exception exception = { };
8811         int err;
8812         do {
8813                 /* first try using integrity protection */
8814                 err = -NFS4ERR_WRONGSEC;
8815
8816                 /* try to use integrity protection with machine cred */
8817                 if (_nfs4_is_integrity_protected(server->nfs_client))
8818                         err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8819                                                           flavors, true);
8820
8821                 /*
8822                  * if unable to use integrity protection, or SECINFO with
8823                  * integrity protection returns NFS4ERR_WRONGSEC (which is
8824                  * disallowed by spec, but exists in deployed servers) use
8825                  * the current filesystem's rpc_client and the user cred.
8826                  */
8827                 if (err == -NFS4ERR_WRONGSEC)
8828                         err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8829                                                           flavors, false);
8830
8831                 switch (err) {
8832                 case 0:
8833                 case -NFS4ERR_WRONGSEC:
8834                 case -ENOTSUPP:
8835                         goto out;
8836                 default:
8837                         err = nfs4_handle_exception(server, err, &exception);
8838                 }
8839         } while (exception.retry);
8840 out:
8841         return err;
8842 }
8843
8844 static int
8845 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8846                     struct nfs_fsinfo *info)
8847 {
8848         int err;
8849         struct page *page;
8850         rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
8851         struct nfs4_secinfo_flavors *flavors;
8852         struct nfs4_secinfo4 *secinfo;
8853         int i;
8854
8855         page = alloc_page(GFP_KERNEL);
8856         if (!page) {
8857                 err = -ENOMEM;
8858                 goto out;
8859         }
8860
8861         flavors = page_address(page);
8862         err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8863
8864         /*
8865          * Fall back on "guess and check" method if
8866          * the server doesn't support SECINFO_NO_NAME
8867          */
8868         if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
8869                 err = nfs4_find_root_sec(server, fhandle, info);
8870                 goto out_freepage;
8871         }
8872         if (err)
8873                 goto out_freepage;
8874
8875         for (i = 0; i < flavors->num_flavors; i++) {
8876                 secinfo = &flavors->flavors[i];
8877
8878                 switch (secinfo->flavor) {
8879                 case RPC_AUTH_NULL:
8880                 case RPC_AUTH_UNIX:
8881                 case RPC_AUTH_GSS:
8882                         flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8883                                         &secinfo->flavor_info);
8884                         break;
8885                 default:
8886                         flavor = RPC_AUTH_MAXFLAVOR;
8887                         break;
8888                 }
8889
8890                 if (!nfs_auth_info_match(&server->auth_info, flavor))
8891                         flavor = RPC_AUTH_MAXFLAVOR;
8892
8893                 if (flavor != RPC_AUTH_MAXFLAVOR) {
8894                         err = nfs4_lookup_root_sec(server, fhandle,
8895                                                    info, flavor);
8896                         if (!err)
8897                                 break;
8898                 }
8899         }
8900
8901         if (flavor == RPC_AUTH_MAXFLAVOR)
8902                 err = -EPERM;
8903
8904 out_freepage:
8905         put_page(page);
8906         if (err == -EACCES)
8907                 return -EPERM;
8908 out:
8909         return err;
8910 }
8911
8912 static int _nfs41_test_stateid(struct nfs_server *server,
8913                 nfs4_stateid *stateid,
8914                 struct rpc_cred *cred)
8915 {
8916         int status;
8917         struct nfs41_test_stateid_args args = {
8918                 .stateid = stateid,
8919         };
8920         struct nfs41_test_stateid_res res;
8921         struct rpc_message msg = {
8922                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8923                 .rpc_argp = &args,
8924                 .rpc_resp = &res,
8925                 .rpc_cred = cred,
8926         };
8927         struct rpc_clnt *rpc_client = server->client;
8928
8929         nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8930                 &rpc_client, &msg);
8931
8932         dprintk("NFS call  test_stateid %p\n", stateid);
8933         nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8934         nfs4_set_sequence_privileged(&args.seq_args);
8935         status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8936                         &args.seq_args, &res.seq_res);
8937         if (status != NFS_OK) {
8938                 dprintk("NFS reply test_stateid: failed, %d\n", status);
8939                 return status;
8940         }
8941         dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
8942         return -res.status;
8943 }
8944
8945 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8946                 int err, struct nfs4_exception *exception)
8947 {
8948         exception->retry = 0;
8949         switch(err) {
8950         case -NFS4ERR_DELAY:
8951         case -NFS4ERR_RETRY_UNCACHED_REP:
8952                 nfs4_handle_exception(server, err, exception);
8953                 break;
8954         case -NFS4ERR_BADSESSION:
8955         case -NFS4ERR_BADSLOT:
8956         case -NFS4ERR_BAD_HIGH_SLOT:
8957         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8958         case -NFS4ERR_DEADSESSION:
8959                 nfs4_do_handle_exception(server, err, exception);
8960         }
8961 }
8962
8963 /**
8964  * nfs41_test_stateid - perform a TEST_STATEID operation
8965  *
8966  * @server: server / transport on which to perform the operation
8967  * @stateid: state ID to test
8968  * @cred: credential
8969  *
8970  * Returns NFS_OK if the server recognizes that "stateid" is valid.
8971  * Otherwise a negative NFS4ERR value is returned if the operation
8972  * failed or the state ID is not currently valid.
8973  */
8974 static int nfs41_test_stateid(struct nfs_server *server,
8975                 nfs4_stateid *stateid,
8976                 struct rpc_cred *cred)
8977 {
8978         struct nfs4_exception exception = { };
8979         int err;
8980         do {
8981                 err = _nfs41_test_stateid(server, stateid, cred);
8982                 nfs4_handle_delay_or_session_error(server, err, &exception);
8983         } while (exception.retry);
8984         return err;
8985 }
8986
8987 struct nfs_free_stateid_data {
8988         struct nfs_server *server;
8989         struct nfs41_free_stateid_args args;
8990         struct nfs41_free_stateid_res res;
8991 };
8992
8993 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8994 {
8995         struct nfs_free_stateid_data *data = calldata;
8996         nfs4_setup_sequence(data->server->nfs_client,
8997                         &data->args.seq_args,
8998                         &data->res.seq_res,
8999                         task);
9000 }
9001
9002 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9003 {
9004         struct nfs_free_stateid_data *data = calldata;
9005
9006         nfs41_sequence_done(task, &data->res.seq_res);
9007
9008         switch (task->tk_status) {
9009         case -NFS4ERR_DELAY:
9010                 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9011                         rpc_restart_call_prepare(task);
9012         }
9013 }
9014
9015 static void nfs41_free_stateid_release(void *calldata)
9016 {
9017         kfree(calldata);
9018 }
9019
9020 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9021         .rpc_call_prepare = nfs41_free_stateid_prepare,
9022         .rpc_call_done = nfs41_free_stateid_done,
9023         .rpc_release = nfs41_free_stateid_release,
9024 };
9025
9026 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
9027                 const nfs4_stateid *stateid,
9028                 struct rpc_cred *cred,
9029                 bool privileged)
9030 {
9031         struct rpc_message msg = {
9032                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9033                 .rpc_cred = cred,
9034         };
9035         struct rpc_task_setup task_setup = {
9036                 .rpc_client = server->client,
9037                 .rpc_message = &msg,
9038                 .callback_ops = &nfs41_free_stateid_ops,
9039                 .flags = RPC_TASK_ASYNC,
9040         };
9041         struct nfs_free_stateid_data *data;
9042
9043         nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9044                 &task_setup.rpc_client, &msg);
9045
9046         dprintk("NFS call  free_stateid %p\n", stateid);
9047         data = kmalloc(sizeof(*data), GFP_NOFS);
9048         if (!data)
9049                 return ERR_PTR(-ENOMEM);
9050         data->server = server;
9051         nfs4_stateid_copy(&data->args.stateid, stateid);
9052
9053         task_setup.callback_data = data;
9054
9055         msg.rpc_argp = &data->args;
9056         msg.rpc_resp = &data->res;
9057         nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
9058         if (privileged)
9059                 nfs4_set_sequence_privileged(&data->args.seq_args);
9060
9061         return rpc_run_task(&task_setup);
9062 }
9063
9064 /**
9065  * nfs41_free_stateid - perform a FREE_STATEID operation
9066  *
9067  * @server: server / transport on which to perform the operation
9068  * @stateid: state ID to release
9069  * @cred: credential
9070  * @is_recovery: set to true if this call needs to be privileged
9071  *
9072  * Note: this function is always asynchronous.
9073  */
9074 static int nfs41_free_stateid(struct nfs_server *server,
9075                 const nfs4_stateid *stateid,
9076                 struct rpc_cred *cred,
9077                 bool is_recovery)
9078 {
9079         struct rpc_task *task;
9080
9081         task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
9082         if (IS_ERR(task))
9083                 return PTR_ERR(task);
9084         rpc_put_task(task);
9085         return 0;
9086 }
9087
9088 static void
9089 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9090 {
9091         struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
9092
9093         nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9094         nfs4_free_lock_state(server, lsp);
9095 }
9096
9097 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9098                 const nfs4_stateid *s2)
9099 {
9100         if (s1->type != s2->type)
9101                 return false;
9102
9103         if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9104                 return false;
9105
9106         if (s1->seqid == s2->seqid)
9107                 return true;
9108
9109         return s1->seqid == 0 || s2->seqid == 0;
9110 }
9111
9112 #endif /* CONFIG_NFS_V4_1 */
9113
9114 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9115                 const nfs4_stateid *s2)
9116 {
9117         return nfs4_stateid_match(s1, s2);
9118 }
9119
9120
9121 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9122         .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9123         .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9124         .recover_open   = nfs4_open_reclaim,
9125         .recover_lock   = nfs4_lock_reclaim,
9126         .establish_clid = nfs4_init_clientid,
9127         .detect_trunking = nfs40_discover_server_trunking,
9128 };
9129
9130 #if defined(CONFIG_NFS_V4_1)
9131 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9132         .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9133         .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9134         .recover_open   = nfs4_open_reclaim,
9135         .recover_lock   = nfs4_lock_reclaim,
9136         .establish_clid = nfs41_init_clientid,
9137         .reclaim_complete = nfs41_proc_reclaim_complete,
9138         .detect_trunking = nfs41_discover_server_trunking,
9139 };
9140 #endif /* CONFIG_NFS_V4_1 */
9141
9142 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9143         .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9144         .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9145         .recover_open   = nfs40_open_expired,
9146         .recover_lock   = nfs4_lock_expired,
9147         .establish_clid = nfs4_init_clientid,
9148 };
9149
9150 #if defined(CONFIG_NFS_V4_1)
9151 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9152         .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9153         .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9154         .recover_open   = nfs41_open_expired,
9155         .recover_lock   = nfs41_lock_expired,
9156         .establish_clid = nfs41_init_clientid,
9157 };
9158 #endif /* CONFIG_NFS_V4_1 */
9159
9160 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9161         .sched_state_renewal = nfs4_proc_async_renew,
9162         .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
9163         .renew_lease = nfs4_proc_renew,
9164 };
9165
9166 #if defined(CONFIG_NFS_V4_1)
9167 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9168         .sched_state_renewal = nfs41_proc_async_sequence,
9169         .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
9170         .renew_lease = nfs4_proc_sequence,
9171 };
9172 #endif
9173
9174 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9175         .get_locations = _nfs40_proc_get_locations,
9176         .fsid_present = _nfs40_proc_fsid_present,
9177 };
9178
9179 #if defined(CONFIG_NFS_V4_1)
9180 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9181         .get_locations = _nfs41_proc_get_locations,
9182         .fsid_present = _nfs41_proc_fsid_present,
9183 };
9184 #endif  /* CONFIG_NFS_V4_1 */
9185
9186 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9187         .minor_version = 0,
9188         .init_caps = NFS_CAP_READDIRPLUS
9189                 | NFS_CAP_ATOMIC_OPEN
9190                 | NFS_CAP_POSIX_LOCK,
9191         .init_client = nfs40_init_client,
9192         .shutdown_client = nfs40_shutdown_client,
9193         .match_stateid = nfs4_match_stateid,
9194         .find_root_sec = nfs4_find_root_sec,
9195         .free_lock_state = nfs4_release_lockowner,
9196         .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9197         .alloc_seqid = nfs_alloc_seqid,
9198         .call_sync_ops = &nfs40_call_sync_ops,
9199         .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9200         .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9201         .state_renewal_ops = &nfs40_state_renewal_ops,
9202         .mig_recovery_ops = &nfs40_mig_recovery_ops,
9203 };
9204
9205 #if defined(CONFIG_NFS_V4_1)
9206 static struct nfs_seqid *
9207 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9208 {
9209         return NULL;
9210 }
9211
9212 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9213         .minor_version = 1,
9214         .init_caps = NFS_CAP_READDIRPLUS
9215                 | NFS_CAP_ATOMIC_OPEN
9216                 | NFS_CAP_POSIX_LOCK
9217                 | NFS_CAP_STATEID_NFSV41
9218                 | NFS_CAP_ATOMIC_OPEN_V1,
9219         .init_client = nfs41_init_client,
9220         .shutdown_client = nfs41_shutdown_client,
9221         .match_stateid = nfs41_match_stateid,
9222         .find_root_sec = nfs41_find_root_sec,
9223         .free_lock_state = nfs41_free_lock_state,
9224         .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9225         .alloc_seqid = nfs_alloc_no_seqid,
9226         .session_trunk = nfs4_test_session_trunk,
9227         .call_sync_ops = &nfs41_call_sync_ops,
9228         .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9229         .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9230         .state_renewal_ops = &nfs41_state_renewal_ops,
9231         .mig_recovery_ops = &nfs41_mig_recovery_ops,
9232 };
9233 #endif
9234
9235 #if defined(CONFIG_NFS_V4_2)
9236 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9237         .minor_version = 2,
9238         .init_caps = NFS_CAP_READDIRPLUS
9239                 | NFS_CAP_ATOMIC_OPEN
9240                 | NFS_CAP_POSIX_LOCK
9241                 | NFS_CAP_STATEID_NFSV41
9242                 | NFS_CAP_ATOMIC_OPEN_V1
9243                 | NFS_CAP_ALLOCATE
9244                 | NFS_CAP_COPY
9245                 | NFS_CAP_DEALLOCATE
9246                 | NFS_CAP_SEEK
9247                 | NFS_CAP_LAYOUTSTATS
9248                 | NFS_CAP_CLONE,
9249         .init_client = nfs41_init_client,
9250         .shutdown_client = nfs41_shutdown_client,
9251         .match_stateid = nfs41_match_stateid,
9252         .find_root_sec = nfs41_find_root_sec,
9253         .free_lock_state = nfs41_free_lock_state,
9254         .call_sync_ops = &nfs41_call_sync_ops,
9255         .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9256         .alloc_seqid = nfs_alloc_no_seqid,
9257         .session_trunk = nfs4_test_session_trunk,
9258         .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9259         .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9260         .state_renewal_ops = &nfs41_state_renewal_ops,
9261         .mig_recovery_ops = &nfs41_mig_recovery_ops,
9262 };
9263 #endif
9264
9265 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9266         [0] = &nfs_v4_0_minor_ops,
9267 #if defined(CONFIG_NFS_V4_1)
9268         [1] = &nfs_v4_1_minor_ops,
9269 #endif
9270 #if defined(CONFIG_NFS_V4_2)
9271         [2] = &nfs_v4_2_minor_ops,
9272 #endif
9273 };
9274
9275 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9276 {
9277         ssize_t error, error2;
9278
9279         error = generic_listxattr(dentry, list, size);
9280         if (error < 0)
9281                 return error;
9282         if (list) {
9283                 list += error;
9284                 size -= error;
9285         }
9286
9287         error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9288         if (error2 < 0)
9289                 return error2;
9290         return error + error2;
9291 }
9292
9293 static const struct inode_operations nfs4_dir_inode_operations = {
9294         .create         = nfs_create,
9295         .lookup         = nfs_lookup,
9296         .atomic_open    = nfs_atomic_open,
9297         .link           = nfs_link,
9298         .unlink         = nfs_unlink,
9299         .symlink        = nfs_symlink,
9300         .mkdir          = nfs_mkdir,
9301         .rmdir          = nfs_rmdir,
9302         .mknod          = nfs_mknod,
9303         .rename         = nfs_rename,
9304         .permission     = nfs_permission,
9305         .getattr        = nfs_getattr,
9306         .setattr        = nfs_setattr,
9307         .listxattr      = nfs4_listxattr,
9308 };
9309
9310 static const struct inode_operations nfs4_file_inode_operations = {
9311         .permission     = nfs_permission,
9312         .getattr        = nfs_getattr,
9313         .setattr        = nfs_setattr,
9314         .listxattr      = nfs4_listxattr,
9315 };
9316
9317 const struct nfs_rpc_ops nfs_v4_clientops = {
9318         .version        = 4,                    /* protocol version */
9319         .dentry_ops     = &nfs4_dentry_operations,
9320         .dir_inode_ops  = &nfs4_dir_inode_operations,
9321         .file_inode_ops = &nfs4_file_inode_operations,
9322         .file_ops       = &nfs4_file_operations,
9323         .getroot        = nfs4_proc_get_root,
9324         .submount       = nfs4_submount,
9325         .try_mount      = nfs4_try_mount,
9326         .getattr        = nfs4_proc_getattr,
9327         .setattr        = nfs4_proc_setattr,
9328         .lookup         = nfs4_proc_lookup,
9329         .access         = nfs4_proc_access,
9330         .readlink       = nfs4_proc_readlink,
9331         .create         = nfs4_proc_create,
9332         .remove         = nfs4_proc_remove,
9333         .unlink_setup   = nfs4_proc_unlink_setup,
9334         .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9335         .unlink_done    = nfs4_proc_unlink_done,
9336         .rename_setup   = nfs4_proc_rename_setup,
9337         .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9338         .rename_done    = nfs4_proc_rename_done,
9339         .link           = nfs4_proc_link,
9340         .symlink        = nfs4_proc_symlink,
9341         .mkdir          = nfs4_proc_mkdir,
9342         .rmdir          = nfs4_proc_remove,
9343         .readdir        = nfs4_proc_readdir,
9344         .mknod          = nfs4_proc_mknod,
9345         .statfs         = nfs4_proc_statfs,
9346         .fsinfo         = nfs4_proc_fsinfo,
9347         .pathconf       = nfs4_proc_pathconf,
9348         .set_capabilities = nfs4_server_capabilities,
9349         .decode_dirent  = nfs4_decode_dirent,
9350         .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9351         .read_setup     = nfs4_proc_read_setup,
9352         .read_done      = nfs4_read_done,
9353         .write_setup    = nfs4_proc_write_setup,
9354         .write_done     = nfs4_write_done,
9355         .commit_setup   = nfs4_proc_commit_setup,
9356         .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9357         .commit_done    = nfs4_commit_done,
9358         .lock           = nfs4_proc_lock,
9359         .clear_acl_cache = nfs4_zap_acl_attr,
9360         .close_context  = nfs4_close_context,
9361         .open_context   = nfs4_atomic_open,
9362         .have_delegation = nfs4_have_delegation,
9363         .return_delegation = nfs4_inode_return_delegation,
9364         .alloc_client   = nfs4_alloc_client,
9365         .init_client    = nfs4_init_client,
9366         .free_client    = nfs4_free_client,
9367         .create_server  = nfs4_create_server,
9368         .clone_server   = nfs_clone_server,
9369 };
9370
9371 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9372         .name   = XATTR_NAME_NFSV4_ACL,
9373         .list   = nfs4_xattr_list_nfs4_acl,
9374         .get    = nfs4_xattr_get_nfs4_acl,
9375         .set    = nfs4_xattr_set_nfs4_acl,
9376 };
9377
9378 const struct xattr_handler *nfs4_xattr_handlers[] = {
9379         &nfs4_xattr_nfs4_acl_handler,
9380 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9381         &nfs4_xattr_nfs4_label_handler,
9382 #endif
9383         NULL
9384 };
9385
9386 /*
9387  * Local variables:
9388  *  c-basic-offset: 8
9389  * End:
9390  */