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