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