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