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