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