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