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