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