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