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