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