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