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