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