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