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