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