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