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