Merge branches 'arm/rockchip', 'arm/exynos', 'arm/smmu', 'x86/vt-d', 'x86/amd', ...
[sfrench/cifs-2.6.git] / fs / nfs / nfs4proc.c
1 /*
2  *  fs/nfs/nfs4proc.c
3  *
4  *  Client-side procedure declarations for NFSv4.
5  *
6  *  Copyright (c) 2002 The Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  Kendrick Smith <kmsmith@umich.edu>
10  *  Andy Adamson   <andros@umich.edu>
11  *
12  *  Redistribution and use in source and binary forms, with or without
13  *  modification, are permitted provided that the following conditions
14  *  are met:
15  *
16  *  1. Redistributions of source code must retain the above copyright
17  *     notice, this list of conditions and the following disclaimer.
18  *  2. Redistributions in binary form must reproduce the above copyright
19  *     notice, this list of conditions and the following disclaimer in the
20  *     documentation and/or other materials provided with the distribution.
21  *  3. Neither the name of the University nor the names of its
22  *     contributors may be used to endorse or promote products derived
23  *     from this software without specific prior written permission.
24  *
25  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 #include <linux/mm.h>
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/file.h>
42 #include <linux/string.h>
43 #include <linux/ratelimit.h>
44 #include <linux/printk.h>
45 #include <linux/slab.h>
46 #include <linux/sunrpc/clnt.h>
47 #include <linux/nfs.h>
48 #include <linux/nfs4.h>
49 #include <linux/nfs_fs.h>
50 #include <linux/nfs_page.h>
51 #include <linux/nfs_mount.h>
52 #include <linux/namei.h>
53 #include <linux/mount.h>
54 #include <linux/module.h>
55 #include <linux/xattr.h>
56 #include <linux/utsname.h>
57 #include <linux/freezer.h>
58
59 #include "nfs4_fs.h"
60 #include "delegation.h"
61 #include "internal.h"
62 #include "iostat.h"
63 #include "callback.h"
64 #include "pnfs.h"
65 #include "netns.h"
66 #include "nfs4idmap.h"
67 #include "nfs4session.h"
68 #include "fscache.h"
69
70 #include "nfs4trace.h"
71
72 #define NFSDBG_FACILITY         NFSDBG_PROC
73
74 #define NFS4_POLL_RETRY_MIN     (HZ/10)
75 #define NFS4_POLL_RETRY_MAX     (15*HZ)
76
77 struct nfs4_opendata;
78 static int _nfs4_proc_open(struct nfs4_opendata *data);
79 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
80 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
81 static int nfs4_async_handle_error(struct rpc_task *, 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)
1208                 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1209         if (state->n_rdonly)
1210                 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1211         if (state->n_rdwr)
1212                 set_bit(NFS_O_RDWR_STATE, &state->flags);
1213 }
1214
1215 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1216                 nfs4_stateid *stateid, fmode_t fmode)
1217 {
1218         clear_bit(NFS_O_RDWR_STATE, &state->flags);
1219         switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1220         case FMODE_WRITE:
1221                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1222                 break;
1223         case FMODE_READ:
1224                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1225                 break;
1226         case 0:
1227                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1228                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1229                 clear_bit(NFS_OPEN_STATE, &state->flags);
1230         }
1231         if (stateid == NULL)
1232                 return;
1233         /* Handle races with OPEN */
1234         if (!nfs4_stateid_match_other(stateid, &state->open_stateid) ||
1235             !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1236                 nfs_resync_open_stateid_locked(state);
1237                 return;
1238         }
1239         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1240                 nfs4_stateid_copy(&state->stateid, stateid);
1241         nfs4_stateid_copy(&state->open_stateid, stateid);
1242 }
1243
1244 static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1245 {
1246         write_seqlock(&state->seqlock);
1247         nfs_clear_open_stateid_locked(state, stateid, fmode);
1248         write_sequnlock(&state->seqlock);
1249         if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1250                 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1251 }
1252
1253 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1254 {
1255         switch (fmode) {
1256                 case FMODE_READ:
1257                         set_bit(NFS_O_RDONLY_STATE, &state->flags);
1258                         break;
1259                 case FMODE_WRITE:
1260                         set_bit(NFS_O_WRONLY_STATE, &state->flags);
1261                         break;
1262                 case FMODE_READ|FMODE_WRITE:
1263                         set_bit(NFS_O_RDWR_STATE, &state->flags);
1264         }
1265         if (!nfs_need_update_open_stateid(state, stateid))
1266                 return;
1267         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1268                 nfs4_stateid_copy(&state->stateid, stateid);
1269         nfs4_stateid_copy(&state->open_stateid, stateid);
1270 }
1271
1272 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1273 {
1274         /*
1275          * Protect the call to nfs4_state_set_mode_locked and
1276          * serialise the stateid update
1277          */
1278         write_seqlock(&state->seqlock);
1279         if (deleg_stateid != NULL) {
1280                 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1281                 set_bit(NFS_DELEGATED_STATE, &state->flags);
1282         }
1283         if (open_stateid != NULL)
1284                 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1285         write_sequnlock(&state->seqlock);
1286         spin_lock(&state->owner->so_lock);
1287         update_open_stateflags(state, fmode);
1288         spin_unlock(&state->owner->so_lock);
1289 }
1290
1291 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1292 {
1293         struct nfs_inode *nfsi = NFS_I(state->inode);
1294         struct nfs_delegation *deleg_cur;
1295         int ret = 0;
1296
1297         fmode &= (FMODE_READ|FMODE_WRITE);
1298
1299         rcu_read_lock();
1300         deleg_cur = rcu_dereference(nfsi->delegation);
1301         if (deleg_cur == NULL)
1302                 goto no_delegation;
1303
1304         spin_lock(&deleg_cur->lock);
1305         if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1306            test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1307             (deleg_cur->type & fmode) != fmode)
1308                 goto no_delegation_unlock;
1309
1310         if (delegation == NULL)
1311                 delegation = &deleg_cur->stateid;
1312         else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1313                 goto no_delegation_unlock;
1314
1315         nfs_mark_delegation_referenced(deleg_cur);
1316         __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1317         ret = 1;
1318 no_delegation_unlock:
1319         spin_unlock(&deleg_cur->lock);
1320 no_delegation:
1321         rcu_read_unlock();
1322
1323         if (!ret && open_stateid != NULL) {
1324                 __update_open_stateid(state, open_stateid, NULL, fmode);
1325                 ret = 1;
1326         }
1327         if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1328                 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1329
1330         return ret;
1331 }
1332
1333 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1334                 const nfs4_stateid *stateid)
1335 {
1336         struct nfs4_state *state = lsp->ls_state;
1337         bool ret = false;
1338
1339         spin_lock(&state->state_lock);
1340         if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1341                 goto out_noupdate;
1342         if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1343                 goto out_noupdate;
1344         nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1345         ret = true;
1346 out_noupdate:
1347         spin_unlock(&state->state_lock);
1348         return ret;
1349 }
1350
1351 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1352 {
1353         struct nfs_delegation *delegation;
1354
1355         rcu_read_lock();
1356         delegation = rcu_dereference(NFS_I(inode)->delegation);
1357         if (delegation == NULL || (delegation->type & fmode) == fmode) {
1358                 rcu_read_unlock();
1359                 return;
1360         }
1361         rcu_read_unlock();
1362         nfs4_inode_return_delegation(inode);
1363 }
1364
1365 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1366 {
1367         struct nfs4_state *state = opendata->state;
1368         struct nfs_inode *nfsi = NFS_I(state->inode);
1369         struct nfs_delegation *delegation;
1370         int open_mode = opendata->o_arg.open_flags;
1371         fmode_t fmode = opendata->o_arg.fmode;
1372         nfs4_stateid stateid;
1373         int ret = -EAGAIN;
1374
1375         for (;;) {
1376                 spin_lock(&state->owner->so_lock);
1377                 if (can_open_cached(state, fmode, open_mode)) {
1378                         update_open_stateflags(state, fmode);
1379                         spin_unlock(&state->owner->so_lock);
1380                         goto out_return_state;
1381                 }
1382                 spin_unlock(&state->owner->so_lock);
1383                 rcu_read_lock();
1384                 delegation = rcu_dereference(nfsi->delegation);
1385                 if (!can_open_delegated(delegation, fmode)) {
1386                         rcu_read_unlock();
1387                         break;
1388                 }
1389                 /* Save the delegation */
1390                 nfs4_stateid_copy(&stateid, &delegation->stateid);
1391                 rcu_read_unlock();
1392                 nfs_release_seqid(opendata->o_arg.seqid);
1393                 if (!opendata->is_recover) {
1394                         ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1395                         if (ret != 0)
1396                                 goto out;
1397                 }
1398                 ret = -EAGAIN;
1399
1400                 /* Try to update the stateid using the delegation */
1401                 if (update_open_stateid(state, NULL, &stateid, fmode))
1402                         goto out_return_state;
1403         }
1404 out:
1405         return ERR_PTR(ret);
1406 out_return_state:
1407         atomic_inc(&state->count);
1408         return state;
1409 }
1410
1411 static void
1412 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1413 {
1414         struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1415         struct nfs_delegation *delegation;
1416         int delegation_flags = 0;
1417
1418         rcu_read_lock();
1419         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1420         if (delegation)
1421                 delegation_flags = delegation->flags;
1422         rcu_read_unlock();
1423         if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1424                 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1425                                    "returning a delegation for "
1426                                    "OPEN(CLAIM_DELEGATE_CUR)\n",
1427                                    clp->cl_hostname);
1428         } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1429                 nfs_inode_set_delegation(state->inode,
1430                                          data->owner->so_cred,
1431                                          &data->o_res);
1432         else
1433                 nfs_inode_reclaim_delegation(state->inode,
1434                                              data->owner->so_cred,
1435                                              &data->o_res);
1436 }
1437
1438 /*
1439  * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1440  * and update the nfs4_state.
1441  */
1442 static struct nfs4_state *
1443 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1444 {
1445         struct inode *inode = data->state->inode;
1446         struct nfs4_state *state = data->state;
1447         int ret;
1448
1449         if (!data->rpc_done) {
1450                 if (data->rpc_status) {
1451                         ret = data->rpc_status;
1452                         goto err;
1453                 }
1454                 /* cached opens have already been processed */
1455                 goto update;
1456         }
1457
1458         ret = nfs_refresh_inode(inode, &data->f_attr);
1459         if (ret)
1460                 goto err;
1461
1462         if (data->o_res.delegation_type != 0)
1463                 nfs4_opendata_check_deleg(data, state);
1464 update:
1465         update_open_stateid(state, &data->o_res.stateid, NULL,
1466                             data->o_arg.fmode);
1467         atomic_inc(&state->count);
1468
1469         return state;
1470 err:
1471         return ERR_PTR(ret);
1472
1473 }
1474
1475 static struct nfs4_state *
1476 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1477 {
1478         struct inode *inode;
1479         struct nfs4_state *state = NULL;
1480         int ret;
1481
1482         if (!data->rpc_done) {
1483                 state = nfs4_try_open_cached(data);
1484                 goto out;
1485         }
1486
1487         ret = -EAGAIN;
1488         if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1489                 goto err;
1490         inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1491         ret = PTR_ERR(inode);
1492         if (IS_ERR(inode))
1493                 goto err;
1494         ret = -ENOMEM;
1495         state = nfs4_get_open_state(inode, data->owner);
1496         if (state == NULL)
1497                 goto err_put_inode;
1498         if (data->o_res.delegation_type != 0)
1499                 nfs4_opendata_check_deleg(data, state);
1500         update_open_stateid(state, &data->o_res.stateid, NULL,
1501                         data->o_arg.fmode);
1502         iput(inode);
1503 out:
1504         nfs_release_seqid(data->o_arg.seqid);
1505         return state;
1506 err_put_inode:
1507         iput(inode);
1508 err:
1509         return ERR_PTR(ret);
1510 }
1511
1512 static struct nfs4_state *
1513 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1514 {
1515         if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1516                 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1517         return _nfs4_opendata_to_nfs4_state(data);
1518 }
1519
1520 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1521 {
1522         struct nfs_inode *nfsi = NFS_I(state->inode);
1523         struct nfs_open_context *ctx;
1524
1525         spin_lock(&state->inode->i_lock);
1526         list_for_each_entry(ctx, &nfsi->open_files, list) {
1527                 if (ctx->state != state)
1528                         continue;
1529                 get_nfs_open_context(ctx);
1530                 spin_unlock(&state->inode->i_lock);
1531                 return ctx;
1532         }
1533         spin_unlock(&state->inode->i_lock);
1534         return ERR_PTR(-ENOENT);
1535 }
1536
1537 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1538                 struct nfs4_state *state, enum open_claim_type4 claim)
1539 {
1540         struct nfs4_opendata *opendata;
1541
1542         opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1543                         NULL, NULL, claim, GFP_NOFS);
1544         if (opendata == NULL)
1545                 return ERR_PTR(-ENOMEM);
1546         opendata->state = state;
1547         atomic_inc(&state->count);
1548         return opendata;
1549 }
1550
1551 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1552 {
1553         struct nfs4_state *newstate;
1554         int ret;
1555
1556         opendata->o_arg.open_flags = 0;
1557         opendata->o_arg.fmode = fmode;
1558         opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1559                         NFS_SB(opendata->dentry->d_sb),
1560                         fmode, 0);
1561         memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1562         memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1563         nfs4_init_opendata_res(opendata);
1564         ret = _nfs4_recover_proc_open(opendata);
1565         if (ret != 0)
1566                 return ret; 
1567         newstate = nfs4_opendata_to_nfs4_state(opendata);
1568         if (IS_ERR(newstate))
1569                 return PTR_ERR(newstate);
1570         nfs4_close_state(newstate, fmode);
1571         *res = newstate;
1572         return 0;
1573 }
1574
1575 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1576 {
1577         struct nfs4_state *newstate;
1578         int ret;
1579
1580         /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1581         clear_bit(NFS_O_RDWR_STATE, &state->flags);
1582         clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1583         clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1584         /* memory barrier prior to reading state->n_* */
1585         clear_bit(NFS_DELEGATED_STATE, &state->flags);
1586         clear_bit(NFS_OPEN_STATE, &state->flags);
1587         smp_rmb();
1588         if (state->n_rdwr != 0) {
1589                 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1590                 if (ret != 0)
1591                         return ret;
1592                 if (newstate != state)
1593                         return -ESTALE;
1594         }
1595         if (state->n_wronly != 0) {
1596                 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1597                 if (ret != 0)
1598                         return ret;
1599                 if (newstate != state)
1600                         return -ESTALE;
1601         }
1602         if (state->n_rdonly != 0) {
1603                 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1604                 if (ret != 0)
1605                         return ret;
1606                 if (newstate != state)
1607                         return -ESTALE;
1608         }
1609         /*
1610          * We may have performed cached opens for all three recoveries.
1611          * Check if we need to update the current stateid.
1612          */
1613         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1614             !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1615                 write_seqlock(&state->seqlock);
1616                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1617                         nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1618                 write_sequnlock(&state->seqlock);
1619         }
1620         return 0;
1621 }
1622
1623 /*
1624  * OPEN_RECLAIM:
1625  *      reclaim state on the server after a reboot.
1626  */
1627 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1628 {
1629         struct nfs_delegation *delegation;
1630         struct nfs4_opendata *opendata;
1631         fmode_t delegation_type = 0;
1632         int status;
1633
1634         opendata = nfs4_open_recoverdata_alloc(ctx, state,
1635                         NFS4_OPEN_CLAIM_PREVIOUS);
1636         if (IS_ERR(opendata))
1637                 return PTR_ERR(opendata);
1638         rcu_read_lock();
1639         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1640         if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1641                 delegation_type = delegation->type;
1642         rcu_read_unlock();
1643         opendata->o_arg.u.delegation_type = delegation_type;
1644         status = nfs4_open_recover(opendata, state);
1645         nfs4_opendata_put(opendata);
1646         return status;
1647 }
1648
1649 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1650 {
1651         struct nfs_server *server = NFS_SERVER(state->inode);
1652         struct nfs4_exception exception = { };
1653         int err;
1654         do {
1655                 err = _nfs4_do_open_reclaim(ctx, state);
1656                 trace_nfs4_open_reclaim(ctx, 0, err);
1657                 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1658                         continue;
1659                 if (err != -NFS4ERR_DELAY)
1660                         break;
1661                 nfs4_handle_exception(server, err, &exception);
1662         } while (exception.retry);
1663         return err;
1664 }
1665
1666 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1667 {
1668         struct nfs_open_context *ctx;
1669         int ret;
1670
1671         ctx = nfs4_state_find_open_context(state);
1672         if (IS_ERR(ctx))
1673                 return -EAGAIN;
1674         ret = nfs4_do_open_reclaim(ctx, state);
1675         put_nfs_open_context(ctx);
1676         return ret;
1677 }
1678
1679 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1680 {
1681         switch (err) {
1682                 default:
1683                         printk(KERN_ERR "NFS: %s: unhandled error "
1684                                         "%d.\n", __func__, err);
1685                 case 0:
1686                 case -ENOENT:
1687                 case -ESTALE:
1688                         break;
1689                 case -NFS4ERR_BADSESSION:
1690                 case -NFS4ERR_BADSLOT:
1691                 case -NFS4ERR_BAD_HIGH_SLOT:
1692                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1693                 case -NFS4ERR_DEADSESSION:
1694                         set_bit(NFS_DELEGATED_STATE, &state->flags);
1695                         nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1696                         return -EAGAIN;
1697                 case -NFS4ERR_STALE_CLIENTID:
1698                 case -NFS4ERR_STALE_STATEID:
1699                         set_bit(NFS_DELEGATED_STATE, &state->flags);
1700                 case -NFS4ERR_EXPIRED:
1701                         /* Don't recall a delegation if it was lost */
1702                         nfs4_schedule_lease_recovery(server->nfs_client);
1703                         return -EAGAIN;
1704                 case -NFS4ERR_MOVED:
1705                         nfs4_schedule_migration_recovery(server);
1706                         return -EAGAIN;
1707                 case -NFS4ERR_LEASE_MOVED:
1708                         nfs4_schedule_lease_moved_recovery(server->nfs_client);
1709                         return -EAGAIN;
1710                 case -NFS4ERR_DELEG_REVOKED:
1711                 case -NFS4ERR_ADMIN_REVOKED:
1712                 case -NFS4ERR_BAD_STATEID:
1713                 case -NFS4ERR_OPENMODE:
1714                         nfs_inode_find_state_and_recover(state->inode,
1715                                         stateid);
1716                         nfs4_schedule_stateid_recovery(server, state);
1717                         return -EAGAIN;
1718                 case -NFS4ERR_DELAY:
1719                 case -NFS4ERR_GRACE:
1720                         set_bit(NFS_DELEGATED_STATE, &state->flags);
1721                         ssleep(1);
1722                         return -EAGAIN;
1723                 case -ENOMEM:
1724                 case -NFS4ERR_DENIED:
1725                         /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1726                         return 0;
1727         }
1728         return err;
1729 }
1730
1731 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1732 {
1733         struct nfs_server *server = NFS_SERVER(state->inode);
1734         struct nfs4_opendata *opendata;
1735         int err;
1736
1737         opendata = nfs4_open_recoverdata_alloc(ctx, state,
1738                         NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1739         if (IS_ERR(opendata))
1740                 return PTR_ERR(opendata);
1741         nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1742         err = nfs4_open_recover(opendata, state);
1743         nfs4_opendata_put(opendata);
1744         return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1745 }
1746
1747 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1748 {
1749         struct nfs4_opendata *data = calldata;
1750
1751         nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1752                              &data->c_arg.seq_args, &data->c_res.seq_res, task);
1753 }
1754
1755 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1756 {
1757         struct nfs4_opendata *data = calldata;
1758
1759         nfs40_sequence_done(task, &data->c_res.seq_res);
1760
1761         data->rpc_status = task->tk_status;
1762         if (data->rpc_status == 0) {
1763                 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1764                 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1765                 renew_lease(data->o_res.server, data->timestamp);
1766                 data->rpc_done = 1;
1767         }
1768 }
1769
1770 static void nfs4_open_confirm_release(void *calldata)
1771 {
1772         struct nfs4_opendata *data = calldata;
1773         struct nfs4_state *state = NULL;
1774
1775         /* If this request hasn't been cancelled, do nothing */
1776         if (data->cancelled == 0)
1777                 goto out_free;
1778         /* In case of error, no cleanup! */
1779         if (!data->rpc_done)
1780                 goto out_free;
1781         state = nfs4_opendata_to_nfs4_state(data);
1782         if (!IS_ERR(state))
1783                 nfs4_close_state(state, data->o_arg.fmode);
1784 out_free:
1785         nfs4_opendata_put(data);
1786 }
1787
1788 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1789         .rpc_call_prepare = nfs4_open_confirm_prepare,
1790         .rpc_call_done = nfs4_open_confirm_done,
1791         .rpc_release = nfs4_open_confirm_release,
1792 };
1793
1794 /*
1795  * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1796  */
1797 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1798 {
1799         struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
1800         struct rpc_task *task;
1801         struct  rpc_message msg = {
1802                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1803                 .rpc_argp = &data->c_arg,
1804                 .rpc_resp = &data->c_res,
1805                 .rpc_cred = data->owner->so_cred,
1806         };
1807         struct rpc_task_setup task_setup_data = {
1808                 .rpc_client = server->client,
1809                 .rpc_message = &msg,
1810                 .callback_ops = &nfs4_open_confirm_ops,
1811                 .callback_data = data,
1812                 .workqueue = nfsiod_workqueue,
1813                 .flags = RPC_TASK_ASYNC,
1814         };
1815         int status;
1816
1817         nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
1818         kref_get(&data->kref);
1819         data->rpc_done = 0;
1820         data->rpc_status = 0;
1821         data->timestamp = jiffies;
1822         task = rpc_run_task(&task_setup_data);
1823         if (IS_ERR(task))
1824                 return PTR_ERR(task);
1825         status = nfs4_wait_for_completion_rpc_task(task);
1826         if (status != 0) {
1827                 data->cancelled = 1;
1828                 smp_wmb();
1829         } else
1830                 status = data->rpc_status;
1831         rpc_put_task(task);
1832         return status;
1833 }
1834
1835 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1836 {
1837         struct nfs4_opendata *data = calldata;
1838         struct nfs4_state_owner *sp = data->owner;
1839         struct nfs_client *clp = sp->so_server->nfs_client;
1840
1841         if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1842                 goto out_wait;
1843         /*
1844          * Check if we still need to send an OPEN call, or if we can use
1845          * a delegation instead.
1846          */
1847         if (data->state != NULL) {
1848                 struct nfs_delegation *delegation;
1849
1850                 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1851                         goto out_no_action;
1852                 rcu_read_lock();
1853                 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1854                 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1855                     data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
1856                     can_open_delegated(delegation, data->o_arg.fmode))
1857                         goto unlock_no_action;
1858                 rcu_read_unlock();
1859         }
1860         /* Update client id. */
1861         data->o_arg.clientid = clp->cl_clientid;
1862         switch (data->o_arg.claim) {
1863         case NFS4_OPEN_CLAIM_PREVIOUS:
1864         case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1865         case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1866                 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1867         case NFS4_OPEN_CLAIM_FH:
1868                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1869                 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1870         }
1871         data->timestamp = jiffies;
1872         if (nfs4_setup_sequence(data->o_arg.server,
1873                                 &data->o_arg.seq_args,
1874                                 &data->o_res.seq_res,
1875                                 task) != 0)
1876                 nfs_release_seqid(data->o_arg.seqid);
1877
1878         /* Set the create mode (note dependency on the session type) */
1879         data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1880         if (data->o_arg.open_flags & O_EXCL) {
1881                 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1882                 if (nfs4_has_persistent_session(clp))
1883                         data->o_arg.createmode = NFS4_CREATE_GUARDED;
1884                 else if (clp->cl_mvops->minor_version > 0)
1885                         data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1886         }
1887         return;
1888 unlock_no_action:
1889         rcu_read_unlock();
1890 out_no_action:
1891         task->tk_action = NULL;
1892 out_wait:
1893         nfs4_sequence_done(task, &data->o_res.seq_res);
1894 }
1895
1896 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1897 {
1898         struct nfs4_opendata *data = calldata;
1899
1900         data->rpc_status = task->tk_status;
1901
1902         if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1903                 return;
1904
1905         if (task->tk_status == 0) {
1906                 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1907                         switch (data->o_res.f_attr->mode & S_IFMT) {
1908                         case S_IFREG:
1909                                 break;
1910                         case S_IFLNK:
1911                                 data->rpc_status = -ELOOP;
1912                                 break;
1913                         case S_IFDIR:
1914                                 data->rpc_status = -EISDIR;
1915                                 break;
1916                         default:
1917                                 data->rpc_status = -ENOTDIR;
1918                         }
1919                 }
1920                 renew_lease(data->o_res.server, data->timestamp);
1921                 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1922                         nfs_confirm_seqid(&data->owner->so_seqid, 0);
1923         }
1924         data->rpc_done = 1;
1925 }
1926
1927 static void nfs4_open_release(void *calldata)
1928 {
1929         struct nfs4_opendata *data = calldata;
1930         struct nfs4_state *state = NULL;
1931
1932         /* If this request hasn't been cancelled, do nothing */
1933         if (data->cancelled == 0)
1934                 goto out_free;
1935         /* In case of error, no cleanup! */
1936         if (data->rpc_status != 0 || !data->rpc_done)
1937                 goto out_free;
1938         /* In case we need an open_confirm, no cleanup! */
1939         if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1940                 goto out_free;
1941         state = nfs4_opendata_to_nfs4_state(data);
1942         if (!IS_ERR(state))
1943                 nfs4_close_state(state, data->o_arg.fmode);
1944 out_free:
1945         nfs4_opendata_put(data);
1946 }
1947
1948 static const struct rpc_call_ops nfs4_open_ops = {
1949         .rpc_call_prepare = nfs4_open_prepare,
1950         .rpc_call_done = nfs4_open_done,
1951         .rpc_release = nfs4_open_release,
1952 };
1953
1954 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1955 {
1956         struct inode *dir = d_inode(data->dir);
1957         struct nfs_server *server = NFS_SERVER(dir);
1958         struct nfs_openargs *o_arg = &data->o_arg;
1959         struct nfs_openres *o_res = &data->o_res;
1960         struct rpc_task *task;
1961         struct rpc_message msg = {
1962                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1963                 .rpc_argp = o_arg,
1964                 .rpc_resp = o_res,
1965                 .rpc_cred = data->owner->so_cred,
1966         };
1967         struct rpc_task_setup task_setup_data = {
1968                 .rpc_client = server->client,
1969                 .rpc_message = &msg,
1970                 .callback_ops = &nfs4_open_ops,
1971                 .callback_data = data,
1972                 .workqueue = nfsiod_workqueue,
1973                 .flags = RPC_TASK_ASYNC,
1974         };
1975         int status;
1976
1977         nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1978         kref_get(&data->kref);
1979         data->rpc_done = 0;
1980         data->rpc_status = 0;
1981         data->cancelled = 0;
1982         data->is_recover = 0;
1983         if (isrecover) {
1984                 nfs4_set_sequence_privileged(&o_arg->seq_args);
1985                 data->is_recover = 1;
1986         }
1987         task = rpc_run_task(&task_setup_data);
1988         if (IS_ERR(task))
1989                 return PTR_ERR(task);
1990         status = nfs4_wait_for_completion_rpc_task(task);
1991         if (status != 0) {
1992                 data->cancelled = 1;
1993                 smp_wmb();
1994         } else
1995                 status = data->rpc_status;
1996         rpc_put_task(task);
1997
1998         return status;
1999 }
2000
2001 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2002 {
2003         struct inode *dir = d_inode(data->dir);
2004         struct nfs_openres *o_res = &data->o_res;
2005         int status;
2006
2007         status = nfs4_run_open_task(data, 1);
2008         if (status != 0 || !data->rpc_done)
2009                 return status;
2010
2011         nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2012
2013         if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2014                 status = _nfs4_proc_open_confirm(data);
2015                 if (status != 0)
2016                         return status;
2017         }
2018
2019         return status;
2020 }
2021
2022 /*
2023  * Additional permission checks in order to distinguish between an
2024  * open for read, and an open for execute. This works around the
2025  * fact that NFSv4 OPEN treats read and execute permissions as being
2026  * the same.
2027  * Note that in the non-execute case, we want to turn off permission
2028  * checking if we just created a new file (POSIX open() semantics).
2029  */
2030 static int nfs4_opendata_access(struct rpc_cred *cred,
2031                                 struct nfs4_opendata *opendata,
2032                                 struct nfs4_state *state, fmode_t fmode,
2033                                 int openflags)
2034 {
2035         struct nfs_access_entry cache;
2036         u32 mask;
2037
2038         /* access call failed or for some reason the server doesn't
2039          * support any access modes -- defer access call until later */
2040         if (opendata->o_res.access_supported == 0)
2041                 return 0;
2042
2043         mask = 0;
2044         /*
2045          * Use openflags to check for exec, because fmode won't
2046          * always have FMODE_EXEC set when file open for exec.
2047          */
2048         if (openflags & __FMODE_EXEC) {
2049                 /* ONLY check for exec rights */
2050                 mask = MAY_EXEC;
2051         } else if ((fmode & FMODE_READ) && !opendata->file_created)
2052                 mask = MAY_READ;
2053
2054         cache.cred = cred;
2055         cache.jiffies = jiffies;
2056         nfs_access_set_mask(&cache, opendata->o_res.access_result);
2057         nfs_access_add_cache(state->inode, &cache);
2058
2059         if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
2060                 return 0;
2061
2062         /* even though OPEN succeeded, access is denied. Close the file */
2063         nfs4_close_state(state, fmode);
2064         return -EACCES;
2065 }
2066
2067 /*
2068  * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2069  */
2070 static int _nfs4_proc_open(struct nfs4_opendata *data)
2071 {
2072         struct inode *dir = d_inode(data->dir);
2073         struct nfs_server *server = NFS_SERVER(dir);
2074         struct nfs_openargs *o_arg = &data->o_arg;
2075         struct nfs_openres *o_res = &data->o_res;
2076         int status;
2077
2078         status = nfs4_run_open_task(data, 0);
2079         if (!data->rpc_done)
2080                 return status;
2081         if (status != 0) {
2082                 if (status == -NFS4ERR_BADNAME &&
2083                                 !(o_arg->open_flags & O_CREAT))
2084                         return -ENOENT;
2085                 return status;
2086         }
2087
2088         nfs_fattr_map_and_free_names(server, &data->f_attr);
2089
2090         if (o_arg->open_flags & O_CREAT) {
2091                 update_changeattr(dir, &o_res->cinfo);
2092                 if (o_arg->open_flags & O_EXCL)
2093                         data->file_created = 1;
2094                 else if (o_res->cinfo.before != o_res->cinfo.after)
2095                         data->file_created = 1;
2096         }
2097         if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2098                 server->caps &= ~NFS_CAP_POSIX_LOCK;
2099         if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2100                 status = _nfs4_proc_open_confirm(data);
2101                 if (status != 0)
2102                         return status;
2103         }
2104         if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
2105                 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2106         return 0;
2107 }
2108
2109 static int nfs4_recover_expired_lease(struct nfs_server *server)
2110 {
2111         return nfs4_client_recover_expired_lease(server->nfs_client);
2112 }
2113
2114 /*
2115  * OPEN_EXPIRED:
2116  *      reclaim state on the server after a network partition.
2117  *      Assumes caller holds the appropriate lock
2118  */
2119 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2120 {
2121         struct nfs4_opendata *opendata;
2122         int ret;
2123
2124         opendata = nfs4_open_recoverdata_alloc(ctx, state,
2125                         NFS4_OPEN_CLAIM_FH);
2126         if (IS_ERR(opendata))
2127                 return PTR_ERR(opendata);
2128         ret = nfs4_open_recover(opendata, state);
2129         if (ret == -ESTALE)
2130                 d_drop(ctx->dentry);
2131         nfs4_opendata_put(opendata);
2132         return ret;
2133 }
2134
2135 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2136 {
2137         struct nfs_server *server = NFS_SERVER(state->inode);
2138         struct nfs4_exception exception = { };
2139         int err;
2140
2141         do {
2142                 err = _nfs4_open_expired(ctx, state);
2143                 trace_nfs4_open_expired(ctx, 0, err);
2144                 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2145                         continue;
2146                 switch (err) {
2147                 default:
2148                         goto out;
2149                 case -NFS4ERR_GRACE:
2150                 case -NFS4ERR_DELAY:
2151                         nfs4_handle_exception(server, err, &exception);
2152                         err = 0;
2153                 }
2154         } while (exception.retry);
2155 out:
2156         return err;
2157 }
2158
2159 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2160 {
2161         struct nfs_open_context *ctx;
2162         int ret;
2163
2164         ctx = nfs4_state_find_open_context(state);
2165         if (IS_ERR(ctx))
2166                 return -EAGAIN;
2167         ret = nfs4_do_open_expired(ctx, state);
2168         put_nfs_open_context(ctx);
2169         return ret;
2170 }
2171
2172 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2173 {
2174         nfs_remove_bad_delegation(state->inode);
2175         write_seqlock(&state->seqlock);
2176         nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2177         write_sequnlock(&state->seqlock);
2178         clear_bit(NFS_DELEGATED_STATE, &state->flags);
2179 }
2180
2181 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2182 {
2183         if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2184                 nfs_finish_clear_delegation_stateid(state);
2185 }
2186
2187 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2188 {
2189         /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2190         nfs40_clear_delegation_stateid(state);
2191         return nfs4_open_expired(sp, state);
2192 }
2193
2194 #if defined(CONFIG_NFS_V4_1)
2195 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2196 {
2197         struct nfs_server *server = NFS_SERVER(state->inode);
2198         nfs4_stateid stateid;
2199         struct nfs_delegation *delegation;
2200         struct rpc_cred *cred;
2201         int status;
2202
2203         /* Get the delegation credential for use by test/free_stateid */
2204         rcu_read_lock();
2205         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2206         if (delegation == NULL) {
2207                 rcu_read_unlock();
2208                 return;
2209         }
2210
2211         nfs4_stateid_copy(&stateid, &delegation->stateid);
2212         cred = get_rpccred(delegation->cred);
2213         rcu_read_unlock();
2214         status = nfs41_test_stateid(server, &stateid, cred);
2215         trace_nfs4_test_delegation_stateid(state, NULL, status);
2216
2217         if (status != NFS_OK) {
2218                 /* Free the stateid unless the server explicitly
2219                  * informs us the stateid is unrecognized. */
2220                 if (status != -NFS4ERR_BAD_STATEID)
2221                         nfs41_free_stateid(server, &stateid, cred);
2222                 nfs_finish_clear_delegation_stateid(state);
2223         }
2224
2225         put_rpccred(cred);
2226 }
2227
2228 /**
2229  * nfs41_check_open_stateid - possibly free an open stateid
2230  *
2231  * @state: NFSv4 state for an inode
2232  *
2233  * Returns NFS_OK if recovery for this stateid is now finished.
2234  * Otherwise a negative NFS4ERR value is returned.
2235  */
2236 static int nfs41_check_open_stateid(struct nfs4_state *state)
2237 {
2238         struct nfs_server *server = NFS_SERVER(state->inode);
2239         nfs4_stateid *stateid = &state->open_stateid;
2240         struct rpc_cred *cred = state->owner->so_cred;
2241         int status;
2242
2243         /* If a state reset has been done, test_stateid is unneeded */
2244         if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2245             (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2246             (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2247                 return -NFS4ERR_BAD_STATEID;
2248
2249         status = nfs41_test_stateid(server, stateid, cred);
2250         trace_nfs4_test_open_stateid(state, NULL, status);
2251         if (status != NFS_OK) {
2252                 /* Free the stateid unless the server explicitly
2253                  * informs us the stateid is unrecognized. */
2254                 if (status != -NFS4ERR_BAD_STATEID)
2255                         nfs41_free_stateid(server, stateid, cred);
2256
2257                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2258                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2259                 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2260                 clear_bit(NFS_OPEN_STATE, &state->flags);
2261         }
2262         return status;
2263 }
2264
2265 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2266 {
2267         int status;
2268
2269         nfs41_check_delegation_stateid(state);
2270         status = nfs41_check_open_stateid(state);
2271         if (status != NFS_OK)
2272                 status = nfs4_open_expired(sp, state);
2273         return status;
2274 }
2275 #endif
2276
2277 /*
2278  * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2279  * fields corresponding to attributes that were used to store the verifier.
2280  * Make sure we clobber those fields in the later setattr call
2281  */
2282 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2283 {
2284         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2285             !(sattr->ia_valid & ATTR_ATIME_SET))
2286                 sattr->ia_valid |= ATTR_ATIME;
2287
2288         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2289             !(sattr->ia_valid & ATTR_MTIME_SET))
2290                 sattr->ia_valid |= ATTR_MTIME;
2291 }
2292
2293 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2294                 fmode_t fmode,
2295                 int flags,
2296                 struct nfs_open_context *ctx)
2297 {
2298         struct nfs4_state_owner *sp = opendata->owner;
2299         struct nfs_server *server = sp->so_server;
2300         struct dentry *dentry;
2301         struct nfs4_state *state;
2302         unsigned int seq;
2303         int ret;
2304
2305         seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2306
2307         ret = _nfs4_proc_open(opendata);
2308         if (ret != 0)
2309                 goto out;
2310
2311         state = nfs4_opendata_to_nfs4_state(opendata);
2312         ret = PTR_ERR(state);
2313         if (IS_ERR(state))
2314                 goto out;
2315         if (server->caps & NFS_CAP_POSIX_LOCK)
2316                 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2317
2318         dentry = opendata->dentry;
2319         if (d_really_is_negative(dentry)) {
2320                 /* FIXME: Is this d_drop() ever needed? */
2321                 d_drop(dentry);
2322                 dentry = d_add_unique(dentry, igrab(state->inode));
2323                 if (dentry == NULL) {
2324                         dentry = opendata->dentry;
2325                 } else if (dentry != ctx->dentry) {
2326                         dput(ctx->dentry);
2327                         ctx->dentry = dget(dentry);
2328                 }
2329                 nfs_set_verifier(dentry,
2330                                 nfs_save_change_attribute(d_inode(opendata->dir)));
2331         }
2332
2333         ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2334         if (ret != 0)
2335                 goto out;
2336
2337         ctx->state = state;
2338         if (d_inode(dentry) == state->inode) {
2339                 nfs_inode_attach_open_context(ctx);
2340                 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2341                         nfs4_schedule_stateid_recovery(server, state);
2342         }
2343 out:
2344         return ret;
2345 }
2346
2347 /*
2348  * Returns a referenced nfs4_state
2349  */
2350 static int _nfs4_do_open(struct inode *dir,
2351                         struct nfs_open_context *ctx,
2352                         int flags,
2353                         struct iattr *sattr,
2354                         struct nfs4_label *label,
2355                         int *opened)
2356 {
2357         struct nfs4_state_owner  *sp;
2358         struct nfs4_state     *state = NULL;
2359         struct nfs_server       *server = NFS_SERVER(dir);
2360         struct nfs4_opendata *opendata;
2361         struct dentry *dentry = ctx->dentry;
2362         struct rpc_cred *cred = ctx->cred;
2363         struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2364         fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2365         enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2366         struct nfs4_label *olabel = NULL;
2367         int status;
2368
2369         /* Protect against reboot recovery conflicts */
2370         status = -ENOMEM;
2371         sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2372         if (sp == NULL) {
2373                 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2374                 goto out_err;
2375         }
2376         status = nfs4_recover_expired_lease(server);
2377         if (status != 0)
2378                 goto err_put_state_owner;
2379         if (d_really_is_positive(dentry))
2380                 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2381         status = -ENOMEM;
2382         if (d_really_is_positive(dentry))
2383                 claim = NFS4_OPEN_CLAIM_FH;
2384         opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2385                         label, claim, GFP_KERNEL);
2386         if (opendata == NULL)
2387                 goto err_put_state_owner;
2388
2389         if (label) {
2390                 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2391                 if (IS_ERR(olabel)) {
2392                         status = PTR_ERR(olabel);
2393                         goto err_opendata_put;
2394                 }
2395         }
2396
2397         if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2398                 if (!opendata->f_attr.mdsthreshold) {
2399                         opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2400                         if (!opendata->f_attr.mdsthreshold)
2401                                 goto err_free_label;
2402                 }
2403                 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2404         }
2405         if (d_really_is_positive(dentry))
2406                 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2407
2408         status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2409         if (status != 0)
2410                 goto err_free_label;
2411         state = ctx->state;
2412
2413         if ((opendata->o_arg.open_flags & O_EXCL) &&
2414             (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2415                 nfs4_exclusive_attrset(opendata, sattr);
2416
2417                 nfs_fattr_init(opendata->o_res.f_attr);
2418                 status = nfs4_do_setattr(state->inode, cred,
2419                                 opendata->o_res.f_attr, sattr,
2420                                 state, label, olabel);
2421                 if (status == 0) {
2422                         nfs_setattr_update_inode(state->inode, sattr,
2423                                         opendata->o_res.f_attr);
2424                         nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2425                 }
2426         }
2427         if (opendata->file_created)
2428                 *opened |= FILE_CREATED;
2429
2430         if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2431                 *ctx_th = opendata->f_attr.mdsthreshold;
2432                 opendata->f_attr.mdsthreshold = NULL;
2433         }
2434
2435         nfs4_label_free(olabel);
2436
2437         nfs4_opendata_put(opendata);
2438         nfs4_put_state_owner(sp);
2439         return 0;
2440 err_free_label:
2441         nfs4_label_free(olabel);
2442 err_opendata_put:
2443         nfs4_opendata_put(opendata);
2444 err_put_state_owner:
2445         nfs4_put_state_owner(sp);
2446 out_err:
2447         return status;
2448 }
2449
2450
2451 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2452                                         struct nfs_open_context *ctx,
2453                                         int flags,
2454                                         struct iattr *sattr,
2455                                         struct nfs4_label *label,
2456                                         int *opened)
2457 {
2458         struct nfs_server *server = NFS_SERVER(dir);
2459         struct nfs4_exception exception = { };
2460         struct nfs4_state *res;
2461         int status;
2462
2463         do {
2464                 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2465                 res = ctx->state;
2466                 trace_nfs4_open_file(ctx, flags, status);
2467                 if (status == 0)
2468                         break;
2469                 /* NOTE: BAD_SEQID means the server and client disagree about the
2470                  * book-keeping w.r.t. state-changing operations
2471                  * (OPEN/CLOSE/LOCK/LOCKU...)
2472                  * It is actually a sign of a bug on the client or on the server.
2473                  *
2474                  * If we receive a BAD_SEQID error in the particular case of
2475                  * doing an OPEN, we assume that nfs_increment_open_seqid() will
2476                  * have unhashed the old state_owner for us, and that we can
2477                  * therefore safely retry using a new one. We should still warn
2478                  * the user though...
2479                  */
2480                 if (status == -NFS4ERR_BAD_SEQID) {
2481                         pr_warn_ratelimited("NFS: v4 server %s "
2482                                         " returned a bad sequence-id error!\n",
2483                                         NFS_SERVER(dir)->nfs_client->cl_hostname);
2484                         exception.retry = 1;
2485                         continue;
2486                 }
2487                 /*
2488                  * BAD_STATEID on OPEN means that the server cancelled our
2489                  * state before it received the OPEN_CONFIRM.
2490                  * Recover by retrying the request as per the discussion
2491                  * on Page 181 of RFC3530.
2492                  */
2493                 if (status == -NFS4ERR_BAD_STATEID) {
2494                         exception.retry = 1;
2495                         continue;
2496                 }
2497                 if (status == -EAGAIN) {
2498                         /* We must have found a delegation */
2499                         exception.retry = 1;
2500                         continue;
2501                 }
2502                 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2503                         continue;
2504                 res = ERR_PTR(nfs4_handle_exception(server,
2505                                         status, &exception));
2506         } while (exception.retry);
2507         return res;
2508 }
2509
2510 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2511                             struct nfs_fattr *fattr, struct iattr *sattr,
2512                             struct nfs4_state *state, struct nfs4_label *ilabel,
2513                             struct nfs4_label *olabel)
2514 {
2515         struct nfs_server *server = NFS_SERVER(inode);
2516         struct nfs_setattrargs  arg = {
2517                 .fh             = NFS_FH(inode),
2518                 .iap            = sattr,
2519                 .server         = server,
2520                 .bitmask = server->attr_bitmask,
2521                 .label          = ilabel,
2522         };
2523         struct nfs_setattrres  res = {
2524                 .fattr          = fattr,
2525                 .label          = olabel,
2526                 .server         = server,
2527         };
2528         struct rpc_message msg = {
2529                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2530                 .rpc_argp       = &arg,
2531                 .rpc_resp       = &res,
2532                 .rpc_cred       = cred,
2533         };
2534         unsigned long timestamp = jiffies;
2535         fmode_t fmode;
2536         bool truncate;
2537         int status;
2538
2539         arg.bitmask = nfs4_bitmask(server, ilabel);
2540         if (ilabel)
2541                 arg.bitmask = nfs4_bitmask(server, olabel);
2542
2543         nfs_fattr_init(fattr);
2544
2545         /* Servers should only apply open mode checks for file size changes */
2546         truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2547         fmode = truncate ? FMODE_WRITE : FMODE_READ;
2548
2549         if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2550                 /* Use that stateid */
2551         } else if (truncate && state != NULL) {
2552                 struct nfs_lockowner lockowner = {
2553                         .l_owner = current->files,
2554                         .l_pid = current->tgid,
2555                 };
2556                 if (!nfs4_valid_open_stateid(state))
2557                         return -EBADF;
2558                 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2559                                 &lockowner) == -EIO)
2560                         return -EBADF;
2561         } else
2562                 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2563
2564         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2565         if (status == 0 && state != NULL)
2566                 renew_lease(server, timestamp);
2567         return status;
2568 }
2569
2570 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2571                            struct nfs_fattr *fattr, struct iattr *sattr,
2572                            struct nfs4_state *state, struct nfs4_label *ilabel,
2573                            struct nfs4_label *olabel)
2574 {
2575         struct nfs_server *server = NFS_SERVER(inode);
2576         struct nfs4_exception exception = {
2577                 .state = state,
2578                 .inode = inode,
2579         };
2580         int err;
2581         do {
2582                 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
2583                 trace_nfs4_setattr(inode, err);
2584                 switch (err) {
2585                 case -NFS4ERR_OPENMODE:
2586                         if (!(sattr->ia_valid & ATTR_SIZE)) {
2587                                 pr_warn_once("NFSv4: server %s is incorrectly "
2588                                                 "applying open mode checks to "
2589                                                 "a SETATTR that is not "
2590                                                 "changing file size.\n",
2591                                                 server->nfs_client->cl_hostname);
2592                         }
2593                         if (state && !(state->state & FMODE_WRITE)) {
2594                                 err = -EBADF;
2595                                 if (sattr->ia_valid & ATTR_OPEN)
2596                                         err = -EACCES;
2597                                 goto out;
2598                         }
2599                 }
2600                 err = nfs4_handle_exception(server, err, &exception);
2601         } while (exception.retry);
2602 out:
2603         return err;
2604 }
2605
2606 struct nfs4_closedata {
2607         struct inode *inode;
2608         struct nfs4_state *state;
2609         struct nfs_closeargs arg;
2610         struct nfs_closeres res;
2611         struct nfs_fattr fattr;
2612         unsigned long timestamp;
2613         bool roc;
2614         u32 roc_barrier;
2615 };
2616
2617 static void nfs4_free_closedata(void *data)
2618 {
2619         struct nfs4_closedata *calldata = data;
2620         struct nfs4_state_owner *sp = calldata->state->owner;
2621         struct super_block *sb = calldata->state->inode->i_sb;
2622
2623         if (calldata->roc)
2624                 pnfs_roc_release(calldata->state->inode);
2625         nfs4_put_open_state(calldata->state);
2626         nfs_free_seqid(calldata->arg.seqid);
2627         nfs4_put_state_owner(sp);
2628         nfs_sb_deactive(sb);
2629         kfree(calldata);
2630 }
2631
2632 static void nfs4_close_done(struct rpc_task *task, void *data)
2633 {
2634         struct nfs4_closedata *calldata = data;
2635         struct nfs4_state *state = calldata->state;
2636         struct nfs_server *server = NFS_SERVER(calldata->inode);
2637         nfs4_stateid *res_stateid = NULL;
2638
2639         dprintk("%s: begin!\n", __func__);
2640         if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2641                 return;
2642         trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
2643         /* hmm. we are done with the inode, and in the process of freeing
2644          * the state_owner. we keep this around to process errors
2645          */
2646         switch (task->tk_status) {
2647                 case 0:
2648                         res_stateid = &calldata->res.stateid;
2649                         if (calldata->arg.fmode == 0 && calldata->roc)
2650                                 pnfs_roc_set_barrier(state->inode,
2651                                                      calldata->roc_barrier);
2652                         renew_lease(server, calldata->timestamp);
2653                         break;
2654                 case -NFS4ERR_ADMIN_REVOKED:
2655                 case -NFS4ERR_STALE_STATEID:
2656                 case -NFS4ERR_OLD_STATEID:
2657                 case -NFS4ERR_BAD_STATEID:
2658                 case -NFS4ERR_EXPIRED:
2659                         if (!nfs4_stateid_match(&calldata->arg.stateid,
2660                                                 &state->open_stateid)) {
2661                                 rpc_restart_call_prepare(task);
2662                                 goto out_release;
2663                         }
2664                         if (calldata->arg.fmode == 0)
2665                                 break;
2666                 default:
2667                         if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
2668                                 rpc_restart_call_prepare(task);
2669                                 goto out_release;
2670                         }
2671         }
2672         nfs_clear_open_stateid(state, res_stateid, calldata->arg.fmode);
2673 out_release:
2674         nfs_release_seqid(calldata->arg.seqid);
2675         nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2676         dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2677 }
2678
2679 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2680 {
2681         struct nfs4_closedata *calldata = data;
2682         struct nfs4_state *state = calldata->state;
2683         struct inode *inode = calldata->inode;
2684         bool is_rdonly, is_wronly, is_rdwr;
2685         int call_close = 0;
2686
2687         dprintk("%s: begin!\n", __func__);
2688         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2689                 goto out_wait;
2690
2691         task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2692         spin_lock(&state->owner->so_lock);
2693         is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2694         is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2695         is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
2696         nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
2697         /* Calculate the change in open mode */
2698         calldata->arg.fmode = 0;
2699         if (state->n_rdwr == 0) {
2700                 if (state->n_rdonly == 0)
2701                         call_close |= is_rdonly;
2702                 else if (is_rdonly)
2703                         calldata->arg.fmode |= FMODE_READ;
2704                 if (state->n_wronly == 0)
2705                         call_close |= is_wronly;
2706                 else if (is_wronly)
2707                         calldata->arg.fmode |= FMODE_WRITE;
2708         } else if (is_rdwr)
2709                 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2710
2711         if (calldata->arg.fmode == 0)
2712                 call_close |= is_rdwr;
2713
2714         if (!nfs4_valid_open_stateid(state))
2715                 call_close = 0;
2716         spin_unlock(&state->owner->so_lock);
2717
2718         if (!call_close) {
2719                 /* Note: exit _without_ calling nfs4_close_done */
2720                 goto out_no_action;
2721         }
2722
2723         if (calldata->arg.fmode == 0) {
2724                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2725                 if (calldata->roc &&
2726                     pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2727                         nfs_release_seqid(calldata->arg.seqid);
2728                         goto out_wait;
2729                     }
2730         }
2731         calldata->arg.share_access =
2732                 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2733                                 calldata->arg.fmode, 0);
2734
2735         nfs_fattr_init(calldata->res.fattr);
2736         calldata->timestamp = jiffies;
2737         if (nfs4_setup_sequence(NFS_SERVER(inode),
2738                                 &calldata->arg.seq_args,
2739                                 &calldata->res.seq_res,
2740                                 task) != 0)
2741                 nfs_release_seqid(calldata->arg.seqid);
2742         dprintk("%s: done!\n", __func__);
2743         return;
2744 out_no_action:
2745         task->tk_action = NULL;
2746 out_wait:
2747         nfs4_sequence_done(task, &calldata->res.seq_res);
2748 }
2749
2750 static const struct rpc_call_ops nfs4_close_ops = {
2751         .rpc_call_prepare = nfs4_close_prepare,
2752         .rpc_call_done = nfs4_close_done,
2753         .rpc_release = nfs4_free_closedata,
2754 };
2755
2756 static bool nfs4_roc(struct inode *inode)
2757 {
2758         if (!nfs_have_layout(inode))
2759                 return false;
2760         return pnfs_roc(inode);
2761 }
2762
2763 /* 
2764  * It is possible for data to be read/written from a mem-mapped file 
2765  * after the sys_close call (which hits the vfs layer as a flush).
2766  * This means that we can't safely call nfsv4 close on a file until 
2767  * the inode is cleared. This in turn means that we are not good
2768  * NFSv4 citizens - we do not indicate to the server to update the file's 
2769  * share state even when we are done with one of the three share 
2770  * stateid's in the inode.
2771  *
2772  * NOTE: Caller must be holding the sp->so_owner semaphore!
2773  */
2774 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2775 {
2776         struct nfs_server *server = NFS_SERVER(state->inode);
2777         struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
2778         struct nfs4_closedata *calldata;
2779         struct nfs4_state_owner *sp = state->owner;
2780         struct rpc_task *task;
2781         struct rpc_message msg = {
2782                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2783                 .rpc_cred = state->owner->so_cred,
2784         };
2785         struct rpc_task_setup task_setup_data = {
2786                 .rpc_client = server->client,
2787                 .rpc_message = &msg,
2788                 .callback_ops = &nfs4_close_ops,
2789                 .workqueue = nfsiod_workqueue,
2790                 .flags = RPC_TASK_ASYNC,
2791         };
2792         int status = -ENOMEM;
2793
2794         nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2795                 &task_setup_data.rpc_client, &msg);
2796
2797         calldata = kzalloc(sizeof(*calldata), gfp_mask);
2798         if (calldata == NULL)
2799                 goto out;
2800         nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2801         calldata->inode = state->inode;
2802         calldata->state = state;
2803         calldata->arg.fh = NFS_FH(state->inode);
2804         /* Serialization for the sequence id */
2805         alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
2806         calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
2807         if (IS_ERR(calldata->arg.seqid))
2808                 goto out_free_calldata;
2809         calldata->arg.fmode = 0;
2810         calldata->arg.bitmask = server->cache_consistency_bitmask;
2811         calldata->res.fattr = &calldata->fattr;
2812         calldata->res.seqid = calldata->arg.seqid;
2813         calldata->res.server = server;
2814         calldata->roc = nfs4_roc(state->inode);
2815         nfs_sb_active(calldata->inode->i_sb);
2816
2817         msg.rpc_argp = &calldata->arg;
2818         msg.rpc_resp = &calldata->res;
2819         task_setup_data.callback_data = calldata;
2820         task = rpc_run_task(&task_setup_data);
2821         if (IS_ERR(task))
2822                 return PTR_ERR(task);
2823         status = 0;
2824         if (wait)
2825                 status = rpc_wait_for_completion_task(task);
2826         rpc_put_task(task);
2827         return status;
2828 out_free_calldata:
2829         kfree(calldata);
2830 out:
2831         nfs4_put_open_state(state);
2832         nfs4_put_state_owner(sp);
2833         return status;
2834 }
2835
2836 static struct inode *
2837 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2838                 int open_flags, struct iattr *attr, int *opened)
2839 {
2840         struct nfs4_state *state;
2841         struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2842
2843         label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
2844
2845         /* Protect against concurrent sillydeletes */
2846         state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
2847
2848         nfs4_label_release_security(label);
2849
2850         if (IS_ERR(state))
2851                 return ERR_CAST(state);
2852         return state->inode;
2853 }
2854
2855 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2856 {
2857         if (ctx->state == NULL)
2858                 return;
2859         if (is_sync)
2860                 nfs4_close_sync(ctx->state, ctx->mode);
2861         else
2862                 nfs4_close_state(ctx->state, ctx->mode);
2863 }
2864
2865 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2866 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
2867 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
2868
2869 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2870 {
2871         struct nfs4_server_caps_arg args = {
2872                 .fhandle = fhandle,
2873         };
2874         struct nfs4_server_caps_res res = {};
2875         struct rpc_message msg = {
2876                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2877                 .rpc_argp = &args,
2878                 .rpc_resp = &res,
2879         };
2880         int status;
2881
2882         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2883         if (status == 0) {
2884                 /* Sanity check the server answers */
2885                 switch (server->nfs_client->cl_minorversion) {
2886                 case 0:
2887                         res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2888                         res.attr_bitmask[2] = 0;
2889                         break;
2890                 case 1:
2891                         res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2892                         break;
2893                 case 2:
2894                         res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2895                 }
2896                 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2897                 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2898                                 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2899                                 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2900                                 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2901                                 NFS_CAP_CTIME|NFS_CAP_MTIME|
2902                                 NFS_CAP_SECURITY_LABEL);
2903                 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2904                                 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2905                         server->caps |= NFS_CAP_ACLS;
2906                 if (res.has_links != 0)
2907                         server->caps |= NFS_CAP_HARDLINKS;
2908                 if (res.has_symlinks != 0)
2909                         server->caps |= NFS_CAP_SYMLINKS;
2910                 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2911                         server->caps |= NFS_CAP_FILEID;
2912                 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2913                         server->caps |= NFS_CAP_MODE;
2914                 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2915                         server->caps |= NFS_CAP_NLINK;
2916                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2917                         server->caps |= NFS_CAP_OWNER;
2918                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2919                         server->caps |= NFS_CAP_OWNER_GROUP;
2920                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2921                         server->caps |= NFS_CAP_ATIME;
2922                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2923                         server->caps |= NFS_CAP_CTIME;
2924                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2925                         server->caps |= NFS_CAP_MTIME;
2926 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
2927                 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2928                         server->caps |= NFS_CAP_SECURITY_LABEL;
2929 #endif
2930                 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2931                                 sizeof(server->attr_bitmask));
2932                 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2933
2934                 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2935                 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2936                 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2937                 server->cache_consistency_bitmask[2] = 0;
2938                 server->acl_bitmask = res.acl_bitmask;
2939                 server->fh_expire_type = res.fh_expire_type;
2940         }
2941
2942         return status;
2943 }
2944
2945 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2946 {
2947         struct nfs4_exception exception = { };
2948         int err;
2949         do {
2950                 err = nfs4_handle_exception(server,
2951                                 _nfs4_server_capabilities(server, fhandle),
2952                                 &exception);
2953         } while (exception.retry);
2954         return err;
2955 }
2956
2957 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2958                 struct nfs_fsinfo *info)
2959 {
2960         u32 bitmask[3];
2961         struct nfs4_lookup_root_arg args = {
2962                 .bitmask = bitmask,
2963         };
2964         struct nfs4_lookup_res res = {
2965                 .server = server,
2966                 .fattr = info->fattr,
2967                 .fh = fhandle,
2968         };
2969         struct rpc_message msg = {
2970                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2971                 .rpc_argp = &args,
2972                 .rpc_resp = &res,
2973         };
2974
2975         bitmask[0] = nfs4_fattr_bitmap[0];
2976         bitmask[1] = nfs4_fattr_bitmap[1];
2977         /*
2978          * Process the label in the upcoming getfattr
2979          */
2980         bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2981
2982         nfs_fattr_init(info->fattr);
2983         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2984 }
2985
2986 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2987                 struct nfs_fsinfo *info)
2988 {
2989         struct nfs4_exception exception = { };
2990         int err;
2991         do {
2992                 err = _nfs4_lookup_root(server, fhandle, info);
2993                 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
2994                 switch (err) {
2995                 case 0:
2996                 case -NFS4ERR_WRONGSEC:
2997                         goto out;
2998                 default:
2999                         err = nfs4_handle_exception(server, err, &exception);
3000                 }
3001         } while (exception.retry);
3002 out:
3003         return err;
3004 }
3005
3006 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3007                                 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3008 {
3009         struct rpc_auth_create_args auth_args = {
3010                 .pseudoflavor = flavor,
3011         };
3012         struct rpc_auth *auth;
3013         int ret;
3014
3015         auth = rpcauth_create(&auth_args, server->client);
3016         if (IS_ERR(auth)) {
3017                 ret = -EACCES;
3018                 goto out;
3019         }
3020         ret = nfs4_lookup_root(server, fhandle, info);
3021 out:
3022         return ret;
3023 }
3024
3025 /*
3026  * Retry pseudoroot lookup with various security flavors.  We do this when:
3027  *
3028  *   NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3029  *   NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3030  *
3031  * Returns zero on success, or a negative NFS4ERR value, or a
3032  * negative errno value.
3033  */
3034 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3035                               struct nfs_fsinfo *info)
3036 {
3037         /* Per 3530bis 15.33.5 */
3038         static const rpc_authflavor_t flav_array[] = {
3039                 RPC_AUTH_GSS_KRB5P,
3040                 RPC_AUTH_GSS_KRB5I,
3041                 RPC_AUTH_GSS_KRB5,
3042                 RPC_AUTH_UNIX,                  /* courtesy */
3043                 RPC_AUTH_NULL,
3044         };
3045         int status = -EPERM;
3046         size_t i;
3047
3048         if (server->auth_info.flavor_len > 0) {
3049                 /* try each flavor specified by user */
3050                 for (i = 0; i < server->auth_info.flavor_len; i++) {
3051                         status = nfs4_lookup_root_sec(server, fhandle, info,
3052                                                 server->auth_info.flavors[i]);
3053                         if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3054                                 continue;
3055                         break;
3056                 }
3057         } else {
3058                 /* no flavors specified by user, try default list */
3059                 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3060                         status = nfs4_lookup_root_sec(server, fhandle, info,
3061                                                       flav_array[i]);
3062                         if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3063                                 continue;
3064                         break;
3065                 }
3066         }
3067
3068         /*
3069          * -EACCESS could mean that the user doesn't have correct permissions
3070          * to access the mount.  It could also mean that we tried to mount
3071          * with a gss auth flavor, but rpc.gssd isn't running.  Either way,
3072          * existing mount programs don't handle -EACCES very well so it should
3073          * be mapped to -EPERM instead.
3074          */
3075         if (status == -EACCES)
3076                 status = -EPERM;
3077         return status;
3078 }
3079
3080 static int nfs4_do_find_root_sec(struct nfs_server *server,
3081                 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3082 {
3083         int mv = server->nfs_client->cl_minorversion;
3084         return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3085 }
3086
3087 /**
3088  * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3089  * @server: initialized nfs_server handle
3090  * @fhandle: we fill in the pseudo-fs root file handle
3091  * @info: we fill in an FSINFO struct
3092  * @auth_probe: probe the auth flavours
3093  *
3094  * Returns zero on success, or a negative errno.
3095  */
3096 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3097                          struct nfs_fsinfo *info,
3098                          bool auth_probe)
3099 {
3100         int status = 0;
3101
3102         if (!auth_probe)
3103                 status = nfs4_lookup_root(server, fhandle, info);
3104
3105         if (auth_probe || status == NFS4ERR_WRONGSEC)
3106                 status = nfs4_do_find_root_sec(server, fhandle, info);
3107
3108         if (status == 0)
3109                 status = nfs4_server_capabilities(server, fhandle);
3110         if (status == 0)
3111                 status = nfs4_do_fsinfo(server, fhandle, info);
3112
3113         return nfs4_map_errors(status);
3114 }
3115
3116 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3117                               struct nfs_fsinfo *info)
3118 {
3119         int error;
3120         struct nfs_fattr *fattr = info->fattr;
3121         struct nfs4_label *label = NULL;
3122
3123         error = nfs4_server_capabilities(server, mntfh);
3124         if (error < 0) {
3125                 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3126                 return error;
3127         }
3128
3129         label = nfs4_label_alloc(server, GFP_KERNEL);
3130         if (IS_ERR(label))
3131                 return PTR_ERR(label);
3132
3133         error = nfs4_proc_getattr(server, mntfh, fattr, label);
3134         if (error < 0) {
3135                 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3136                 goto err_free_label;
3137         }
3138
3139         if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3140             !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3141                 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3142
3143 err_free_label:
3144         nfs4_label_free(label);
3145
3146         return error;
3147 }
3148
3149 /*
3150  * Get locations and (maybe) other attributes of a referral.
3151  * Note that we'll actually follow the referral later when
3152  * we detect fsid mismatch in inode revalidation
3153  */
3154 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3155                              const struct qstr *name, struct nfs_fattr *fattr,
3156                              struct nfs_fh *fhandle)
3157 {
3158         int status = -ENOMEM;
3159         struct page *page = NULL;
3160         struct nfs4_fs_locations *locations = NULL;
3161
3162         page = alloc_page(GFP_KERNEL);
3163         if (page == NULL)
3164                 goto out;
3165         locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3166         if (locations == NULL)
3167                 goto out;
3168
3169         status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3170         if (status != 0)
3171                 goto out;
3172
3173         /*
3174          * If the fsid didn't change, this is a migration event, not a
3175          * referral.  Cause us to drop into the exception handler, which
3176          * will kick off migration recovery.
3177          */
3178         if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3179                 dprintk("%s: server did not return a different fsid for"
3180                         " a referral at %s\n", __func__, name->name);
3181                 status = -NFS4ERR_MOVED;
3182                 goto out;
3183         }
3184         /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3185         nfs_fixup_referral_attributes(&locations->fattr);
3186
3187         /* replace the lookup nfs_fattr with the locations nfs_fattr */
3188         memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3189         memset(fhandle, 0, sizeof(struct nfs_fh));
3190 out:
3191         if (page)
3192                 __free_page(page);
3193         kfree(locations);
3194         return status;
3195 }
3196
3197 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3198                                 struct nfs_fattr *fattr, struct nfs4_label *label)
3199 {
3200         struct nfs4_getattr_arg args = {
3201                 .fh = fhandle,
3202                 .bitmask = server->attr_bitmask,
3203         };
3204         struct nfs4_getattr_res res = {
3205                 .fattr = fattr,
3206                 .label = label,
3207                 .server = server,
3208         };
3209         struct rpc_message msg = {
3210                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3211                 .rpc_argp = &args,
3212                 .rpc_resp = &res,
3213         };
3214
3215         args.bitmask = nfs4_bitmask(server, label);
3216
3217         nfs_fattr_init(fattr);
3218         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3219 }
3220
3221 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3222                                 struct nfs_fattr *fattr, struct nfs4_label *label)
3223 {
3224         struct nfs4_exception exception = { };
3225         int err;
3226         do {
3227                 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3228                 trace_nfs4_getattr(server, fhandle, fattr, err);
3229                 err = nfs4_handle_exception(server, err,
3230                                 &exception);
3231         } while (exception.retry);
3232         return err;
3233 }
3234
3235 /* 
3236  * The file is not closed if it is opened due to the a request to change
3237  * the size of the file. The open call will not be needed once the
3238  * VFS layer lookup-intents are implemented.
3239  *
3240  * Close is called when the inode is destroyed.
3241  * If we haven't opened the file for O_WRONLY, we
3242  * need to in the size_change case to obtain a stateid.
3243  *
3244  * Got race?
3245  * Because OPEN is always done by name in nfsv4, it is
3246  * possible that we opened a different file by the same
3247  * name.  We can recognize this race condition, but we
3248  * can't do anything about it besides returning an error.
3249  *
3250  * This will be fixed with VFS changes (lookup-intent).
3251  */
3252 static int
3253 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3254                   struct iattr *sattr)
3255 {
3256         struct inode *inode = d_inode(dentry);
3257         struct rpc_cred *cred = NULL;
3258         struct nfs4_state *state = NULL;
3259         struct nfs4_label *label = NULL;
3260         int status;
3261
3262         if (pnfs_ld_layoutret_on_setattr(inode) &&
3263             sattr->ia_valid & ATTR_SIZE &&
3264             sattr->ia_size < i_size_read(inode))
3265                 pnfs_commit_and_return_layout(inode);
3266
3267         nfs_fattr_init(fattr);
3268         
3269         /* Deal with open(O_TRUNC) */
3270         if (sattr->ia_valid & ATTR_OPEN)
3271                 sattr->ia