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