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