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