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