blob: 184d57a5098d920eb11ef0a19c63e85fd9eeeaf1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Trond Myklebust4ce79712005-06-22 17:16:21 +000058#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050060#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050061#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040062#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040063#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040064#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040065#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020076/* file attributes which can be mapped to nfs attributes */
77#define NFS4_VALID_ATTRS (ATTR_MODE \
78 | ATTR_UID \
79 | ATTR_GID \
80 | ATTR_SIZE \
81 | ATTR_ATIME \
82 | ATTR_MTIME \
83 | ATTR_CTIME \
84 | ATTR_ATIME_SET \
85 | ATTR_MTIME_SET)
86
Trond Myklebustcdd4e682006-01-03 09:55:12 +010087struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010088static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080089static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050091static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040092static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
93static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040094static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
95 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110096 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040097 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040098#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040099static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
100 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400101static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
102 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400103#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400104
105#ifdef CONFIG_NFS_V4_SECURITY_LABEL
106static inline struct nfs4_label *
107nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
108 struct iattr *sattr, struct nfs4_label *label)
109{
110 int err;
111
112 if (label == NULL)
113 return NULL;
114
115 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
116 return NULL;
117
David Quigleyaa9c2662013-05-22 12:50:44 -0400118 err = security_dentry_init_security(dentry, sattr->ia_mode,
119 &dentry->d_name, (void **)&label->label, &label->len);
120 if (err == 0)
121 return label;
122
123 return NULL;
124}
125static inline void
126nfs4_label_release_security(struct nfs4_label *label)
127{
128 if (label)
129 security_release_secctx(label->label, label->len);
130}
131static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
132{
133 if (label)
134 return server->attr_bitmask;
135
136 return server->attr_bitmask_nl;
137}
138#else
139static inline struct nfs4_label *
140nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
141 struct iattr *sattr, struct nfs4_label *l)
142{ return NULL; }
143static inline void
144nfs4_label_release_security(struct nfs4_label *label)
145{ return; }
146static inline u32 *
147nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
148{ return server->attr_bitmask; }
149#endif
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500152static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Trond Myklebust52567b02009-10-23 14:46:42 -0400154 if (err >= -1000)
155 return err;
156 switch (err) {
157 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500158 case -NFS4ERR_LAYOUTTRYLATER:
159 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400160 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000161 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400162 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000163 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800164 case -NFS4ERR_BADOWNER:
165 case -NFS4ERR_BADNAME:
166 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400167 case -NFS4ERR_SHARE_DENIED:
168 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400169 case -NFS4ERR_MINOR_VERS_MISMATCH:
170 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400171 case -NFS4ERR_FILE_OPEN:
172 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400173 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700175 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400176 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400178 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181/*
182 * This is our standard bitmap for GETATTR requests.
183 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400184const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 FATTR4_WORD0_TYPE
186 | FATTR4_WORD0_CHANGE
187 | FATTR4_WORD0_SIZE
188 | FATTR4_WORD0_FSID
189 | FATTR4_WORD0_FILEID,
190 FATTR4_WORD1_MODE
191 | FATTR4_WORD1_NUMLINKS
192 | FATTR4_WORD1_OWNER
193 | FATTR4_WORD1_OWNER_GROUP
194 | FATTR4_WORD1_RAWDEV
195 | FATTR4_WORD1_SPACE_USED
196 | FATTR4_WORD1_TIME_ACCESS
197 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400198 | FATTR4_WORD1_TIME_MODIFY
199 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400200#ifdef CONFIG_NFS_V4_SECURITY_LABEL
201 FATTR4_WORD2_SECURITY_LABEL
202#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203};
204
Trond Myklebust1549210f2012-06-05 09:16:47 -0400205static const u32 nfs4_pnfs_open_bitmap[3] = {
206 FATTR4_WORD0_TYPE
207 | FATTR4_WORD0_CHANGE
208 | FATTR4_WORD0_SIZE
209 | FATTR4_WORD0_FSID
210 | FATTR4_WORD0_FILEID,
211 FATTR4_WORD1_MODE
212 | FATTR4_WORD1_NUMLINKS
213 | FATTR4_WORD1_OWNER
214 | FATTR4_WORD1_OWNER_GROUP
215 | FATTR4_WORD1_RAWDEV
216 | FATTR4_WORD1_SPACE_USED
217 | FATTR4_WORD1_TIME_ACCESS
218 | FATTR4_WORD1_TIME_METADATA
219 | FATTR4_WORD1_TIME_MODIFY,
220 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500221#ifdef CONFIG_NFS_V4_SECURITY_LABEL
222 | FATTR4_WORD2_SECURITY_LABEL
223#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400224};
225
Andy Adamsone23008e2012-10-02 21:07:32 -0400226static const u32 nfs4_open_noattr_bitmap[3] = {
227 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400228 | FATTR4_WORD0_FILEID,
229};
230
David Quigleya09df2c2013-05-22 12:50:41 -0400231const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 FATTR4_WORD0_FILES_AVAIL
233 | FATTR4_WORD0_FILES_FREE
234 | FATTR4_WORD0_FILES_TOTAL,
235 FATTR4_WORD1_SPACE_AVAIL
236 | FATTR4_WORD1_SPACE_FREE
237 | FATTR4_WORD1_SPACE_TOTAL
238};
239
David Quigleya09df2c2013-05-22 12:50:41 -0400240const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 FATTR4_WORD0_MAXLINK
242 | FATTR4_WORD0_MAXNAME,
243 0
244};
245
Fred Isamandae100c2011-07-30 20:52:37 -0400246const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 | FATTR4_WORD0_MAXREAD
248 | FATTR4_WORD0_MAXWRITE
249 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700250 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400251 | FATTR4_WORD1_FS_LAYOUT_TYPES,
252 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800253 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254};
255
David Quigleya09df2c2013-05-22 12:50:41 -0400256const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400257 FATTR4_WORD0_TYPE
258 | FATTR4_WORD0_CHANGE
259 | FATTR4_WORD0_SIZE
260 | FATTR4_WORD0_FSID
261 | FATTR4_WORD0_FILEID
262 | FATTR4_WORD0_FS_LOCATIONS,
263 FATTR4_WORD1_MODE
264 | FATTR4_WORD1_NUMLINKS
265 | FATTR4_WORD1_OWNER
266 | FATTR4_WORD1_OWNER_GROUP
267 | FATTR4_WORD1_RAWDEV
268 | FATTR4_WORD1_SPACE_USED
269 | FATTR4_WORD1_TIME_ACCESS
270 | FATTR4_WORD1_TIME_METADATA
271 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400272 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400273};
274
Al Virobc4785c2006-10-19 23:28:51 -0700275static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 struct nfs4_readdir_arg *readdir)
277{
Al Viro0dbb4c62006-10-19 23:28:49 -0700278 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000281 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
283 return;
284 }
285
286 readdir->cookie = 0;
287 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
288 if (cookie == 2)
289 return;
290
291 /*
292 * NFSv4 servers do not return entries for '.' and '..'
293 * Therefore, we fake these entries here. We let '.'
294 * have cookie 0 and '..' have cookie 1. Note that
295 * when talking to the server, we always send cookie 0
296 * instead of 1 or 2.
297 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800298 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 if (cookie == 0) {
301 *p++ = xdr_one; /* next */
302 *p++ = xdr_zero; /* cookie, first word */
303 *p++ = xdr_one; /* cookie, second word */
304 *p++ = xdr_one; /* entry len */
305 memcpy(p, ".\0\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 */
David Howells2b0143b2015-03-17 22:25:59 +0000310 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312
313 *p++ = xdr_one; /* next */
314 *p++ = xdr_zero; /* cookie, first word */
315 *p++ = xdr_two; /* cookie, second word */
316 *p++ = xdr_two; /* entry len */
317 memcpy(p, "..\0\0", 4); /* entry */
318 p++;
319 *p++ = xdr_one; /* bitmap length */
320 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
321 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000322 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 readdir->pgbase = (char *)p - (char *)start;
325 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800326 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Trond Myklebust26d36302016-09-22 13:39:05 -0400329static void nfs4_test_and_free_stateid(struct nfs_server *server,
330 nfs4_stateid *stateid,
331 struct rpc_cred *cred)
332{
333 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
334
335 ops->test_and_free_expired(server, stateid, cred);
336}
337
338static void __nfs4_free_revoked_stateid(struct nfs_server *server,
339 nfs4_stateid *stateid,
340 struct rpc_cred *cred)
341{
342 stateid->type = NFS4_REVOKED_STATEID_TYPE;
343 nfs4_test_and_free_stateid(server, stateid, cred);
344}
345
346static void nfs4_free_revoked_stateid(struct nfs_server *server,
347 const nfs4_stateid *stateid,
348 struct rpc_cred *cred)
349{
350 nfs4_stateid tmp;
351
352 nfs4_stateid_copy(&tmp, stateid);
353 __nfs4_free_revoked_stateid(server, &tmp, cred);
354}
355
NeilBrown8478eaa2014-09-18 16:09:27 +1000356static long nfs4_update_delay(long *timeout)
357{
358 long ret;
359 if (!timeout)
360 return NFS4_POLL_RETRY_MAX;
361 if (*timeout <= 0)
362 *timeout = NFS4_POLL_RETRY_MIN;
363 if (*timeout > NFS4_POLL_RETRY_MAX)
364 *timeout = NFS4_POLL_RETRY_MAX;
365 ret = *timeout;
366 *timeout <<= 1;
367 return ret;
368}
369
Trond Myklebust65de8722008-12-23 15:21:44 -0500370static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
371{
372 int res = 0;
373
374 might_sleep();
375
NeilBrown8478eaa2014-09-18 16:09:27 +1000376 freezable_schedule_timeout_killable_unsafe(
377 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500378 if (fatal_signal_pending(current))
379 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500380 return res;
381}
382
383/* This is the error handling routine for processes that are allowed
384 * to sleep.
385 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400386static int nfs4_do_handle_exception(struct nfs_server *server,
387 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500388{
389 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500390 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400391 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500392 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500393 int ret = errorcode;
394
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400395 exception->delay = 0;
396 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500397 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400398
399 if (stateid == NULL && state != NULL)
400 stateid = &state->stateid;
401
Trond Myklebust65de8722008-12-23 15:21:44 -0500402 switch(errorcode) {
403 case 0:
404 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400405 case -NFS4ERR_DELEG_REVOKED:
406 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400407 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400408 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400409 if (inode != NULL && stateid != NULL) {
410 nfs_inode_find_state_and_recover(inode,
411 stateid);
412 goto wait_on_recovery;
413 }
414 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400415 if (inode) {
416 int err;
417
418 err = nfs_async_inode_return_delegation(inode,
419 stateid);
420 if (err == 0)
421 goto wait_on_recovery;
422 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
423 exception->retry = 1;
424 break;
425 }
426 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500427 if (state == NULL)
428 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400429 ret = nfs4_schedule_stateid_recovery(server, state);
430 if (ret < 0)
431 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500432 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500433 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500434 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500435 nfs4_schedule_lease_recovery(clp);
436 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400437 case -NFS4ERR_MOVED:
438 ret = nfs4_schedule_migration_recovery(server);
439 if (ret < 0)
440 break;
441 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400442 case -NFS4ERR_LEASE_MOVED:
443 nfs4_schedule_lease_moved_recovery(clp);
444 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500445#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400446 case -NFS4ERR_BADSESSION:
447 case -NFS4ERR_BADSLOT:
448 case -NFS4ERR_BAD_HIGH_SLOT:
449 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
450 case -NFS4ERR_DEADSESSION:
451 case -NFS4ERR_SEQ_FALSE_RETRY:
452 case -NFS4ERR_SEQ_MISORDERED:
453 dprintk("%s ERROR: %d Reset session\n", __func__,
454 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400455 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400456 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500457#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500458 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500459 if (exception->timeout > HZ) {
460 /* We have retried a decent amount, time to
461 * fail
462 */
463 ret = -EBUSY;
464 break;
465 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500466 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400467 nfs_inc_server_stats(server, NFSIOS_DELAY);
468 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400469 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400470 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400471 exception->delay = 1;
472 return 0;
473
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400474 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500475 case -NFS4ERR_OLD_STATEID:
476 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800477 break;
478 case -NFS4ERR_BADOWNER:
479 /* The following works around a Linux server bug! */
480 case -NFS4ERR_BADNAME:
481 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
482 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
483 exception->retry = 1;
484 printk(KERN_WARNING "NFS: v4 server %s "
485 "does not accept raw "
486 "uid/gids. "
487 "Reenabling the idmapper.\n",
488 server->nfs_client->cl_hostname);
489 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500490 }
491 /* We failed to handle the error */
492 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500493wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400494 exception->recovering = 1;
495 return 0;
496}
497
498/* This is the error handling routine for processes that are allowed
499 * to sleep.
500 */
501int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
502{
503 struct nfs_client *clp = server->nfs_client;
504 int ret;
505
506 ret = nfs4_do_handle_exception(server, errorcode, exception);
507 if (exception->delay) {
508 ret = nfs4_delay(server->client, &exception->timeout);
509 goto out_retry;
510 }
511 if (exception->recovering) {
512 ret = nfs4_wait_clnt_recover(clp);
513 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
514 return -EIO;
515 goto out_retry;
516 }
517 return ret;
518out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500519 if (ret == 0)
520 exception->retry = 1;
521 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500522}
523
Trond Myklebust037fc982015-09-20 15:51:00 -0400524static int
525nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
526 int errorcode, struct nfs4_exception *exception)
527{
528 struct nfs_client *clp = server->nfs_client;
529 int ret;
530
531 ret = nfs4_do_handle_exception(server, errorcode, exception);
532 if (exception->delay) {
533 rpc_delay(task, nfs4_update_delay(&exception->timeout));
534 goto out_retry;
535 }
536 if (exception->recovering) {
537 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
538 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
539 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
540 goto out_retry;
541 }
542 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
543 ret = -EIO;
544 return ret;
545out_retry:
546 if (ret == 0)
547 exception->retry = 1;
548 return ret;
549}
550
551static int
552nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
553 struct nfs4_state *state, long *timeout)
554{
555 struct nfs4_exception exception = {
556 .state = state,
557 };
558
559 if (task->tk_status >= 0)
560 return 0;
561 if (timeout)
562 exception.timeout = *timeout;
563 task->tk_status = nfs4_async_handle_exception(task, server,
564 task->tk_status,
565 &exception);
566 if (exception.delay && timeout)
567 *timeout = exception.timeout;
568 if (exception.retry)
569 return -EAGAIN;
570 return 0;
571}
572
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400573/*
574 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
575 * or 'false' otherwise.
576 */
577static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
578{
579 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500580 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400581}
Trond Myklebust65de8722008-12-23 15:21:44 -0500582
Trond Myklebust452e9352010-07-31 14:29:06 -0400583static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 spin_lock(&clp->cl_lock);
586 if (time_before(clp->cl_last_renewal,timestamp))
587 clp->cl_last_renewal = timestamp;
588 spin_unlock(&clp->cl_lock);
589}
590
Trond Myklebust452e9352010-07-31 14:29:06 -0400591static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
592{
Trond Myklebustbe824162015-07-05 14:50:46 -0400593 struct nfs_client *clp = server->nfs_client;
594
595 if (!nfs4_has_session(clp))
596 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400597}
598
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400599struct nfs4_call_sync_data {
600 const struct nfs_server *seq_server;
601 struct nfs4_sequence_args *seq_args;
602 struct nfs4_sequence_res *seq_res;
603};
604
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800605void nfs4_init_sequence(struct nfs4_sequence_args *args,
606 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400607{
608 args->sa_slot = NULL;
609 args->sa_cache_this = cache_reply;
610 args->sa_privileged = 0;
611
612 res->sr_slot = NULL;
613}
614
615static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
616{
617 args->sa_privileged = 1;
618}
619
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400620static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400621{
622 struct nfs4_slot *slot = res->sr_slot;
623 struct nfs4_slot_table *tbl;
624
Chuck Lever3bd23842013-08-09 12:49:19 -0400625 tbl = slot->table;
626 spin_lock(&tbl->slot_tbl_lock);
627 if (!nfs41_wake_and_assign_slot(tbl, slot))
628 nfs4_free_slot(tbl, slot);
629 spin_unlock(&tbl->slot_tbl_lock);
630
631 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400632}
633
634static int nfs40_sequence_done(struct rpc_task *task,
635 struct nfs4_sequence_res *res)
636{
637 if (res->sr_slot != NULL)
638 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400639 return 1;
640}
641
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400642#if defined(CONFIG_NFS_V4_1)
643
Trond Myklebustd185a332010-06-16 09:52:25 -0400644static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400645{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500646 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400647 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500648 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500649 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400650
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500651 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500652 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500653
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400654 /* Bump the slot sequence number */
655 if (slot->seq_done)
656 slot->seq_nr++;
657 slot->seq_done = 0;
658
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500659 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500660 /* Be nice to the server: try to ensure that the last transmitted
661 * value for highest_user_slotid <= target_highest_slotid
662 */
663 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
664 send_new_highest_used_slotid = true;
665
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500666 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500667 send_new_highest_used_slotid = false;
668 goto out_unlock;
669 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500670 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500671
672 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
673 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500674out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500675 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400676 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500677 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400678 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400679 if (waitqueue_active(&tbl->slot_waitq))
680 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400681}
682
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400683static int nfs41_sequence_process(struct rpc_task *task,
684 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400685{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500686 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500687 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400688 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500689 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500690 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400691
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500692 if (slot == NULL)
693 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400694 /* don't increment the sequence number if the task wasn't sent */
695 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400696 goto out;
697
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500698 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500699
Trond Myklebustac20d162012-12-15 15:36:07 -0500700 if (slot->interrupted) {
701 slot->interrupted = 0;
702 interrupted = true;
703 }
704
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400705 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500706 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400707 switch (res->sr_status) {
708 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400709 /* If previous op on slot was interrupted and we reused
710 * the seq# and got a reply from the cache, then retry
711 */
712 if (task->tk_status == -EREMOTEIO && interrupted) {
713 ++slot->seq_nr;
714 goto retry_nowait;
715 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400716 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400717 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500718 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500719 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500720 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400721 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
722 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500723 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400724 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500725 case 1:
726 /*
727 * sr_status remains 1 if an RPC level error occurred.
728 * The server may or may not have processed the sequence
729 * operation..
730 * Mark the slot as having hosted an interrupted RPC call.
731 */
732 slot->interrupted = 1;
733 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400734 case -NFS4ERR_DELAY:
735 /* The server detected a resend of the RPC call and
736 * returned NFS4ERR_DELAY as per Section 2.10.6.2
737 * of RFC5661.
738 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500739 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400740 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500741 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500742 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400743 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500744 case -NFS4ERR_BADSLOT:
745 /*
746 * The slot id we used was probably retired. Try again
747 * using a different slot id.
748 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500749 goto retry_nowait;
750 case -NFS4ERR_SEQ_MISORDERED:
751 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500752 * Was the last operation on this sequence interrupted?
753 * If so, retry after bumping the sequence number.
754 */
755 if (interrupted) {
756 ++slot->seq_nr;
757 goto retry_nowait;
758 }
759 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500760 * Could this slot have been previously retired?
761 * If so, then the server may be expecting seq_nr = 1!
762 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500763 if (slot->seq_nr != 1) {
764 slot->seq_nr = 1;
765 goto retry_nowait;
766 }
767 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500768 case -NFS4ERR_SEQ_FALSE_RETRY:
769 ++slot->seq_nr;
770 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400771 default:
772 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400773 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400774 }
775out:
776 /* The session may be reset by one of the error handlers. */
777 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500778out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500779 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500780retry_nowait:
781 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400782 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500783 task->tk_status = 0;
784 ret = 0;
785 }
786 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400787out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400788 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400789 goto out;
790 rpc_delay(task, NFS4_POLL_RETRY_MAX);
791 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400792}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400793
794int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
795{
796 if (!nfs41_sequence_process(task, res))
797 return 0;
798 if (res->sr_slot != NULL)
799 nfs41_sequence_free_slot(res);
800 return 1;
801
802}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500803EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400804
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400805static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
806{
807 if (res->sr_slot == NULL)
808 return 1;
809 if (res->sr_slot->table->session != NULL)
810 return nfs41_sequence_process(task, res);
811 return nfs40_sequence_done(task, res);
812}
813
814static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
815{
816 if (res->sr_slot != NULL) {
817 if (res->sr_slot->table->session != NULL)
818 nfs41_sequence_free_slot(res);
819 else
820 nfs40_sequence_free_slot(res);
821 }
822}
823
Peng Tao2c4b1312014-06-11 05:24:16 +0800824int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400825{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500826 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400827 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400828 if (!res->sr_slot->table->session)
829 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400830 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400831}
Peng Tao2c4b1312014-06-11 05:24:16 +0800832EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400833
Andy Adamsonce5039c2009-04-01 09:22:13 -0400834static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
835{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400836 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400837
Trond Myklebust035168a2010-06-16 09:52:26 -0400838 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
839
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500840 nfs4_setup_sequence(data->seq_server->nfs_client,
841 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400842}
843
Andy Adamson69ab40c2009-04-01 09:22:19 -0400844static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
845{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400846 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400847
Trond Myklebust14516c32010-07-31 14:29:06 -0400848 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400849}
850
Trond Myklebust17280172012-03-11 13:11:00 -0400851static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400852 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400853 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400854};
855
Chuck Lever3bd23842013-08-09 12:49:19 -0400856#else /* !CONFIG_NFS_V4_1 */
857
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400858static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
859{
860 return nfs40_sequence_done(task, res);
861}
862
863static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
864{
865 if (res->sr_slot != NULL)
866 nfs40_sequence_free_slot(res);
867}
868
Peng Tao2c4b1312014-06-11 05:24:16 +0800869int nfs4_sequence_done(struct rpc_task *task,
870 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400871{
Chuck Lever3bd23842013-08-09 12:49:19 -0400872 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400873}
Peng Tao2c4b1312014-06-11 05:24:16 +0800874EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400875
876#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400877
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500878int nfs4_setup_sequence(const struct nfs_client *client,
879 struct nfs4_sequence_args *args,
880 struct nfs4_sequence_res *res,
881 struct rpc_task *task)
882{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500883 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500884 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500885 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500886
Anna Schumaker9dd91072017-01-10 12:01:46 -0500887 /* slot already allocated? */
888 if (res->sr_slot != NULL)
889 goto out_start;
890
Anna Schumaker76ee0352017-01-10 16:49:31 -0500891 if (session) {
892 tbl = &session->fc_slot_table;
893 task->tk_timeout = 0;
894 }
895
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500896 spin_lock(&tbl->slot_tbl_lock);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500897 /* The state manager will wait until the slot table is empty */
898 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
899 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500900
Anna Schumaker3d358082017-01-11 10:54:04 -0500901 slot = nfs4_alloc_slot(tbl);
902 if (IS_ERR(slot)) {
903 /* Try again in 1/4 second */
904 if (slot == ERR_PTR(-ENOMEM))
905 task->tk_timeout = HZ >> 2;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500906 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -0500907 }
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500908 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500909
Anna Schumaker3d358082017-01-11 10:54:04 -0500910 slot->privileged = args->sa_privileged ? 1 : 0;
911 args->sa_slot = slot;
912
913 res->sr_slot = slot;
914 if (session) {
915 res->sr_timestamp = jiffies;
916 res->sr_status_flags = 0;
917 res->sr_status = 1;
Anna Schumaker3d358082017-01-11 10:54:04 -0500918 }
919
Anna Schumakerad05cc02017-01-11 13:37:06 -0500920 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd91072017-01-10 12:01:46 -0500921out_start:
922 rpc_call_start(task);
923 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500924
925out_sleep:
926 if (args->sa_privileged)
927 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
928 NULL, RPC_PRIORITY_PRIVILEGED);
929 else
930 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
931 spin_unlock(&tbl->slot_tbl_lock);
932 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500933}
934EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
935
Chuck Lever9915ea72013-08-09 12:48:27 -0400936static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
937{
938 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500939 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -0400940 data->seq_args, data->seq_res, task);
941}
942
943static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
944{
945 struct nfs4_call_sync_data *data = calldata;
946 nfs4_sequence_done(task, data->seq_res);
947}
948
949static const struct rpc_call_ops nfs40_call_sync_ops = {
950 .rpc_call_prepare = nfs40_call_sync_prepare,
951 .rpc_call_done = nfs40_call_sync_done,
952};
953
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400954static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400955 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500956 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100957 struct nfs4_sequence_args *args,
958 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400959{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100960 int ret;
961 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400962 struct nfs_client *clp = server->nfs_client;
963 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100964 .seq_server = server,
965 .seq_args = args,
966 .seq_res = res,
967 };
968 struct rpc_task_setup task_setup = {
969 .rpc_client = clnt,
970 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400971 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100972 .callback_data = &data
973 };
974
975 task = rpc_run_task(&task_setup);
976 if (IS_ERR(task))
977 ret = PTR_ERR(task);
978 else {
979 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400980 rpc_put_task(task);
981 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100982 return ret;
983}
984
Bryan Schumaker7c513052011-03-24 17:12:24 +0000985int nfs4_call_sync(struct rpc_clnt *clnt,
986 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000987 struct rpc_message *msg,
988 struct nfs4_sequence_args *args,
989 struct nfs4_sequence_res *res,
990 int cache_reply)
991{
Chuck Levera9c92d62013-08-09 12:48:18 -0400992 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400993 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000994}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Trond Myklebustd3129ef2017-01-11 22:07:28 -0500996static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
997 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 struct nfs_inode *nfsi = NFS_I(dir);
1000
1001 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001002 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001003 if (cinfo->atomic && cinfo->before == dir->i_version) {
1004 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1005 nfsi->attrtimeo_timestamp = jiffies;
1006 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 nfs_force_lookup_revalidate(dir);
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001008 if (cinfo->before != dir->i_version)
1009 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1010 NFS_INO_INVALID_ACL;
1011 }
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001012 dir->i_version = cinfo->after;
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001013 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001014 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001015 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 spin_unlock(&dir->i_lock);
1017}
1018
1019struct nfs4_opendata {
1020 struct kref kref;
1021 struct nfs_openargs o_arg;
1022 struct nfs_openres o_res;
1023 struct nfs_open_confirmargs c_arg;
1024 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001025 struct nfs4_string owner_name;
1026 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001027 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001029 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001031 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 struct nfs4_state_owner *owner;
1033 struct nfs4_state *state;
1034 struct iattr attrs;
1035 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001036 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001037 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001038 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001039 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 int cancelled;
1041};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001042
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001043static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1044 int err, struct nfs4_exception *exception)
1045{
1046 if (err != -EINVAL)
1047 return false;
1048 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1049 return false;
1050 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1051 exception->retry = 1;
1052 return true;
1053}
1054
Trond Myklebust6ae37332015-01-30 14:21:14 -05001055static u32
1056nfs4_map_atomic_open_share(struct nfs_server *server,
1057 fmode_t fmode, int openflags)
1058{
1059 u32 res = 0;
1060
1061 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1062 case FMODE_READ:
1063 res = NFS4_SHARE_ACCESS_READ;
1064 break;
1065 case FMODE_WRITE:
1066 res = NFS4_SHARE_ACCESS_WRITE;
1067 break;
1068 case FMODE_READ|FMODE_WRITE:
1069 res = NFS4_SHARE_ACCESS_BOTH;
1070 }
1071 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1072 goto out;
1073 /* Want no delegation if we're using O_DIRECT */
1074 if (openflags & O_DIRECT)
1075 res |= NFS4_SHARE_WANT_NO_DELEG;
1076out:
1077 return res;
1078}
1079
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001080static enum open_claim_type4
1081nfs4_map_atomic_open_claim(struct nfs_server *server,
1082 enum open_claim_type4 claim)
1083{
1084 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1085 return claim;
1086 switch (claim) {
1087 default:
1088 return claim;
1089 case NFS4_OPEN_CLAIM_FH:
1090 return NFS4_OPEN_CLAIM_NULL;
1091 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1092 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1093 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1094 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1095 }
1096}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001099{
1100 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001101 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001102 p->o_res.seqid = p->o_arg.seqid;
1103 p->c_res.seqid = p->c_arg.seqid;
1104 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001105 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001106 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001107 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001108}
1109
Al Viro82a2c1b2011-06-22 18:30:55 -04001110static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001111 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001112 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001113 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001114 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001115 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001116{
Al Viro82a2c1b2011-06-22 18:30:55 -04001117 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001118 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001119 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001120 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001121 struct nfs4_opendata *p;
1122
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001123 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001124 if (p == NULL)
1125 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001126
1127 p->f_label = nfs4_label_alloc(server, gfp_mask);
1128 if (IS_ERR(p->f_label))
1129 goto err_free_p;
1130
Kinglong Meea49c2692015-07-27 15:31:38 +08001131 p->a_label = nfs4_label_alloc(server, gfp_mask);
1132 if (IS_ERR(p->a_label))
1133 goto err_free_f;
1134
Trond Myklebust63f5f792015-01-23 19:19:25 -05001135 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1136 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001137 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001138 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001139 nfs_sb_active(dentry->d_sb);
1140 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001141 p->dir = parent;
1142 p->owner = sp;
1143 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001144 p->o_arg.open_flags = flags;
1145 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001146 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001147 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001148 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1149 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001150 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1151 * will return permission denied for all bits until close */
1152 if (!(flags & O_EXCL)) {
1153 /* ask server to check for all possible rights as results
1154 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001155 switch (p->o_arg.claim) {
1156 default:
1157 break;
1158 case NFS4_OPEN_CLAIM_NULL:
1159 case NFS4_OPEN_CLAIM_FH:
1160 p->o_arg.access = NFS4_ACCESS_READ |
1161 NFS4_ACCESS_MODIFY |
1162 NFS4_ACCESS_EXTEND |
1163 NFS4_ACCESS_EXECUTE;
1164 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001165 }
David Howells7539bba2006-08-22 20:06:09 -04001166 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001167 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1168 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001169 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001170 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001171 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001172 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001173 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001174 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001175 case NFS4_OPEN_CLAIM_NULL:
1176 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1177 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1178 p->o_arg.fh = NFS_FH(dir);
1179 break;
1180 case NFS4_OPEN_CLAIM_PREVIOUS:
1181 case NFS4_OPEN_CLAIM_FH:
1182 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1183 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001184 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001185 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001186 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001187 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001188
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001189 p->o_arg.u.attrs = &p->attrs;
1190 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001191
1192 verf[0] = jiffies;
1193 verf[1] = current->pid;
1194 memcpy(p->o_arg.u.verifier.data, verf,
1195 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001196 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001197 p->c_arg.fh = &p->o_res.fh;
1198 p->c_arg.stateid = &p->o_res.stateid;
1199 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001200 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001201 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001202 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001203
1204err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001205 nfs4_label_free(p->a_label);
1206err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001207 nfs4_label_free(p->f_label);
1208err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001209 kfree(p);
1210err:
1211 dput(parent);
1212 return NULL;
1213}
1214
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001215static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001216{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001217 struct nfs4_opendata *p = container_of(kref,
1218 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001219 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001220
1221 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001222 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001223 if (p->state != NULL)
1224 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001225 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001226
Kinglong Meea49c2692015-07-27 15:31:38 +08001227 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001228 nfs4_label_free(p->f_label);
1229
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001230 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001231 dput(p->dentry);
1232 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001233 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001234 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001235 kfree(p);
1236}
1237
1238static void nfs4_opendata_put(struct nfs4_opendata *p)
1239{
1240 if (p != NULL)
1241 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001242}
1243
Trond Myklebust24311f82015-09-20 10:50:17 -04001244static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1245 fmode_t fmode)
1246{
1247 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1248 case FMODE_READ|FMODE_WRITE:
1249 return state->n_rdwr != 0;
1250 case FMODE_WRITE:
1251 return state->n_wronly != 0;
1252 case FMODE_READ:
1253 return state->n_rdonly != 0;
1254 }
1255 WARN_ON_ONCE(1);
1256 return false;
1257}
1258
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001259static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001260{
1261 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001262
Trond Myklebust536e43d2012-01-17 22:04:26 -05001263 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001264 goto out;
1265 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001266 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001267 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1268 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001269 break;
1270 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001271 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1272 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001273 break;
1274 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001275 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1276 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001277 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001278out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001279 return ret;
1280}
1281
Trond Myklebust2a606182015-08-19 22:30:00 -05001282static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1283 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001284{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001285 if (delegation == NULL)
1286 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001287 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001288 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001289 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1290 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001291 switch (claim) {
1292 case NFS4_OPEN_CLAIM_NULL:
1293 case NFS4_OPEN_CLAIM_FH:
1294 break;
1295 case NFS4_OPEN_CLAIM_PREVIOUS:
1296 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1297 break;
1298 default:
1299 return 0;
1300 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001301 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001302 return 1;
1303}
1304
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001305static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001306{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001307 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001308 case FMODE_WRITE:
1309 state->n_wronly++;
1310 break;
1311 case FMODE_READ:
1312 state->n_rdonly++;
1313 break;
1314 case FMODE_READ|FMODE_WRITE:
1315 state->n_rdwr++;
1316 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001317 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001318}
1319
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001320#ifdef CONFIG_NFS_V4_1
1321static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1322{
1323 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1324 return true;
1325 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1326 return true;
1327 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1328 return true;
1329 return false;
1330}
1331#endif /* CONFIG_NFS_V4_1 */
1332
Trond Myklebust4f14c192014-02-12 19:15:06 -05001333static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001334{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001335 struct nfs_client *clp = state->owner->so_server->nfs_client;
1336 bool need_recover = false;
1337
1338 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1339 need_recover = true;
1340 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1341 need_recover = true;
1342 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1343 need_recover = true;
1344 if (need_recover)
1345 nfs4_state_mark_reclaim_nograce(clp, state);
1346}
1347
Trond Myklebuste999e802014-02-10 18:20:47 -05001348static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001349 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001350{
1351 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1352 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001353 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001354 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001355 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001356 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001357 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001358 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1359 return true;
1360 return false;
1361}
1362
Trond Myklebustf95549c2015-01-23 18:06:09 -05001363static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1364{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001365 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1366 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001367 if (state->n_wronly)
1368 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1369 if (state->n_rdonly)
1370 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1371 if (state->n_rdwr)
1372 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001373 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001374}
1375
Trond Myklebust226056c2014-02-11 10:41:07 -05001376static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1377 nfs4_stateid *stateid, fmode_t fmode)
1378{
1379 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1380 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1381 case FMODE_WRITE:
1382 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1383 break;
1384 case FMODE_READ:
1385 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1386 break;
1387 case 0:
1388 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1389 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1390 clear_bit(NFS_OPEN_STATE, &state->flags);
1391 }
1392 if (stateid == NULL)
1393 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001394 /* Handle OPEN+OPEN_DOWNGRADE races */
1395 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1396 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001397 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001398 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001399 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001400 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001401 nfs4_stateid_copy(&state->stateid, stateid);
1402 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001403}
1404
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001405static void nfs_clear_open_stateid(struct nfs4_state *state,
1406 nfs4_stateid *arg_stateid,
1407 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001408{
1409 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001410 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1411 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1412 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001413 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001414 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1415 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001416}
1417
Trond Myklebust1393d962016-09-22 13:39:13 -04001418static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1419 const nfs4_stateid *stateid, fmode_t fmode,
1420 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001421{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001422 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001423 case FMODE_READ:
1424 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1425 break;
1426 case FMODE_WRITE:
1427 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1428 break;
1429 case FMODE_READ|FMODE_WRITE:
1430 set_bit(NFS_O_RDWR_STATE, &state->flags);
1431 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001432 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001433 return;
1434 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1435 nfs4_stateid_copy(&state->stateid, stateid);
1436 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001437}
1438
Trond Myklebust1393d962016-09-22 13:39:13 -04001439static void __update_open_stateid(struct nfs4_state *state,
1440 const nfs4_stateid *open_stateid,
1441 const nfs4_stateid *deleg_stateid,
1442 fmode_t fmode,
1443 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001445 /*
1446 * Protect the call to nfs4_state_set_mode_locked and
1447 * serialise the stateid update
1448 */
Andrew Elble361cad32015-12-02 09:20:57 -05001449 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001450 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001451 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001452 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001453 set_bit(NFS_DELEGATED_STATE, &state->flags);
1454 }
1455 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001456 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001457 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001458 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001459 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
Trond Myklebust1393d962016-09-22 13:39:13 -04001462static int update_open_stateid(struct nfs4_state *state,
1463 const nfs4_stateid *open_stateid,
1464 const nfs4_stateid *delegation,
1465 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001466{
Trond Myklebust1393d962016-09-22 13:39:13 -04001467 struct nfs_server *server = NFS_SERVER(state->inode);
1468 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001469 struct nfs_inode *nfsi = NFS_I(state->inode);
1470 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001471 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001472 int ret = 0;
1473
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001474 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001475
1476 rcu_read_lock();
1477 deleg_cur = rcu_dereference(nfsi->delegation);
1478 if (deleg_cur == NULL)
1479 goto no_delegation;
1480
1481 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001482 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001483 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001484 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001485 goto no_delegation_unlock;
1486
1487 if (delegation == NULL)
1488 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001489 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001490 goto no_delegation_unlock;
1491
Trond Myklebustb7391f42008-12-23 15:21:52 -05001492 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001493 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1494 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001495 ret = 1;
1496no_delegation_unlock:
1497 spin_unlock(&deleg_cur->lock);
1498no_delegation:
1499 rcu_read_unlock();
1500
1501 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001502 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001503 ret = 1;
1504 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001505 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001506 nfs4_schedule_state_manager(clp);
1507 if (freeme.type != 0)
1508 nfs4_test_and_free_stateid(server, &freeme,
1509 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001510
1511 return ret;
1512}
1513
Trond Myklebust39071e62015-01-24 15:07:56 -05001514static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1515 const nfs4_stateid *stateid)
1516{
1517 struct nfs4_state *state = lsp->ls_state;
1518 bool ret = false;
1519
1520 spin_lock(&state->state_lock);
1521 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1522 goto out_noupdate;
1523 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1524 goto out_noupdate;
1525 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1526 ret = true;
1527out_noupdate:
1528 spin_unlock(&state->state_lock);
1529 return ret;
1530}
Trond Myklebust34310432008-12-23 15:21:38 -05001531
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001532static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001533{
1534 struct nfs_delegation *delegation;
1535
1536 rcu_read_lock();
1537 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001538 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001539 rcu_read_unlock();
1540 return;
1541 }
1542 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001543 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001544}
1545
Trond Myklebust6ee41262007-07-08 14:11:36 -04001546static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001547{
1548 struct nfs4_state *state = opendata->state;
1549 struct nfs_inode *nfsi = NFS_I(state->inode);
1550 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001551 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001552 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001553 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001554 nfs4_stateid stateid;
1555 int ret = -EAGAIN;
1556
Trond Myklebustaac00a82007-07-05 19:02:21 -04001557 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001558 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001559 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001560 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001561 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001562 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001563 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001564 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001565 rcu_read_lock();
1566 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001567 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001568 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001569 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001570 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001571 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001572 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001573 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001574 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001575 if (!opendata->is_recover) {
1576 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1577 if (ret != 0)
1578 goto out;
1579 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001580 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001581
1582 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001583 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001584 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001585 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001586out:
1587 return ERR_PTR(ret);
1588out_return_state:
1589 atomic_inc(&state->count);
1590 return state;
1591}
1592
Andy Adamsone23008e2012-10-02 21:07:32 -04001593static void
1594nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1595{
1596 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1597 struct nfs_delegation *delegation;
1598 int delegation_flags = 0;
1599
1600 rcu_read_lock();
1601 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1602 if (delegation)
1603 delegation_flags = delegation->flags;
1604 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001605 switch (data->o_arg.claim) {
1606 default:
1607 break;
1608 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1609 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001610 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1611 "returning a delegation for "
1612 "OPEN(CLAIM_DELEGATE_CUR)\n",
1613 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001614 return;
1615 }
1616 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001617 nfs_inode_set_delegation(state->inode,
1618 data->owner->so_cred,
1619 &data->o_res);
1620 else
1621 nfs_inode_reclaim_delegation(state->inode,
1622 data->owner->so_cred,
1623 &data->o_res);
1624}
1625
1626/*
1627 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1628 * and update the nfs4_state.
1629 */
1630static struct nfs4_state *
1631_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1632{
1633 struct inode *inode = data->state->inode;
1634 struct nfs4_state *state = data->state;
1635 int ret;
1636
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001637 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001638 if (data->rpc_status)
1639 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001640 /* cached opens have already been processed */
1641 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001642 }
1643
Andy Adamsone23008e2012-10-02 21:07:32 -04001644 ret = nfs_refresh_inode(inode, &data->f_attr);
1645 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001646 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001647
1648 if (data->o_res.delegation_type != 0)
1649 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001650update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001651 update_open_stateid(state, &data->o_res.stateid, NULL,
1652 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001653 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001654
1655 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001656}
1657
1658static struct nfs4_state *
1659_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001660{
1661 struct inode *inode;
1662 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001663 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001664
Trond Myklebustaac00a82007-07-05 19:02:21 -04001665 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001666 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001667 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001668 goto out;
1669 }
1670
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001671 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001672 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001673 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001674 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001675 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001676 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001677 goto err;
1678 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001679 state = nfs4_get_open_state(inode, data->owner);
1680 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001681 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001682 if (data->o_res.delegation_type != 0)
1683 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001684 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001685 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001686 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001687out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001688 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001689 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001690err_put_inode:
1691 iput(inode);
1692err:
1693 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001694}
1695
Andy Adamsone23008e2012-10-02 21:07:32 -04001696static struct nfs4_state *
1697nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1698{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001699 struct nfs4_state *ret;
1700
Andy Adamsone23008e2012-10-02 21:07:32 -04001701 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001702 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1703 else
1704 ret = _nfs4_opendata_to_nfs4_state(data);
1705 nfs4_sequence_free_slot(&data->o_res.seq_res);
1706 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001707}
1708
Trond Myklebust864472e2006-01-03 09:55:15 +01001709static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1710{
1711 struct nfs_inode *nfsi = NFS_I(state->inode);
1712 struct nfs_open_context *ctx;
1713
1714 spin_lock(&state->inode->i_lock);
1715 list_for_each_entry(ctx, &nfsi->open_files, list) {
1716 if (ctx->state != state)
1717 continue;
1718 get_nfs_open_context(ctx);
1719 spin_unlock(&state->inode->i_lock);
1720 return ctx;
1721 }
1722 spin_unlock(&state->inode->i_lock);
1723 return ERR_PTR(-ENOENT);
1724}
1725
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001726static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1727 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001728{
1729 struct nfs4_opendata *opendata;
1730
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001731 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001732 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001733 if (opendata == NULL)
1734 return ERR_PTR(-ENOMEM);
1735 opendata->state = state;
1736 atomic_inc(&state->count);
1737 return opendata;
1738}
1739
Trond Myklebust24311f82015-09-20 10:50:17 -04001740static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1741 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001742{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001743 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001744 int ret;
1745
Trond Myklebust24311f82015-09-20 10:50:17 -04001746 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001747 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001748 opendata->o_arg.open_flags = 0;
1749 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001750 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1751 NFS_SB(opendata->dentry->d_sb),
1752 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001753 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1754 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1755 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001756 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001757 if (ret != 0)
1758 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001759 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001760 if (IS_ERR(newstate))
1761 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001762 if (newstate != opendata->state)
1763 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001764 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001765 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001766}
1767
1768static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1769{
Trond Myklebust864472e2006-01-03 09:55:15 +01001770 int ret;
1771
Trond Myklebust4f14c192014-02-12 19:15:06 -05001772 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1773 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1774 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1775 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001776 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001777 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001778 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001779 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001780 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1781 if (ret != 0)
1782 return ret;
1783 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1784 if (ret != 0)
1785 return ret;
1786 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1787 if (ret != 0)
1788 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001789 /*
1790 * We may have performed cached opens for all three recoveries.
1791 * Check if we need to update the current stateid.
1792 */
1793 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001794 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001795 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001796 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001797 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001798 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001799 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001800 return 0;
1801}
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803/*
1804 * OPEN_RECLAIM:
1805 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001807static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001809 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001810 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001811 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 int status;
1813
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001814 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1815 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001816 if (IS_ERR(opendata))
1817 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001818 rcu_read_lock();
1819 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001820 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001821 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001822 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001823 opendata->o_arg.u.delegation_type = delegation_type;
1824 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001825 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return status;
1827}
1828
Trond Myklebust539cd032007-06-05 11:46:42 -04001829static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
1831 struct nfs_server *server = NFS_SERVER(state->inode);
1832 struct nfs4_exception exception = { };
1833 int err;
1834 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001835 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001836 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001837 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1838 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001839 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001840 break;
1841 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 } while (exception.retry);
1843 return err;
1844}
1845
Trond Myklebust864472e2006-01-03 09:55:15 +01001846static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1847{
1848 struct nfs_open_context *ctx;
1849 int ret;
1850
1851 ctx = nfs4_state_find_open_context(state);
1852 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001853 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001854 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001855 put_nfs_open_context(ctx);
1856 return ret;
1857}
1858
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001859static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001861 switch (err) {
1862 default:
1863 printk(KERN_ERR "NFS: %s: unhandled error "
1864 "%d.\n", __func__, err);
1865 case 0:
1866 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001867 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001868 case -ESTALE:
1869 break;
1870 case -NFS4ERR_BADSESSION:
1871 case -NFS4ERR_BADSLOT:
1872 case -NFS4ERR_BAD_HIGH_SLOT:
1873 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1874 case -NFS4ERR_DEADSESSION:
1875 set_bit(NFS_DELEGATED_STATE, &state->flags);
1876 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1877 return -EAGAIN;
1878 case -NFS4ERR_STALE_CLIENTID:
1879 case -NFS4ERR_STALE_STATEID:
1880 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001881 /* Don't recall a delegation if it was lost */
1882 nfs4_schedule_lease_recovery(server->nfs_client);
1883 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001884 case -NFS4ERR_MOVED:
1885 nfs4_schedule_migration_recovery(server);
1886 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001887 case -NFS4ERR_LEASE_MOVED:
1888 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1889 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001890 case -NFS4ERR_DELEG_REVOKED:
1891 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001892 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001893 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001894 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001895 nfs_inode_find_state_and_recover(state->inode,
1896 stateid);
1897 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001898 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001899 case -NFS4ERR_DELAY:
1900 case -NFS4ERR_GRACE:
1901 set_bit(NFS_DELEGATED_STATE, &state->flags);
1902 ssleep(1);
1903 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001904 case -ENOMEM:
1905 case -NFS4ERR_DENIED:
1906 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1907 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 return err;
1910}
1911
Trond Myklebust24311f82015-09-20 10:50:17 -04001912int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1913 struct nfs4_state *state, const nfs4_stateid *stateid,
1914 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001915{
1916 struct nfs_server *server = NFS_SERVER(state->inode);
1917 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001918 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001919
1920 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1921 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1922 if (IS_ERR(opendata))
1923 return PTR_ERR(opendata);
1924 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001925 write_seqlock(&state->seqlock);
1926 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1927 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001928 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1929 switch (type & (FMODE_READ|FMODE_WRITE)) {
1930 case FMODE_READ|FMODE_WRITE:
1931 case FMODE_WRITE:
1932 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1933 if (err)
1934 break;
1935 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1936 if (err)
1937 break;
1938 case FMODE_READ:
1939 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1940 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001941 nfs4_opendata_put(opendata);
1942 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1943}
1944
Chuck Leverbe05c862013-08-09 12:49:47 -04001945static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1946{
1947 struct nfs4_opendata *data = calldata;
1948
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001949 nfs4_setup_sequence(data->o_arg.server->nfs_client,
1950 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001951}
1952
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001953static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1954{
1955 struct nfs4_opendata *data = calldata;
1956
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001957 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001958
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001959 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001960 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001961 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001962 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001963 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001964 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001965 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001966}
1967
1968static void nfs4_open_confirm_release(void *calldata)
1969{
1970 struct nfs4_opendata *data = calldata;
1971 struct nfs4_state *state = NULL;
1972
1973 /* If this request hasn't been cancelled, do nothing */
1974 if (data->cancelled == 0)
1975 goto out_free;
1976 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001977 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001978 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001979 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001980 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001981 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001982out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001983 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001984}
1985
1986static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001987 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001988 .rpc_call_done = nfs4_open_confirm_done,
1989 .rpc_release = nfs4_open_confirm_release,
1990};
1991
1992/*
1993 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1994 */
1995static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1996{
David Howells2b0143b2015-03-17 22:25:59 +00001997 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001998 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001999 struct rpc_message msg = {
2000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2001 .rpc_argp = &data->c_arg,
2002 .rpc_resp = &data->c_res,
2003 .rpc_cred = data->owner->so_cred,
2004 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002005 struct rpc_task_setup task_setup_data = {
2006 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002007 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002008 .callback_ops = &nfs4_open_confirm_ops,
2009 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002010 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002011 .flags = RPC_TASK_ASYNC,
2012 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 int status;
2014
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002015 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002016 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002017 data->rpc_done = 0;
2018 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002019 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002020 if (data->is_recover)
2021 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002022 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002023 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002024 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002025 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002026 if (status != 0) {
2027 data->cancelled = 1;
2028 smp_wmb();
2029 } else
2030 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002031 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 return status;
2033}
2034
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002035static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002037 struct nfs4_opendata *data = calldata;
2038 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002039 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002040 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002041
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002042 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002043 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002044 /*
2045 * Check if we still need to send an OPEN call, or if we can use
2046 * a delegation instead.
2047 */
2048 if (data->state != NULL) {
2049 struct nfs_delegation *delegation;
2050
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002051 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002052 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002053 rcu_read_lock();
2054 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002055 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002056 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002057 rcu_read_unlock();
2058 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002059 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002060 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002061 switch (claim) {
2062 default:
2063 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002064 case NFS4_OPEN_CLAIM_PREVIOUS:
2065 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2066 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002067 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002068 case NFS4_OPEN_CLAIM_FH:
2069 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002070 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2071 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002072 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002073 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002074 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002075 &data->o_res.seq_res,
2076 task) != 0)
2077 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002078
2079 /* Set the create mode (note dependency on the session type) */
2080 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2081 if (data->o_arg.open_flags & O_EXCL) {
2082 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2083 if (nfs4_has_persistent_session(clp))
2084 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2085 else if (clp->cl_mvops->minor_version > 0)
2086 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2087 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002088 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002089unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002090 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002091 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002092out_no_action:
2093 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002094out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002095 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002096}
2097
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002098static void nfs4_open_done(struct rpc_task *task, void *calldata)
2099{
2100 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002102 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002103
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002104 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002105 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002106
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002107 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002108 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2109 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002110 case S_IFREG:
2111 break;
2112 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002113 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002114 break;
2115 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002116 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002117 break;
2118 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002119 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002120 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002121 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002122 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002123 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2124 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002125 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002126 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002127}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002128
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002129static void nfs4_open_release(void *calldata)
2130{
2131 struct nfs4_opendata *data = calldata;
2132 struct nfs4_state *state = NULL;
2133
2134 /* If this request hasn't been cancelled, do nothing */
2135 if (data->cancelled == 0)
2136 goto out_free;
2137 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002138 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002139 goto out_free;
2140 /* In case we need an open_confirm, no cleanup! */
2141 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2142 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002143 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002144 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002145 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002146out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002147 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002148}
2149
2150static const struct rpc_call_ops nfs4_open_ops = {
2151 .rpc_call_prepare = nfs4_open_prepare,
2152 .rpc_call_done = nfs4_open_done,
2153 .rpc_release = nfs4_open_release,
2154};
2155
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002156static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002157{
David Howells2b0143b2015-03-17 22:25:59 +00002158 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002159 struct nfs_server *server = NFS_SERVER(dir);
2160 struct nfs_openargs *o_arg = &data->o_arg;
2161 struct nfs_openres *o_res = &data->o_res;
2162 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002163 struct rpc_message msg = {
2164 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2165 .rpc_argp = o_arg,
2166 .rpc_resp = o_res,
2167 .rpc_cred = data->owner->so_cred,
2168 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002169 struct rpc_task_setup task_setup_data = {
2170 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002171 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002172 .callback_ops = &nfs4_open_ops,
2173 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002174 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002175 .flags = RPC_TASK_ASYNC,
2176 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002177 int status;
2178
Chuck Levera9c92d62013-08-09 12:48:18 -04002179 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002180 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002181 data->rpc_done = 0;
2182 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002183 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002184 data->is_recover = 0;
2185 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002186 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002187 data->is_recover = 1;
2188 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002189 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002190 if (IS_ERR(task))
2191 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002192 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002193 if (status != 0) {
2194 data->cancelled = 1;
2195 smp_wmb();
2196 } else
2197 status = data->rpc_status;
2198 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002199
2200 return status;
2201}
2202
2203static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2204{
David Howells2b0143b2015-03-17 22:25:59 +00002205 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002206 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002207 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002208
2209 status = nfs4_run_open_task(data, 1);
2210 if (status != 0 || !data->rpc_done)
2211 return status;
2212
Trond Myklebust6926afd2012-01-07 13:22:46 -05002213 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2214
Anna Schumakerd7e98252017-01-11 16:13:29 -05002215 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002216 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002217
2218 return status;
2219}
2220
Trond Myklebustf3792d62014-07-10 08:54:32 -04002221/*
2222 * Additional permission checks in order to distinguish between an
2223 * open for read, and an open for execute. This works around the
2224 * fact that NFSv4 OPEN treats read and execute permissions as being
2225 * the same.
2226 * Note that in the non-execute case, we want to turn off permission
2227 * checking if we just created a new file (POSIX open() semantics).
2228 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002229static int nfs4_opendata_access(struct rpc_cred *cred,
2230 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002231 struct nfs4_state *state, fmode_t fmode,
2232 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002233{
2234 struct nfs_access_entry cache;
2235 u32 mask;
2236
2237 /* access call failed or for some reason the server doesn't
2238 * support any access modes -- defer access call until later */
2239 if (opendata->o_res.access_supported == 0)
2240 return 0;
2241
2242 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002243 /*
2244 * Use openflags to check for exec, because fmode won't
2245 * always have FMODE_EXEC set when file open for exec.
2246 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002247 if (openflags & __FMODE_EXEC) {
2248 /* ONLY check for exec rights */
2249 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002250 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002251 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002252
2253 cache.cred = cred;
2254 cache.jiffies = jiffies;
2255 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2256 nfs_access_add_cache(state->inode, &cache);
2257
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002258 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002259 return 0;
2260
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002261 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002262}
2263
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002264/*
2265 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2266 */
2267static int _nfs4_proc_open(struct nfs4_opendata *data)
2268{
David Howells2b0143b2015-03-17 22:25:59 +00002269 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002270 struct nfs_server *server = NFS_SERVER(dir);
2271 struct nfs_openargs *o_arg = &data->o_arg;
2272 struct nfs_openres *o_res = &data->o_res;
2273 int status;
2274
2275 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002276 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002277 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002278 if (status != 0) {
2279 if (status == -NFS4ERR_BADNAME &&
2280 !(o_arg->open_flags & O_CREAT))
2281 return -ENOENT;
2282 return status;
2283 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002284
Trond Myklebust6926afd2012-01-07 13:22:46 -05002285 nfs_fattr_map_and_free_names(server, &data->f_attr);
2286
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002287 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002288 if (o_arg->open_flags & O_EXCL)
2289 data->file_created = 1;
2290 else if (o_res->cinfo.before != o_res->cinfo.after)
2291 data->file_created = 1;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002292 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002293 update_changeattr(dir, &o_res->cinfo,
2294 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002295 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002296 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2297 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002299 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002301 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
2303 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002304 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306}
2307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308/*
2309 * OPEN_EXPIRED:
2310 * reclaim state on the server after a network partition.
2311 * Assumes caller holds the appropriate lock
2312 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002313static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002315 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002316 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002318 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002319 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002320 if (IS_ERR(opendata))
2321 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002322 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002323 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002324 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002325 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002326 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
2328
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002329static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002330{
Trond Myklebust539cd032007-06-05 11:46:42 -04002331 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002332 struct nfs4_exception exception = { };
2333 int err;
2334
2335 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002336 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002337 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002338 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2339 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002340 switch (err) {
2341 default:
2342 goto out;
2343 case -NFS4ERR_GRACE:
2344 case -NFS4ERR_DELAY:
2345 nfs4_handle_exception(server, err, &exception);
2346 err = 0;
2347 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002348 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002349out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002350 return err;
2351}
2352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2354{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002356 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Trond Myklebust864472e2006-01-03 09:55:15 +01002358 ctx = nfs4_state_find_open_context(state);
2359 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002360 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002361 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002362 put_nfs_open_context(ctx);
2363 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
Trond Myklebust41020b62016-09-22 13:38:58 -04002366static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2367 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002368{
Trond Myklebust41020b62016-09-22 13:38:58 -04002369 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002370 write_seqlock(&state->seqlock);
2371 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2372 write_sequnlock(&state->seqlock);
2373 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2374}
2375
2376static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2377{
2378 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002379 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002380}
2381
2382static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2383{
2384 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2385 nfs40_clear_delegation_stateid(state);
2386 return nfs4_open_expired(sp, state);
2387}
2388
Trond Myklebust45870d62016-09-22 13:38:59 -04002389static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2390 nfs4_stateid *stateid,
2391 struct rpc_cred *cred)
2392{
2393 return -NFS4ERR_BAD_STATEID;
2394}
2395
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002396#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002397static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2398 nfs4_stateid *stateid,
2399 struct rpc_cred *cred)
2400{
2401 int status;
2402
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002403 switch (stateid->type) {
2404 default:
2405 break;
2406 case NFS4_INVALID_STATEID_TYPE:
2407 case NFS4_SPECIAL_STATEID_TYPE:
2408 return -NFS4ERR_BAD_STATEID;
2409 case NFS4_REVOKED_STATEID_TYPE:
2410 goto out_free;
2411 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002412
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002413 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002414 switch (status) {
2415 case -NFS4ERR_EXPIRED:
2416 case -NFS4ERR_ADMIN_REVOKED:
2417 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002418 break;
2419 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002420 return status;
2421 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002422out_free:
2423 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002424 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002425 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002426}
2427
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002428static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002429{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002430 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002431 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002432 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002433 struct rpc_cred *cred;
2434 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002435
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002436 /* Get the delegation credential for use by test/free_stateid */
2437 rcu_read_lock();
2438 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002439 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002440 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002441 return;
2442 }
2443
2444 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002445 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2446 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2447 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002448 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002449 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002450 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002451 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002452
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002453 cred = get_rpccred(delegation->cred);
2454 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002455 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002456 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002457 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002458 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002459
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002460 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002461}
2462
2463/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002464 * nfs41_check_expired_locks - possibly free a lock stateid
2465 *
2466 * @state: NFSv4 state for an inode
2467 *
2468 * Returns NFS_OK if recovery for this stateid is now finished.
2469 * Otherwise a negative NFS4ERR value is returned.
2470 */
2471static int nfs41_check_expired_locks(struct nfs4_state *state)
2472{
2473 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002474 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002475 struct nfs_server *server = NFS_SERVER(state->inode);
2476
2477 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2478 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002479
2480 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002481 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2482 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2483 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2484
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002485 atomic_inc(&lsp->ls_count);
2486 spin_unlock(&state->state_lock);
2487
2488 nfs4_put_lock_state(prev);
2489 prev = lsp;
2490
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002491 status = nfs41_test_and_free_expired_stateid(server,
2492 &lsp->ls_stateid,
2493 cred);
2494 trace_nfs4_test_lock_stateid(state, lsp, status);
2495 if (status == -NFS4ERR_EXPIRED ||
2496 status == -NFS4ERR_BAD_STATEID) {
2497 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002498 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002499 if (!recover_lost_locks)
2500 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2501 } else if (status != NFS_OK) {
2502 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002503 nfs4_put_lock_state(prev);
2504 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002505 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002506 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002507 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002508 }
2509 spin_unlock(&state->state_lock);
2510 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002511out:
2512 return ret;
2513}
2514
2515/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002516 * nfs41_check_open_stateid - possibly free an open stateid
2517 *
2518 * @state: NFSv4 state for an inode
2519 *
2520 * Returns NFS_OK if recovery for this stateid is now finished.
2521 * Otherwise a negative NFS4ERR value is returned.
2522 */
2523static int nfs41_check_open_stateid(struct nfs4_state *state)
2524{
2525 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002526 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002527 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002528 int status;
2529
Trond Myklebustb134fc42016-09-22 13:39:16 -04002530 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002531 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2532 if (nfs4_have_delegation(state->inode, state->state))
2533 return NFS_OK;
2534 return -NFS4ERR_OPENMODE;
2535 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002536 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002537 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002538 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002539 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002540 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002541 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2542 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2543 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002544 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002545 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002546 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002547 if (status != NFS_OK)
2548 return status;
2549 if (nfs_open_stateid_recover_openmode(state))
2550 return -NFS4ERR_OPENMODE;
2551 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002552}
2553
2554static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2555{
Chuck Levereb64cf92012-07-11 16:30:05 -04002556 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002557
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002558 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002559 status = nfs41_check_expired_locks(state);
2560 if (status != NFS_OK)
2561 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002562 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002563 if (status != NFS_OK)
2564 status = nfs4_open_expired(sp, state);
2565 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002566}
2567#endif
2568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002570 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2571 * fields corresponding to attributes that were used to store the verifier.
2572 * Make sure we clobber those fields in the later setattr call
2573 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002574static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2575 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002576{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002577 const u32 *attrset = opendata->o_res.attrset;
2578
2579 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002580 !(sattr->ia_valid & ATTR_ATIME_SET))
2581 sattr->ia_valid |= ATTR_ATIME;
2582
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002583 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002584 !(sattr->ia_valid & ATTR_MTIME_SET))
2585 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002586
2587 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002588 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2589 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002590 sattr->ia_valid &= ~ATTR_MODE;
2591
2592 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2593 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002594}
2595
Trond Myklebustc21443c2013-02-07 14:26:21 -05002596static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2597 fmode_t fmode,
2598 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002599 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002600{
2601 struct nfs4_state_owner *sp = opendata->owner;
2602 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002603 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002604 struct nfs4_state *state;
2605 unsigned int seq;
2606 int ret;
2607
2608 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2609
2610 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002611 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002612 goto out;
2613
2614 state = nfs4_opendata_to_nfs4_state(opendata);
2615 ret = PTR_ERR(state);
2616 if (IS_ERR(state))
2617 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002618 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002619 if (server->caps & NFS_CAP_POSIX_LOCK)
2620 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002621 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2622 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002623
Trond Myklebust275bb302013-05-29 13:11:28 -04002624 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002625 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002626 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002627 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002628 alias = d_exact_alias(dentry, state->inode);
2629 if (!alias)
2630 alias = d_splice_alias(igrab(state->inode), dentry);
2631 /* d_splice_alias() can't fail here - it's a non-directory */
2632 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002633 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002634 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002635 }
2636 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002637 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002638 }
2639
Trond Myklebustc21443c2013-02-07 14:26:21 -05002640 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2641 if (ret != 0)
2642 goto out;
2643
David Howells2b0143b2015-03-17 22:25:59 +00002644 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002645 nfs_inode_attach_open_context(ctx);
2646 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2647 nfs4_schedule_stateid_recovery(server, state);
2648 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002649out:
2650 return ret;
2651}
2652
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002653/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002654 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 */
Andy Adamson82be4172012-05-23 05:02:35 -04002656static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002657 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002658 int flags,
2659 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002660 struct nfs4_label *label,
2661 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662{
2663 struct nfs4_state_owner *sp;
2664 struct nfs4_state *state = NULL;
2665 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002666 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002667 struct dentry *dentry = ctx->dentry;
2668 struct rpc_cred *cred = ctx->cred;
2669 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2670 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002671 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002672 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002673 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
2675 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002677 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2678 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2680 goto out_err;
2681 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002682 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002683 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002684 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002685 if (d_really_is_positive(dentry))
2686 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002687 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002688 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002689 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002690 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002691 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002692 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002693 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
David Quigley14c43f72013-05-22 12:50:43 -04002695 if (label) {
2696 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2697 if (IS_ERR(olabel)) {
2698 status = PTR_ERR(olabel);
2699 goto err_opendata_put;
2700 }
2701 }
2702
Trond Myklebuste911b812014-03-26 13:24:37 -07002703 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2704 if (!opendata->f_attr.mdsthreshold) {
2705 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2706 if (!opendata->f_attr.mdsthreshold)
2707 goto err_free_label;
2708 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002709 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002710 }
David Howells2b0143b2015-03-17 22:25:59 +00002711 if (d_really_is_positive(dentry))
2712 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002713
Trond Myklebust3efb9722013-05-29 13:17:04 -04002714 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002715 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002716 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002717 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002718
NeilBrownefcbc042015-07-30 13:00:56 +10002719 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002720 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002721 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002722 /*
2723 * send create attributes which was not set by open
2724 * with an extra setattr.
2725 */
2726 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2727 nfs_fattr_init(opendata->o_res.f_attr);
2728 status = nfs4_do_setattr(state->inode, cred,
2729 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002730 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002731 if (status == 0) {
2732 nfs_setattr_update_inode(state->inode, sattr,
2733 opendata->o_res.f_attr);
2734 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2735 }
David Quigley1775fd32013-05-22 12:50:42 -04002736 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002737 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002738 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002739 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002740
Trond Myklebuste911b812014-03-26 13:24:37 -07002741 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002742 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002743 opendata->f_attr.mdsthreshold = NULL;
2744 }
Andy Adamson82be4172012-05-23 05:02:35 -04002745
David Quigley14c43f72013-05-22 12:50:43 -04002746 nfs4_label_free(olabel);
2747
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002748 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002751err_free_label:
2752 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002753err_opendata_put:
2754 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002755err_put_state_owner:
2756 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 return status;
2759}
2760
2761
Andy Adamson82be4172012-05-23 05:02:35 -04002762static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002763 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002764 int flags,
2765 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002766 struct nfs4_label *label,
2767 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002769 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 struct nfs4_exception exception = { };
2771 struct nfs4_state *res;
2772 int status;
2773
2774 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002775 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002776 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002777 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 if (status == 0)
2779 break;
2780 /* NOTE: BAD_SEQID means the server and client disagree about the
2781 * book-keeping w.r.t. state-changing operations
2782 * (OPEN/CLOSE/LOCK/LOCKU...)
2783 * It is actually a sign of a bug on the client or on the server.
2784 *
2785 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002786 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 * have unhashed the old state_owner for us, and that we can
2788 * therefore safely retry using a new one. We should still warn
2789 * the user though...
2790 */
2791 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002792 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002793 " returned a bad sequence-id error!\n",
2794 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 exception.retry = 1;
2796 continue;
2797 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002798 /*
2799 * BAD_STATEID on OPEN means that the server cancelled our
2800 * state before it received the OPEN_CONFIRM.
2801 * Recover by retrying the request as per the discussion
2802 * on Page 181 of RFC3530.
2803 */
2804 if (status == -NFS4ERR_BAD_STATEID) {
2805 exception.retry = 1;
2806 continue;
2807 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002808 if (status == -EAGAIN) {
2809 /* We must have found a delegation */
2810 exception.retry = 1;
2811 continue;
2812 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002813 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2814 continue;
2815 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 status, &exception));
2817 } while (exception.retry);
2818 return res;
2819}
2820
Trond Myklebust8487c472016-06-26 08:44:35 -04002821static int _nfs4_do_setattr(struct inode *inode,
2822 struct nfs_setattrargs *arg,
2823 struct nfs_setattrres *res,
2824 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002825 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002826{
2827 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002828 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04002829 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2830 .rpc_argp = arg,
2831 .rpc_resp = res,
2832 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002833 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002834 struct rpc_cred *delegation_cred = NULL;
2835 unsigned long timestamp = jiffies;
2836 fmode_t fmode;
2837 bool truncate;
2838 int status;
2839
2840 nfs_fattr_init(res->fattr);
2841
2842 /* Servers should only apply open mode checks for file size changes */
2843 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2844 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2845
2846 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2847 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002848 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002849 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002850 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002851 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002852 l_ctx = nfs_get_lock_context(ctx);
2853 if (IS_ERR(l_ctx))
2854 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002855 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2856 &arg->stateid, &delegation_cred);
2857 nfs_put_lock_context(l_ctx);
2858 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002859 return -EBADF;
2860 } else
2861 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2862 if (delegation_cred)
2863 msg.rpc_cred = delegation_cred;
2864
2865 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2866
2867 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002868 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002869 renew_lease(server, timestamp);
2870 trace_nfs4_setattr(inode, &arg->stateid, status);
2871 return status;
2872}
2873
2874static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2875 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002876 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002877 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002879 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002880 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002881 struct nfs_setattrargs arg = {
2882 .fh = NFS_FH(inode),
2883 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 .server = server,
2885 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002886 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002887 };
2888 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002890 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002892 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002893 struct nfs4_exception exception = {
2894 .state = state,
2895 .inode = inode,
2896 .stateid = &arg.stateid,
2897 };
2898 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
David Quigleyaa9c2662013-05-22 12:50:44 -04002900 arg.bitmask = nfs4_bitmask(server, ilabel);
2901 if (ilabel)
2902 arg.bitmask = nfs4_bitmask(server, olabel);
2903
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 do {
NeilBrown29b59f92016-10-13 15:26:47 +11002905 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04002906 switch (err) {
2907 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002908 if (!(sattr->ia_valid & ATTR_SIZE)) {
2909 pr_warn_once("NFSv4: server %s is incorrectly "
2910 "applying open mode checks to "
2911 "a SETATTR that is not "
2912 "changing file size.\n",
2913 server->nfs_client->cl_hostname);
2914 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002915 if (state && !(state->state & FMODE_WRITE)) {
2916 err = -EBADF;
2917 if (sattr->ia_valid & ATTR_OPEN)
2918 err = -EACCES;
2919 goto out;
2920 }
2921 }
2922 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002924out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 return err;
2926}
2927
Peng Tao500d7012015-09-22 11:35:22 +08002928static bool
2929nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2930{
2931 if (inode == NULL || !nfs_have_layout(inode))
2932 return false;
2933
2934 return pnfs_wait_on_layoutreturn(inode, task);
2935}
2936
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937struct nfs4_closedata {
2938 struct inode *inode;
2939 struct nfs4_state *state;
2940 struct nfs_closeargs arg;
2941 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05002942 struct {
2943 struct nfs4_layoutreturn_args arg;
2944 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04002945 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05002946 u32 roc_barrier;
2947 bool roc;
2948 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002949 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002950 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951};
2952
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002953static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002954{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002955 struct nfs4_closedata *calldata = data;
2956 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002957 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002958
Trond Myklebustcf805162016-11-15 14:56:07 -05002959 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05002960 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
2961 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07002962 nfs4_put_open_state(calldata->state);
2963 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002964 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002965 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002966 kfree(calldata);
2967}
2968
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002969static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002971 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002974 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Chuck Levera3ca5652012-03-01 17:00:40 -05002976 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002977 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2978 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002979 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05002980
2981 /* Handle Layoutreturn errors */
2982 if (calldata->arg.lr_args && task->tk_status != 0) {
2983 switch (calldata->res.lr_ret) {
2984 default:
2985 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
2986 break;
2987 case 0:
2988 calldata->arg.lr_args = NULL;
2989 calldata->res.lr_res = NULL;
2990 break;
2991 case -NFS4ERR_ADMIN_REVOKED:
2992 case -NFS4ERR_DELEG_REVOKED:
2993 case -NFS4ERR_EXPIRED:
2994 case -NFS4ERR_BAD_STATEID:
2995 case -NFS4ERR_OLD_STATEID:
2996 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
2997 case -NFS4ERR_WRONG_CRED:
2998 calldata->arg.lr_args = NULL;
2999 calldata->res.lr_res = NULL;
3000 calldata->res.lr_ret = 0;
3001 rpc_restart_call_prepare(task);
3002 return;
3003 }
3004 }
3005
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003006 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 * the state_owner. we keep this around to process errors
3008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 switch (task->tk_status) {
3010 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003011 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003012 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003013 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003014 case -NFS4ERR_ACCESS:
3015 if (calldata->arg.bitmask != NULL) {
3016 calldata->arg.bitmask = NULL;
3017 calldata->res.fattr = NULL;
3018 task->tk_status = 0;
3019 rpc_restart_call_prepare(task);
3020 goto out_release;
3021
3022 }
3023 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003024 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003026 case -NFS4ERR_EXPIRED:
3027 nfs4_free_revoked_stateid(server,
3028 &calldata->arg.stateid,
3029 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003030 case -NFS4ERR_OLD_STATEID:
3031 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003032 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003033 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003034 rpc_restart_call_prepare(task);
3035 goto out_release;
3036 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003037 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003038 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003040 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003041 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003042 goto out_release;
3043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003045 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3046 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003047out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003048 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003049 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003050 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051}
3052
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003053static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003055 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003056 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003057 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003058 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003059 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003060
Chuck Levera3ca5652012-03-01 17:00:40 -05003061 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003062 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003063 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003064
Trond Myklebust88069f72009-12-08 08:33:16 -05003065 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003066 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003067 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3068 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3069 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003070 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003071 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003072 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003073 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003074 if (state->n_rdonly == 0)
3075 call_close |= is_rdonly;
3076 else if (is_rdonly)
3077 calldata->arg.fmode |= FMODE_READ;
3078 if (state->n_wronly == 0)
3079 call_close |= is_wronly;
3080 else if (is_wronly)
3081 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003082 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3083 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003084 } else if (is_rdwr)
3085 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3086
Trond Myklebust5cc78612016-11-14 11:19:56 -05003087 if (!nfs4_valid_open_stateid(state) ||
3088 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003089 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003090 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003091
3092 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003093 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003094 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003095 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003096
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003097 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003098 nfs_release_seqid(calldata->arg.seqid);
3099 goto out_wait;
3100 }
3101
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003102 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003103 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003104
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003105 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003106 /* Close-to-open cache consistency revalidation */
3107 if (!nfs4_have_delegation(inode, FMODE_READ))
3108 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3109 else
3110 calldata->arg.bitmask = NULL;
3111 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003112
Trond Myklebust6ae37332015-01-30 14:21:14 -05003113 calldata->arg.share_access =
3114 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3115 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003116
Trond Myklebustd8d84982016-12-19 12:14:44 -05003117 if (calldata->res.fattr == NULL)
3118 calldata->arg.bitmask = NULL;
3119 else if (calldata->arg.bitmask == NULL)
3120 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003121 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003122 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003123 &calldata->arg.seq_args,
3124 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003125 task) != 0)
3126 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003127 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003128 return;
3129out_no_action:
3130 task->tk_action = NULL;
3131out_wait:
3132 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133}
3134
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003135static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003136 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003137 .rpc_call_done = nfs4_close_done,
3138 .rpc_release = nfs4_free_closedata,
3139};
3140
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141/*
3142 * It is possible for data to be read/written from a mem-mapped file
3143 * after the sys_close call (which hits the vfs layer as a flush).
3144 * This means that we can't safely call nfsv4 close on a file until
3145 * the inode is cleared. This in turn means that we are not good
3146 * NFSv4 citizens - we do not indicate to the server to update the file's
3147 * share state even when we are done with one of the three share
3148 * stateid's in the inode.
3149 *
3150 * NOTE: Caller must be holding the sp->so_owner semaphore!
3151 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003152int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003154 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003155 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003157 struct nfs4_state_owner *sp = state->owner;
3158 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003159 struct rpc_message msg = {
3160 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3161 .rpc_cred = state->owner->so_cred,
3162 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003163 struct rpc_task_setup task_setup_data = {
3164 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003165 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003166 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003167 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003168 .flags = RPC_TASK_ASYNC,
3169 };
Trond Myklebust95121352005-10-18 14:20:12 -07003170 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003172 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3173 &task_setup_data.rpc_client, &msg);
3174
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003175 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003177 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003178 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003179 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003181 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003183 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3184 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003185 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003186 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003187 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003188 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003189 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003190 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003191 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003192 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003193 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003194 calldata->lr.roc = pnfs_roc(state->inode,
3195 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3196 if (calldata->lr.roc) {
3197 calldata->arg.lr_args = &calldata->lr.arg;
3198 calldata->res.lr_res = &calldata->lr.res;
3199 }
Al Viro643168c2011-06-22 18:20:23 -04003200 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003201
Trond Myklebust1174dd12010-12-21 10:52:24 -05003202 msg.rpc_argp = &calldata->arg;
3203 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003204 task_setup_data.callback_data = calldata;
3205 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003206 if (IS_ERR(task))
3207 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003208 status = 0;
3209 if (wait)
3210 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003211 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003212 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003213out_free_calldata:
3214 kfree(calldata);
3215out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003216 nfs4_put_open_state(state);
3217 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003218 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219}
3220
Trond Myklebust2b484292010-09-17 10:56:51 -04003221static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003222nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3223 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003226 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3227
3228 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Trond Myklebust565277f2007-10-15 18:17:53 -04003230 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003231 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003232
3233 nfs4_label_release_security(label);
3234
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003235 if (IS_ERR(state))
3236 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003237 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238}
3239
Trond Myklebust1185a552009-12-03 15:54:02 -05003240static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003241{
3242 if (ctx->state == NULL)
3243 return;
3244 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003245 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003246 else
Al Viro643168c2011-06-22 18:20:23 -04003247 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003248}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
Trond Myklebustb944dba2013-11-04 15:20:20 -05003250#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3251#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003252#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3255{
Kinglong Mee8c612822015-08-26 21:12:58 +08003256 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003257 struct nfs4_server_caps_arg args = {
3258 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003259 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003260 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 struct nfs4_server_caps_res res = {};
3262 struct rpc_message msg = {
3263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003264 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 .rpc_resp = &res,
3266 };
3267 int status;
3268
Kinglong Mee8c612822015-08-26 21:12:58 +08003269 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3270 FATTR4_WORD0_FH_EXPIRE_TYPE |
3271 FATTR4_WORD0_LINK_SUPPORT |
3272 FATTR4_WORD0_SYMLINK_SUPPORT |
3273 FATTR4_WORD0_ACLSUPPORT;
3274 if (minorversion)
3275 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3276
Bryan Schumaker7c513052011-03-24 17:12:24 +00003277 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003279 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003280 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003281 case 0:
3282 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3283 res.attr_bitmask[2] = 0;
3284 break;
3285 case 1:
3286 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3287 break;
3288 case 2:
3289 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003292 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3293 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3294 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3295 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003296 NFS_CAP_CTIME|NFS_CAP_MTIME|
3297 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003298 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3299 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 server->caps |= NFS_CAP_ACLS;
3301 if (res.has_links != 0)
3302 server->caps |= NFS_CAP_HARDLINKS;
3303 if (res.has_symlinks != 0)
3304 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003305 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3306 server->caps |= NFS_CAP_FILEID;
3307 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3308 server->caps |= NFS_CAP_MODE;
3309 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3310 server->caps |= NFS_CAP_NLINK;
3311 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3312 server->caps |= NFS_CAP_OWNER;
3313 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3314 server->caps |= NFS_CAP_OWNER_GROUP;
3315 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3316 server->caps |= NFS_CAP_ATIME;
3317 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3318 server->caps |= NFS_CAP_CTIME;
3319 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3320 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003321#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3322 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3323 server->caps |= NFS_CAP_SECURITY_LABEL;
3324#endif
3325 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3326 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003327 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003328
Trond Myklebusta65318b2009-03-11 14:10:28 -04003329 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3330 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3331 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003332 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003333 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3334 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003336 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003338
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 return status;
3340}
3341
Trond Myklebust55a97592006-06-09 09:34:19 -04003342int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343{
3344 struct nfs4_exception exception = { };
3345 int err;
3346 do {
3347 err = nfs4_handle_exception(server,
3348 _nfs4_server_capabilities(server, fhandle),
3349 &exception);
3350 } while (exception.retry);
3351 return err;
3352}
3353
3354static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3355 struct nfs_fsinfo *info)
3356{
David Quigleyaa9c2662013-05-22 12:50:44 -04003357 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003359 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 };
3361 struct nfs4_lookup_res res = {
3362 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003363 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 .fh = fhandle,
3365 };
3366 struct rpc_message msg = {
3367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3368 .rpc_argp = &args,
3369 .rpc_resp = &res,
3370 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003371
David Quigleyaa9c2662013-05-22 12:50:44 -04003372 bitmask[0] = nfs4_fattr_bitmap[0];
3373 bitmask[1] = nfs4_fattr_bitmap[1];
3374 /*
3375 * Process the label in the upcoming getfattr
3376 */
3377 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3378
Trond Myklebust0e574af2005-10-27 22:12:38 -04003379 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003380 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381}
3382
3383static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3384 struct nfs_fsinfo *info)
3385{
3386 struct nfs4_exception exception = { };
3387 int err;
3388 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003389 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003390 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003391 switch (err) {
3392 case 0:
3393 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003394 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003395 default:
3396 err = nfs4_handle_exception(server, err, &exception);
3397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003399out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 return err;
3401}
3402
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003403static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3404 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3405{
Trond Myklebustc2190662013-08-26 19:23:04 -04003406 struct rpc_auth_create_args auth_args = {
3407 .pseudoflavor = flavor,
3408 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003409 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003410
Trond Myklebustc2190662013-08-26 19:23:04 -04003411 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003412 if (IS_ERR(auth))
3413 return -EACCES;
3414 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003415}
3416
Chuck Lever9a744ba2013-03-16 15:56:02 -04003417/*
3418 * Retry pseudoroot lookup with various security flavors. We do this when:
3419 *
3420 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3421 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3422 *
3423 * Returns zero on success, or a negative NFS4ERR value, or a
3424 * negative errno value.
3425 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003426static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003427 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003429 /* Per 3530bis 15.33.5 */
3430 static const rpc_authflavor_t flav_array[] = {
3431 RPC_AUTH_GSS_KRB5P,
3432 RPC_AUTH_GSS_KRB5I,
3433 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003434 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003435 RPC_AUTH_NULL,
3436 };
3437 int status = -EPERM;
3438 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003440 if (server->auth_info.flavor_len > 0) {
3441 /* try each flavor specified by user */
3442 for (i = 0; i < server->auth_info.flavor_len; i++) {
3443 status = nfs4_lookup_root_sec(server, fhandle, info,
3444 server->auth_info.flavors[i]);
3445 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3446 continue;
3447 break;
3448 }
3449 } else {
3450 /* no flavors specified by user, try default list */
3451 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3452 status = nfs4_lookup_root_sec(server, fhandle, info,
3453 flav_array[i]);
3454 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3455 continue;
3456 break;
3457 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003458 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003459
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003460 /*
3461 * -EACCESS could mean that the user doesn't have correct permissions
3462 * to access the mount. It could also mean that we tried to mount
3463 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3464 * existing mount programs don't handle -EACCES very well so it should
3465 * be mapped to -EPERM instead.
3466 */
3467 if (status == -EACCES)
3468 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003469 return status;
3470}
3471
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003472/**
3473 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3474 * @server: initialized nfs_server handle
3475 * @fhandle: we fill in the pseudo-fs root file handle
3476 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003477 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003478 *
3479 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003480 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003481int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003482 struct nfs_fsinfo *info,
3483 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003484{
Andre Przywarac7757072015-04-23 17:17:40 +01003485 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003486
Andre Przywarac7757072015-04-23 17:17:40 +01003487 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003488 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003489
3490 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003491 status = server->nfs_client->cl_mvops->find_root_sec(server,
3492 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003493
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 if (status == 0)
3495 status = nfs4_server_capabilities(server, fhandle);
3496 if (status == 0)
3497 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003498
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003499 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500}
3501
Bryan Schumakerbae36242012-05-10 15:07:31 -04003502static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3503 struct nfs_fsinfo *info)
3504{
3505 int error;
3506 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003507 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003508
3509 error = nfs4_server_capabilities(server, mntfh);
3510 if (error < 0) {
3511 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3512 return error;
3513 }
3514
David Quigley14c43f72013-05-22 12:50:43 -04003515 label = nfs4_label_alloc(server, GFP_KERNEL);
3516 if (IS_ERR(label))
3517 return PTR_ERR(label);
3518
David Quigley1775fd32013-05-22 12:50:42 -04003519 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003520 if (error < 0) {
3521 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003522 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003523 }
3524
3525 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3526 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3527 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3528
David Quigley14c43f72013-05-22 12:50:43 -04003529err_free_label:
3530 nfs4_label_free(label);
3531
Bryan Schumakerbae36242012-05-10 15:07:31 -04003532 return error;
3533}
3534
Manoj Naik6b97fd32006-06-09 09:34:29 -04003535/*
3536 * Get locations and (maybe) other attributes of a referral.
3537 * Note that we'll actually follow the referral later when
3538 * we detect fsid mismatch in inode revalidation
3539 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003540static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3541 const struct qstr *name, struct nfs_fattr *fattr,
3542 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003543{
3544 int status = -ENOMEM;
3545 struct page *page = NULL;
3546 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003547
3548 page = alloc_page(GFP_KERNEL);
3549 if (page == NULL)
3550 goto out;
3551 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3552 if (locations == NULL)
3553 goto out;
3554
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003555 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003556 if (status != 0)
3557 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003558
3559 /*
3560 * If the fsid didn't change, this is a migration event, not a
3561 * referral. Cause us to drop into the exception handler, which
3562 * will kick off migration recovery.
3563 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003564 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003565 dprintk("%s: server did not return a different fsid for"
3566 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003567 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003568 goto out;
3569 }
Andy Adamson533eb462011-06-13 18:25:56 -04003570 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3571 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003572
Andy Adamson533eb462011-06-13 18:25:56 -04003573 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003574 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003575 memset(fhandle, 0, sizeof(struct nfs_fh));
3576out:
3577 if (page)
3578 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003579 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003580 return status;
3581}
3582
David Quigley1775fd32013-05-22 12:50:42 -04003583static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3584 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585{
3586 struct nfs4_getattr_arg args = {
3587 .fh = fhandle,
3588 .bitmask = server->attr_bitmask,
3589 };
3590 struct nfs4_getattr_res res = {
3591 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003592 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 .server = server,
3594 };
3595 struct rpc_message msg = {
3596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3597 .rpc_argp = &args,
3598 .rpc_resp = &res,
3599 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003600
3601 args.bitmask = nfs4_bitmask(server, label);
3602
Trond Myklebust0e574af2005-10-27 22:12:38 -04003603 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003604 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605}
3606
David Quigley1775fd32013-05-22 12:50:42 -04003607static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3608 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609{
3610 struct nfs4_exception exception = { };
3611 int err;
3612 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003613 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3614 trace_nfs4_getattr(server, fhandle, fattr, err);
3615 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 &exception);
3617 } while (exception.retry);
3618 return err;
3619}
3620
3621/*
3622 * The file is not closed if it is opened due to the a request to change
3623 * the size of the file. The open call will not be needed once the
3624 * VFS layer lookup-intents are implemented.
3625 *
3626 * Close is called when the inode is destroyed.
3627 * If we haven't opened the file for O_WRONLY, we
3628 * need to in the size_change case to obtain a stateid.
3629 *
3630 * Got race?
3631 * Because OPEN is always done by name in nfsv4, it is
3632 * possible that we opened a different file by the same
3633 * name. We can recognize this race condition, but we
3634 * can't do anything about it besides returning an error.
3635 *
3636 * This will be fixed with VFS changes (lookup-intent).
3637 */
3638static int
3639nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3640 struct iattr *sattr)
3641{
David Howells2b0143b2015-03-17 22:25:59 +00003642 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003643 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003644 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003645 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 int status;
3647
Peng Tao88ac8152014-09-12 11:04:10 +08003648 if (pnfs_ld_layoutret_on_setattr(inode) &&
3649 sattr->ia_valid & ATTR_SIZE &&
3650 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003651 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003652
Trond Myklebust0e574af2005-10-27 22:12:38 -04003653 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
Andy Adamson26699402012-05-30 16:12:24 -04003655 /* Deal with open(O_TRUNC) */
3656 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003657 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003658
3659 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003660 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003661 return 0;
3662
Trond Myklebustd5308382005-11-04 15:33:38 -05003663 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003664 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003665
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003666 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003667 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003668 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003669 }
3670
David Quigley14c43f72013-05-22 12:50:43 -04003671 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3672 if (IS_ERR(label))
3673 return PTR_ERR(label);
3674
NeilBrown29b59f92016-10-13 15:26:47 +11003675 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003676 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003677 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003678 nfs_setsecurity(inode, fattr, label);
3679 }
David Quigley14c43f72013-05-22 12:50:43 -04003680 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 return status;
3682}
3683
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003684static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3685 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003686 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003687{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003688 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003689 int status;
3690 struct nfs4_lookup_arg args = {
3691 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003692 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003693 .name = name,
3694 };
3695 struct nfs4_lookup_res res = {
3696 .server = server,
3697 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003698 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003699 .fh = fhandle,
3700 };
3701 struct rpc_message msg = {
3702 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3703 .rpc_argp = &args,
3704 .rpc_resp = &res,
3705 };
3706
David Quigleyaa9c2662013-05-22 12:50:44 -04003707 args.bitmask = nfs4_bitmask(server, label);
3708
David Howells2b3de442006-08-22 20:06:09 -04003709 nfs_fattr_init(fattr);
3710
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003712 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 dprintk("NFS reply lookup: %d\n", status);
3714 return status;
3715}
3716
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003717static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003718{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003719 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003720 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003721 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3722 fattr->nlink = 2;
3723}
3724
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003725static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003726 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003727 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003728{
3729 struct nfs4_exception exception = { };
3730 struct rpc_clnt *client = *clnt;
3731 int err;
3732 do {
David Quigley1775fd32013-05-22 12:50:42 -04003733 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003734 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003735 switch (err) {
3736 case -NFS4ERR_BADNAME:
3737 err = -ENOENT;
3738 goto out;
3739 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003740 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003741 if (err == -NFS4ERR_MOVED)
3742 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003743 goto out;
3744 case -NFS4ERR_WRONGSEC:
3745 err = -EPERM;
3746 if (client != *clnt)
3747 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003748 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003749 if (IS_ERR(client))
3750 return PTR_ERR(client);
3751
3752 exception.retry = 1;
3753 break;
3754 default:
3755 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3756 }
3757 } while (exception.retry);
3758
3759out:
3760 if (err == 0)
3761 *clnt = client;
3762 else if (client != *clnt)
3763 rpc_shutdown_client(client);
3764
3765 return err;
3766}
3767
Al Virobeffb8f2016-07-20 16:34:42 -04003768static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003769 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3770 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003772 int status;
3773 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003774
David Quigley1775fd32013-05-22 12:50:42 -04003775 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003776 if (client != NFS_CLIENT(dir)) {
3777 rpc_shutdown_client(client);
3778 nfs_fixup_secinfo_attributes(fattr);
3779 }
3780 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781}
3782
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003783struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003784nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003785 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3786{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003787 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003788 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003789
David Quigley1775fd32013-05-22 12:50:42 -04003790 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003791 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003792 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003793 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003794}
3795
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3797{
Trond Myklebust76b32992007-08-10 17:45:11 -04003798 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 struct nfs4_accessargs args = {
3800 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003801 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003803 struct nfs4_accessres res = {
3804 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003805 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 struct rpc_message msg = {
3807 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3808 .rpc_argp = &args,
3809 .rpc_resp = &res,
3810 .rpc_cred = entry->cred,
3811 };
3812 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003813 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
3815 /*
3816 * Determine which access bits we want to ask for...
3817 */
3818 if (mode & MAY_READ)
3819 args.access |= NFS4_ACCESS_READ;
3820 if (S_ISDIR(inode->i_mode)) {
3821 if (mode & MAY_WRITE)
3822 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3823 if (mode & MAY_EXEC)
3824 args.access |= NFS4_ACCESS_LOOKUP;
3825 } else {
3826 if (mode & MAY_WRITE)
3827 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3828 if (mode & MAY_EXEC)
3829 args.access |= NFS4_ACCESS_EXECUTE;
3830 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003831
3832 res.fattr = nfs_alloc_fattr();
3833 if (res.fattr == NULL)
3834 return -ENOMEM;
3835
Bryan Schumaker7c513052011-03-24 17:12:24 +00003836 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003838 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003839 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003841 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 return status;
3843}
3844
3845static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3846{
3847 struct nfs4_exception exception = { };
3848 int err;
3849 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003850 err = _nfs4_proc_access(inode, entry);
3851 trace_nfs4_access(inode, err);
3852 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 &exception);
3854 } while (exception.retry);
3855 return err;
3856}
3857
3858/*
3859 * TODO: For the time being, we don't try to get any attributes
3860 * along with any of the zero-copy operations READ, READDIR,
3861 * READLINK, WRITE.
3862 *
3863 * In the case of the first three, we want to put the GETATTR
3864 * after the read-type operation -- this is because it is hard
3865 * to predict the length of a GETATTR response in v4, and thus
3866 * align the READ data correctly. This means that the GETATTR
3867 * may end up partially falling into the page cache, and we should
3868 * shift it into the 'tail' of the xdr_buf before processing.
3869 * To do this efficiently, we need to know the total length
3870 * of data received, which doesn't seem to be available outside
3871 * of the RPC layer.
3872 *
3873 * In the case of WRITE, we also want to put the GETATTR after
3874 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003875 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 *
3877 * Both of these changes to the XDR layer would in fact be quite
3878 * minor, but I decided to leave them for a subsequent patch.
3879 */
3880static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3881 unsigned int pgbase, unsigned int pglen)
3882{
3883 struct nfs4_readlink args = {
3884 .fh = NFS_FH(inode),
3885 .pgbase = pgbase,
3886 .pglen = pglen,
3887 .pages = &page,
3888 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003889 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 struct rpc_message msg = {
3891 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3892 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003893 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 };
3895
Bryan Schumaker7c513052011-03-24 17:12:24 +00003896 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897}
3898
3899static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3900 unsigned int pgbase, unsigned int pglen)
3901{
3902 struct nfs4_exception exception = { };
3903 int err;
3904 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003905 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3906 trace_nfs4_readlink(inode, err);
3907 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 &exception);
3909 } while (exception.retry);
3910 return err;
3911}
3912
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003914 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916static int
3917nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003918 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003920 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04003921 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003922 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 int status = 0;
3925
NeilBrown532d4de2016-10-13 15:26:47 +11003926 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003927 if (IS_ERR(ctx))
3928 return PTR_ERR(ctx);
3929
David Quigleyaa9c2662013-05-22 12:50:44 -04003930 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3931
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003932 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
3933 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003934 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 if (IS_ERR(state)) {
3936 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003937 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003940 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003941 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 return status;
3943}
3944
Al Virobeffb8f2016-07-20 16:34:42 -04003945static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946{
Trond Myklebust16e42952005-10-27 22:12:44 -04003947 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003948 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003950 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003952 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003953 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003954 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3957 .rpc_argp = &args,
3958 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05003960 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04003961 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003962
Bryan Schumaker7c513052011-03-24 17:12:24 +00003963 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003964 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05003965 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 return status;
3967}
3968
Al Virobeffb8f2016-07-20 16:34:42 -04003969static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970{
3971 struct nfs4_exception exception = { };
3972 int err;
3973 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003974 err = _nfs4_proc_remove(dir, name);
3975 trace_nfs4_remove(dir, name, err);
3976 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 &exception);
3978 } while (exception.retry);
3979 return err;
3980}
3981
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003982static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003984 struct nfs_server *server = NFS_SERVER(dir);
3985 struct nfs_removeargs *args = msg->rpc_argp;
3986 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003988 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003990 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003991
3992 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993}
3994
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003995static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3996{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003997 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003998 &data->args.seq_args,
3999 &data->res.seq_res,
4000 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001}
4002
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004003static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004005 struct nfs_unlinkdata *data = task->tk_calldata;
4006 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004007
Trond Myklebust14516c32010-07-31 14:29:06 -04004008 if (!nfs4_sequence_done(task, &res->seq_res))
4009 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004010 if (nfs4_async_handle_error(task, res->server, NULL,
4011 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004012 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004013 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004014 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004015 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016}
4017
Jeff Laytond3d41522010-09-17 17:31:57 -04004018static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4019{
4020 struct nfs_server *server = NFS_SERVER(dir);
4021 struct nfs_renameargs *arg = msg->rpc_argp;
4022 struct nfs_renameres *res = msg->rpc_resp;
4023
4024 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004025 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004026 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004027}
4028
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004029static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4030{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004031 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004032 &data->args.seq_args,
4033 &data->res.seq_res,
4034 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004035}
4036
4037static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4038 struct inode *new_dir)
4039{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004040 struct nfs_renamedata *data = task->tk_calldata;
4041 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004042
4043 if (!nfs4_sequence_done(task, &res->seq_res))
4044 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004045 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004046 return 0;
4047
Trond Myklebustc733c492017-01-11 12:32:26 -05004048 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004049 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004050 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004051 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004052 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004053 return 1;
4054}
4055
Al Virobeffb8f2016-07-20 16:34:42 -04004056static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004058 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 struct nfs4_link_arg arg = {
4060 .fh = NFS_FH(inode),
4061 .dir_fh = NFS_FH(dir),
4062 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004063 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004065 struct nfs4_link_res res = {
4066 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004067 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004068 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 struct rpc_message msg = {
4070 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4071 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004072 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004074 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075
Trond Myklebust136f2622010-04-16 16:22:49 -04004076 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004077 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004078 goto out;
4079
David Quigley14c43f72013-05-22 12:50:43 -04004080 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4081 if (IS_ERR(res.label)) {
4082 status = PTR_ERR(res.label);
4083 goto out;
4084 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004085 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004086
Bryan Schumaker7c513052011-03-24 17:12:24 +00004087 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004088 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004089 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004090 status = nfs_post_op_update_inode(inode, res.fattr);
4091 if (!status)
4092 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004093 }
David Quigley14c43f72013-05-22 12:50:43 -04004094
4095
4096 nfs4_label_free(res.label);
4097
Trond Myklebust136f2622010-04-16 16:22:49 -04004098out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004099 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 return status;
4101}
4102
Al Virobeffb8f2016-07-20 16:34:42 -04004103static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104{
4105 struct nfs4_exception exception = { };
4106 int err;
4107 do {
4108 err = nfs4_handle_exception(NFS_SERVER(inode),
4109 _nfs4_proc_link(inode, dir, name),
4110 &exception);
4111 } while (exception.retry);
4112 return err;
4113}
4114
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004115struct nfs4_createdata {
4116 struct rpc_message msg;
4117 struct nfs4_create_arg arg;
4118 struct nfs4_create_res res;
4119 struct nfs_fh fh;
4120 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004121 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004122};
4123
4124static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004125 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004126{
4127 struct nfs4_createdata *data;
4128
4129 data = kzalloc(sizeof(*data), GFP_KERNEL);
4130 if (data != NULL) {
4131 struct nfs_server *server = NFS_SERVER(dir);
4132
David Quigley14c43f72013-05-22 12:50:43 -04004133 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4134 if (IS_ERR(data->label))
4135 goto out_free;
4136
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004137 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4138 data->msg.rpc_argp = &data->arg;
4139 data->msg.rpc_resp = &data->res;
4140 data->arg.dir_fh = NFS_FH(dir);
4141 data->arg.server = server;
4142 data->arg.name = name;
4143 data->arg.attrs = sattr;
4144 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004145 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004146 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004147 data->res.server = server;
4148 data->res.fh = &data->fh;
4149 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004150 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004151 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004152 }
4153 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004154out_free:
4155 kfree(data);
4156 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004157}
4158
4159static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4160{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004161 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004162 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004163 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004164 update_changeattr(dir, &data->res.dir_cinfo,
4165 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004166 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004167 }
4168 return status;
4169}
4170
4171static void nfs4_free_createdata(struct nfs4_createdata *data)
4172{
David Quigley14c43f72013-05-22 12:50:43 -04004173 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004174 kfree(data);
4175}
4176
Chuck Lever4f390c12006-08-22 20:06:22 -04004177static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004178 struct page *page, unsigned int len, struct iattr *sattr,
4179 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004181 struct nfs4_createdata *data;
4182 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
Chuck Lever94a6d752006-08-22 20:06:23 -04004184 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004185 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004186
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004187 status = -ENOMEM;
4188 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4189 if (data == NULL)
4190 goto out;
4191
4192 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4193 data->arg.u.symlink.pages = &page;
4194 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004195 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004197 status = nfs4_do_create(dir, dentry, data);
4198
4199 nfs4_free_createdata(data);
4200out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 return status;
4202}
4203
Chuck Lever4f390c12006-08-22 20:06:22 -04004204static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004205 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206{
4207 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004208 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004210
4211 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4212
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004214 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4215 trace_nfs4_symlink(dir, &dentry->d_name, err);
4216 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 &exception);
4218 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004219
4220 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 return err;
4222}
4223
4224static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004225 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004227 struct nfs4_createdata *data;
4228 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004230 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4231 if (data == NULL)
4232 goto out;
4233
David Quigley1775fd32013-05-22 12:50:42 -04004234 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004235 status = nfs4_do_create(dir, dentry, data);
4236
4237 nfs4_free_createdata(data);
4238out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 return status;
4240}
4241
4242static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4243 struct iattr *sattr)
4244{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004245 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004247 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004249
David Quigleyaa9c2662013-05-22 12:50:44 -04004250 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4251
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004252 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4253 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004255 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4256 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4257 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 &exception);
4259 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004260 nfs4_label_release_security(label);
4261
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 return err;
4263}
4264
4265static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004266 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267{
David Howells2b0143b2015-03-17 22:25:59 +00004268 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 struct nfs4_readdir_arg args = {
4270 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004271 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 .pgbase = 0,
4273 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004274 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004275 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 };
4277 struct nfs4_readdir_res res;
4278 struct rpc_message msg = {
4279 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4280 .rpc_argp = &args,
4281 .rpc_resp = &res,
4282 .rpc_cred = cred,
4283 };
4284 int status;
4285
Al Viro6de14722013-09-16 10:53:17 -04004286 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4287 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004288 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004289 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004291 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05004292 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004293 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004294 status += args.pgbase;
4295 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004296
4297 nfs_invalidate_atime(dir);
4298
Harvey Harrison3110ff82008-05-02 13:42:44 -07004299 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 return status;
4301}
4302
4303static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004304 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305{
4306 struct nfs4_exception exception = { };
4307 int err;
4308 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004309 err = _nfs4_proc_readdir(dentry, cred, cookie,
4310 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004311 trace_nfs4_readdir(d_inode(dentry), err);
4312 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 &exception);
4314 } while (exception.retry);
4315 return err;
4316}
4317
4318static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004319 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004321 struct nfs4_createdata *data;
4322 int mode = sattr->ia_mode;
4323 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004325 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4326 if (data == NULL)
4327 goto out;
4328
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004330 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004332 data->arg.ftype = NF4BLK;
4333 data->arg.u.device.specdata1 = MAJOR(rdev);
4334 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 }
4336 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004337 data->arg.ftype = NF4CHR;
4338 data->arg.u.device.specdata1 = MAJOR(rdev);
4339 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004340 } else if (!S_ISSOCK(mode)) {
4341 status = -EINVAL;
4342 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 }
David Quigley1775fd32013-05-22 12:50:42 -04004344
David Quigleyaa9c2662013-05-22 12:50:44 -04004345 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004346 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004347out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004348 nfs4_free_createdata(data);
4349out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 return status;
4351}
4352
4353static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4354 struct iattr *sattr, dev_t rdev)
4355{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004356 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004358 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004360
David Quigleyaa9c2662013-05-22 12:50:44 -04004361 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4362
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004363 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4364 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004366 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4367 trace_nfs4_mknod(dir, &dentry->d_name, err);
4368 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 &exception);
4370 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004371
4372 nfs4_label_release_security(label);
4373
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 return err;
4375}
4376
4377static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4378 struct nfs_fsstat *fsstat)
4379{
4380 struct nfs4_statfs_arg args = {
4381 .fh = fhandle,
4382 .bitmask = server->attr_bitmask,
4383 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004384 struct nfs4_statfs_res res = {
4385 .fsstat = fsstat,
4386 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 struct rpc_message msg = {
4388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4389 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004390 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 };
4392
Trond Myklebust0e574af2005-10-27 22:12:38 -04004393 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004394 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395}
4396
4397static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4398{
4399 struct nfs4_exception exception = { };
4400 int err;
4401 do {
4402 err = nfs4_handle_exception(server,
4403 _nfs4_proc_statfs(server, fhandle, fsstat),
4404 &exception);
4405 } while (exception.retry);
4406 return err;
4407}
4408
4409static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4410 struct nfs_fsinfo *fsinfo)
4411{
4412 struct nfs4_fsinfo_arg args = {
4413 .fh = fhandle,
4414 .bitmask = server->attr_bitmask,
4415 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004416 struct nfs4_fsinfo_res res = {
4417 .fsinfo = fsinfo,
4418 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 struct rpc_message msg = {
4420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4421 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004422 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 };
4424
Bryan Schumaker7c513052011-03-24 17:12:24 +00004425 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426}
4427
4428static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4429{
4430 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004431 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 int err;
4433
4434 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004435 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004436 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004437 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004438 nfs4_set_lease_period(server->nfs_client,
4439 fsinfo->lease_time * HZ,
4440 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004441 break;
4442 }
4443 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 } while (exception.retry);
4445 return err;
4446}
4447
4448static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4449{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004450 int error;
4451
Trond Myklebust0e574af2005-10-27 22:12:38 -04004452 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004453 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004454 if (error == 0) {
4455 /* block layout checks this! */
4456 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004457 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004458 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004459
4460 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461}
4462
4463static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4464 struct nfs_pathconf *pathconf)
4465{
4466 struct nfs4_pathconf_arg args = {
4467 .fh = fhandle,
4468 .bitmask = server->attr_bitmask,
4469 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004470 struct nfs4_pathconf_res res = {
4471 .pathconf = pathconf,
4472 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 struct rpc_message msg = {
4474 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4475 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004476 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 };
4478
4479 /* None of the pathconf attributes are mandatory to implement */
4480 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4481 memset(pathconf, 0, sizeof(*pathconf));
4482 return 0;
4483 }
4484
Trond Myklebust0e574af2005-10-27 22:12:38 -04004485 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004486 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487}
4488
4489static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4490 struct nfs_pathconf *pathconf)
4491{
4492 struct nfs4_exception exception = { };
4493 int err;
4494
4495 do {
4496 err = nfs4_handle_exception(server,
4497 _nfs4_proc_pathconf(server, fhandle, pathconf),
4498 &exception);
4499 } while (exception.retry);
4500 return err;
4501}
4502
Trond Myklebust5521abf2013-03-16 20:54:34 -04004503int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004504 const struct nfs_open_context *ctx,
4505 const struct nfs_lock_context *l_ctx,
4506 fmode_t fmode)
4507{
NeilBrown17393472016-10-13 15:26:47 +11004508 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004509}
4510EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4511
Trond Myklebust5521abf2013-03-16 20:54:34 -04004512static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4513 const struct nfs_open_context *ctx,
4514 const struct nfs_lock_context *l_ctx,
4515 fmode_t fmode)
4516{
4517 nfs4_stateid current_stateid;
4518
Trond Myklebuste1253be2014-03-05 08:44:23 -05004519 /* If the current stateid represents a lost lock, then exit */
4520 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4521 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004522 return nfs4_stateid_match(stateid, &current_stateid);
4523}
4524
4525static bool nfs4_error_stateid_expired(int err)
4526{
4527 switch (err) {
4528 case -NFS4ERR_DELEG_REVOKED:
4529 case -NFS4ERR_ADMIN_REVOKED:
4530 case -NFS4ERR_BAD_STATEID:
4531 case -NFS4ERR_STALE_STATEID:
4532 case -NFS4ERR_OLD_STATEID:
4533 case -NFS4ERR_OPENMODE:
4534 case -NFS4ERR_EXPIRED:
4535 return true;
4536 }
4537 return false;
4538}
4539
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004540static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004542 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004544 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004545 if (task->tk_status < 0) {
4546 struct nfs4_exception exception = {
4547 .inode = hdr->inode,
4548 .state = hdr->args.context->state,
4549 .stateid = &hdr->args.stateid,
4550 };
4551 task->tk_status = nfs4_async_handle_exception(task,
4552 server, task->tk_status, &exception);
4553 if (exception.retry) {
4554 rpc_restart_call_prepare(task);
4555 return -EAGAIN;
4556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004558
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004560 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004561 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562}
4563
Trond Myklebust5521abf2013-03-16 20:54:34 -04004564static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004565 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004566{
4567
4568 if (!nfs4_error_stateid_expired(task->tk_status) ||
4569 nfs4_stateid_is_current(&args->stateid,
4570 args->context,
4571 args->lock_context,
4572 FMODE_READ))
4573 return false;
4574 rpc_restart_call_prepare(task);
4575 return true;
4576}
4577
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004578static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004579{
4580
4581 dprintk("--> %s\n", __func__);
4582
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004583 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004584 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004585 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004586 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004587 if (task->tk_status > 0)
4588 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004589 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4590 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004591}
4592
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004593static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4594 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004596 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004597 if (!hdr->pgio_done_cb)
4598 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004599 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004600 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601}
4602
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004603static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4604 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004605{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004606 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004607 &hdr->args.seq_args,
4608 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004609 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004610 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004611 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4612 hdr->args.lock_context,
4613 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004614 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004615 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004616 return -EIO;
4617 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618}
4619
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004620static int nfs4_write_done_cb(struct rpc_task *task,
4621 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004623 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004624
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004625 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004626 if (task->tk_status < 0) {
4627 struct nfs4_exception exception = {
4628 .inode = hdr->inode,
4629 .state = hdr->args.context->state,
4630 .stateid = &hdr->args.stateid,
4631 };
4632 task->tk_status = nfs4_async_handle_exception(task,
4633 NFS_SERVER(inode), task->tk_status,
4634 &exception);
4635 if (exception.retry) {
4636 rpc_restart_call_prepare(task);
4637 return -EAGAIN;
4638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004640 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004641 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004642 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004643 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004644 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645}
4646
Trond Myklebust5521abf2013-03-16 20:54:34 -04004647static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004648 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004649{
4650
4651 if (!nfs4_error_stateid_expired(task->tk_status) ||
4652 nfs4_stateid_is_current(&args->stateid,
4653 args->context,
4654 args->lock_context,
4655 FMODE_WRITE))
4656 return false;
4657 rpc_restart_call_prepare(task);
4658 return true;
4659}
4660
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004661static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004662{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004663 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004664 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004665 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004666 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004667 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4668 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004669}
4670
Trond Myklebust5a37f852012-04-28 14:55:16 -04004671static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004672bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004673{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004674 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004675 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004676 return false;
4677 /* Otherwise, request attributes if and only if we don't hold
4678 * a delegation
4679 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004680 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004681}
Fred Isamana69aef12011-03-03 15:13:47 +00004682
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004683static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4684 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004686 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004687
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004688 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4689 hdr->args.bitmask = NULL;
4690 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004691 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004692 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004693
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004694 if (!hdr->pgio_done_cb)
4695 hdr->pgio_done_cb = nfs4_write_done_cb;
4696 hdr->res.server = server;
4697 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004699 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004700 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701}
4702
Fred Isaman0b7c0152012-04-20 14:47:39 -04004703static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4704{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004705 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004706 &data->args.seq_args,
4707 &data->res.seq_res,
4708 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709}
4710
Fred Isaman0b7c0152012-04-20 14:47:39 -04004711static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004714
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004715 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004716 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4717 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004718 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004719 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004721 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722}
4723
Fred Isaman0b7c0152012-04-20 14:47:39 -04004724static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004725{
4726 if (!nfs4_sequence_done(task, &data->res.seq_res))
4727 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004728 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004729}
4730
Fred Isaman0b7c0152012-04-20 14:47:39 -04004731static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004733 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004734
Fred Isaman0b7c0152012-04-20 14:47:39 -04004735 if (data->commit_done_cb == NULL)
4736 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004737 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004738 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004739 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740}
4741
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004742struct nfs4_renewdata {
4743 struct nfs_client *client;
4744 unsigned long timestamp;
4745};
4746
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747/*
4748 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4749 * standalone procedure for queueing an asynchronous RENEW.
4750 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004751static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004752{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004753 struct nfs4_renewdata *data = calldata;
4754 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004755
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004756 if (atomic_read(&clp->cl_count) > 1)
4757 nfs4_schedule_state_renewal(clp);
4758 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004759 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004760}
4761
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004762static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004764 struct nfs4_renewdata *data = calldata;
4765 struct nfs_client *clp = data->client;
4766 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004768 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004769 switch (task->tk_status) {
4770 case 0:
4771 break;
4772 case -NFS4ERR_LEASE_MOVED:
4773 nfs4_schedule_lease_moved_recovery(clp);
4774 break;
4775 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004776 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004777 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4778 return;
4779 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004780 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004781 return;
4782 }
4783 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004785 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786}
4787
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004788static const struct rpc_call_ops nfs4_renew_ops = {
4789 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004790 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004791};
4792
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004793static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794{
4795 struct rpc_message msg = {
4796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4797 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004798 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004800 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004802 if (renew_flags == 0)
4803 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004804 if (!atomic_inc_not_zero(&clp->cl_count))
4805 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004806 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004807 if (data == NULL)
4808 return -ENOMEM;
4809 data->client = clp;
4810 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004811 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004812 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813}
4814
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004815static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816{
4817 struct rpc_message msg = {
4818 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4819 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004820 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 };
4822 unsigned long now = jiffies;
4823 int status;
4824
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004825 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 if (status < 0)
4827 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004828 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 return 0;
4830}
4831
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004832static inline int nfs4_server_supports_acls(struct nfs_server *server)
4833{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004834 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004835}
4836
Trond Myklebust21f498c2012-08-24 10:59:25 -04004837/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4838 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004839 * the stack.
4840 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004841#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004842
Neil Hormane9e3d722011-03-04 19:26:03 -05004843static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004844 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004845{
4846 struct page *newpage, **spages;
4847 int rc = 0;
4848 size_t len;
4849 spages = pages;
4850
4851 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004852 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004853 newpage = alloc_page(GFP_KERNEL);
4854
4855 if (newpage == NULL)
4856 goto unwind;
4857 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05004858 buf += len;
4859 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05004860 *pages++ = newpage;
4861 rc++;
4862 } while (buflen != 0);
4863
4864 return rc;
4865
4866unwind:
4867 for(; rc > 0; rc--)
4868 __free_page(spages[rc-1]);
4869 return -ENOMEM;
4870}
4871
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004872struct nfs4_cached_acl {
4873 int cached;
4874 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004875 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004876};
4877
4878static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004879{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004880 struct nfs_inode *nfsi = NFS_I(inode);
4881
4882 spin_lock(&inode->i_lock);
4883 kfree(nfsi->nfs4_acl);
4884 nfsi->nfs4_acl = acl;
4885 spin_unlock(&inode->i_lock);
4886}
4887
4888static void nfs4_zap_acl_attr(struct inode *inode)
4889{
4890 nfs4_set_cached_acl(inode, NULL);
4891}
4892
4893static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4894{
4895 struct nfs_inode *nfsi = NFS_I(inode);
4896 struct nfs4_cached_acl *acl;
4897 int ret = -ENOENT;
4898
4899 spin_lock(&inode->i_lock);
4900 acl = nfsi->nfs4_acl;
4901 if (acl == NULL)
4902 goto out;
4903 if (buf == NULL) /* user is just asking for length */
4904 goto out_len;
4905 if (acl->cached == 0)
4906 goto out;
4907 ret = -ERANGE; /* see getxattr(2) man page */
4908 if (acl->len > buflen)
4909 goto out;
4910 memcpy(buf, acl->data, acl->len);
4911out_len:
4912 ret = acl->len;
4913out:
4914 spin_unlock(&inode->i_lock);
4915 return ret;
4916}
4917
Sachin Prabhu5794d212012-04-17 14:36:40 +01004918static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004919{
4920 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004921 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004922
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004923 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004924 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004925 if (acl == NULL)
4926 goto out;
4927 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004928 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004929 } else {
4930 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4931 if (acl == NULL)
4932 goto out;
4933 acl->cached = 0;
4934 }
4935 acl->len = acl_len;
4936out:
4937 nfs4_set_cached_acl(inode, acl);
4938}
4939
Andy Adamsonbf118a32011-12-07 11:55:27 -05004940/*
4941 * The getxattr API returns the required buffer length when called with a
4942 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4943 * the required buf. On a NULL buf, we send a page of data to the server
4944 * guessing that the ACL request can be serviced by a page. If so, we cache
4945 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4946 * the cache. If not so, we throw away the page, and cache the required
4947 * length. The next getxattr call will then produce another round trip to
4948 * the server, this time with the input buf of the required size.
4949 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004950static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004951{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05004952 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004953 struct nfs_getaclargs args = {
4954 .fh = NFS_FH(inode),
4955 .acl_pages = pages,
4956 .acl_len = buflen,
4957 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004958 struct nfs_getaclres res = {
4959 .acl_len = buflen,
4960 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004961 struct rpc_message msg = {
4962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4963 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004964 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004965 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05004966 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004967 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004968
Trond Myklebust21f498c2012-08-24 10:59:25 -04004969 if (npages > ARRAY_SIZE(pages))
4970 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004971
Andy Adamsonbf118a32011-12-07 11:55:27 -05004972 for (i = 0; i < npages; i++) {
4973 pages[i] = alloc_page(GFP_KERNEL);
4974 if (!pages[i])
4975 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004976 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004977
4978 /* for decoding across pages */
4979 res.acl_scratch = alloc_page(GFP_KERNEL);
4980 if (!res.acl_scratch)
4981 goto out_free;
4982
Andy Adamsonbf118a32011-12-07 11:55:27 -05004983 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004984
Peng Taode040be2012-01-10 22:42:47 +08004985 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004986 __func__, buf, buflen, npages, args.acl_len);
4987 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4988 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004989 if (ret)
4990 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004991
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004992 /* Handle the case where the passed-in buffer is too short */
4993 if (res.acl_flags & NFS4_ACL_TRUNC) {
4994 /* Did the user only issue a request for the acl length? */
4995 if (buf == NULL)
4996 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004997 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004998 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004999 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005000 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005001 if (buf) {
5002 if (res.acl_len > buflen) {
5003 ret = -ERANGE;
5004 goto out_free;
5005 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005006 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005007 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005008out_ok:
5009 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005010out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005011 for (i = 0; i < npages; i++)
5012 if (pages[i])
5013 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005014 if (res.acl_scratch)
5015 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005016 return ret;
5017}
5018
Trond Myklebust16b42892006-08-24 12:27:15 -04005019static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5020{
5021 struct nfs4_exception exception = { };
5022 ssize_t ret;
5023 do {
5024 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005025 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005026 if (ret >= 0)
5027 break;
5028 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5029 } while (exception.retry);
5030 return ret;
5031}
5032
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005033static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5034{
5035 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005036 int ret;
5037
5038 if (!nfs4_server_supports_acls(server))
5039 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005040 ret = nfs_revalidate_inode(server, inode);
5041 if (ret < 0)
5042 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005043 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5044 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005045 ret = nfs4_read_cached_acl(inode, buf, buflen);
5046 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005047 /* -ENOENT is returned if there is no ACL or if there is an ACL
5048 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005049 return ret;
5050 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005051}
5052
Trond Myklebust16b42892006-08-24 12:27:15 -04005053static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005054{
5055 struct nfs_server *server = NFS_SERVER(inode);
5056 struct page *pages[NFS4ACL_MAXPAGES];
5057 struct nfs_setaclargs arg = {
5058 .fh = NFS_FH(inode),
5059 .acl_pages = pages,
5060 .acl_len = buflen,
5061 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005062 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005063 struct rpc_message msg = {
5064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5065 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005066 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005067 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005068 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005069 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005070
5071 if (!nfs4_server_supports_acls(server))
5072 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005073 if (npages > ARRAY_SIZE(pages))
5074 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005075 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005076 if (i < 0)
5077 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005078 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005079 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005080
5081 /*
5082 * Free each page after tx, so the only ref left is
5083 * held by the network stack
5084 */
5085 for (; i > 0; i--)
5086 put_page(pages[i-1]);
5087
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005088 /*
5089 * Acl update can result in inode attribute update.
5090 * so mark the attribute cache invalid.
5091 */
5092 spin_lock(&inode->i_lock);
5093 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5094 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005095 nfs_access_zap_cache(inode);
5096 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005097 return ret;
5098}
5099
Trond Myklebust16b42892006-08-24 12:27:15 -04005100static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5101{
5102 struct nfs4_exception exception = { };
5103 int err;
5104 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005105 err = __nfs4_proc_set_acl(inode, buf, buflen);
5106 trace_nfs4_set_acl(inode, err);
5107 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005108 &exception);
5109 } while (exception.retry);
5110 return err;
5111}
5112
David Quigleyaa9c2662013-05-22 12:50:44 -04005113#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5114static int _nfs4_get_security_label(struct inode *inode, void *buf,
5115 size_t buflen)
5116{
5117 struct nfs_server *server = NFS_SERVER(inode);
5118 struct nfs_fattr fattr;
5119 struct nfs4_label label = {0, 0, buflen, buf};
5120
5121 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005122 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005123 .fh = NFS_FH(inode),
5124 .bitmask = bitmask,
5125 };
5126 struct nfs4_getattr_res res = {
5127 .fattr = &fattr,
5128 .label = &label,
5129 .server = server,
5130 };
5131 struct rpc_message msg = {
5132 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005133 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005134 .rpc_resp = &res,
5135 };
5136 int ret;
5137
5138 nfs_fattr_init(&fattr);
5139
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005140 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005141 if (ret)
5142 return ret;
5143 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5144 return -ENOENT;
5145 if (buflen < label.len)
5146 return -ERANGE;
5147 return 0;
5148}
5149
5150static int nfs4_get_security_label(struct inode *inode, void *buf,
5151 size_t buflen)
5152{
5153 struct nfs4_exception exception = { };
5154 int err;
5155
5156 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5157 return -EOPNOTSUPP;
5158
5159 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005160 err = _nfs4_get_security_label(inode, buf, buflen);
5161 trace_nfs4_get_security_label(inode, err);
5162 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005163 &exception);
5164 } while (exception.retry);
5165 return err;
5166}
5167
5168static int _nfs4_do_set_security_label(struct inode *inode,
5169 struct nfs4_label *ilabel,
5170 struct nfs_fattr *fattr,
5171 struct nfs4_label *olabel)
5172{
5173
5174 struct iattr sattr = {0};
5175 struct nfs_server *server = NFS_SERVER(inode);
5176 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005177 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005178 .fh = NFS_FH(inode),
5179 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005180 .server = server,
5181 .bitmask = bitmask,
5182 .label = ilabel,
5183 };
5184 struct nfs_setattrres res = {
5185 .fattr = fattr,
5186 .label = olabel,
5187 .server = server,
5188 };
5189 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005190 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5191 .rpc_argp = &arg,
5192 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005193 };
5194 int status;
5195
Jeff Layton12207f62013-11-01 10:49:32 -04005196 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005197
Jeff Layton12207f62013-11-01 10:49:32 -04005198 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005199 if (status)
5200 dprintk("%s failed: %d\n", __func__, status);
5201
5202 return status;
5203}
5204
5205static int nfs4_do_set_security_label(struct inode *inode,
5206 struct nfs4_label *ilabel,
5207 struct nfs_fattr *fattr,
5208 struct nfs4_label *olabel)
5209{
5210 struct nfs4_exception exception = { };
5211 int err;
5212
5213 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005214 err = _nfs4_do_set_security_label(inode, ilabel,
5215 fattr, olabel);
5216 trace_nfs4_set_security_label(inode, err);
5217 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005218 &exception);
5219 } while (exception.retry);
5220 return err;
5221}
5222
5223static int
Al Viro59301222016-05-27 10:19:30 -04005224nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005225{
5226 struct nfs4_label ilabel, *olabel = NULL;
5227 struct nfs_fattr fattr;
5228 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005229 int status;
5230
5231 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5232 return -EOPNOTSUPP;
5233
5234 nfs_fattr_init(&fattr);
5235
5236 ilabel.pi = 0;
5237 ilabel.lfs = 0;
5238 ilabel.label = (char *)buf;
5239 ilabel.len = buflen;
5240
5241 cred = rpc_lookup_cred();
5242 if (IS_ERR(cred))
5243 return PTR_ERR(cred);
5244
5245 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5246 if (IS_ERR(olabel)) {
5247 status = -PTR_ERR(olabel);
5248 goto out;
5249 }
5250
5251 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5252 if (status == 0)
5253 nfs_setsecurity(inode, &fattr, olabel);
5254
5255 nfs4_label_free(olabel);
5256out:
5257 put_rpccred(cred);
5258 return status;
5259}
5260#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5261
5262
Chuck Leverf0920752012-05-21 22:45:41 -04005263static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5264 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005265{
5266 __be32 verf[2];
5267
Chuck Lever2c820d92012-05-21 22:45:33 -04005268 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5269 /* An impossible timestamp guarantees this value
5270 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005271 verf[0] = cpu_to_be32(U32_MAX);
5272 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005273 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005274 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005275 u64 ns = ktime_to_ns(nn->boot_time);
5276
5277 verf[0] = cpu_to_be32(ns >> 32);
5278 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005279 }
Chuck Levercd937102012-03-02 17:14:31 -05005280 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5281}
5282
Jeff Laytona3192682015-06-09 19:43:59 -04005283static int
5284nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005285{
Jeff Laytona3192682015-06-09 19:43:59 -04005286 size_t len;
5287 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005288
Trond Myklebustceb3a162015-01-03 15:16:04 -05005289 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005290 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005291
Jeff Laytona3192682015-06-09 19:43:59 -04005292 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005293 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005294 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5295 1 +
5296 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5297 1;
5298 rcu_read_unlock();
5299
5300 if (len > NFS4_OPAQUE_LIMIT + 1)
5301 return -EINVAL;
5302
5303 /*
5304 * Since this string is allocated at mount time, and held until the
5305 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5306 * about a memory-reclaim deadlock.
5307 */
5308 str = kmalloc(len, GFP_KERNEL);
5309 if (!str)
5310 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005311
Chuck Levere984a552012-09-14 17:24:21 -04005312 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005313 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005314 clp->cl_ipaddr,
5315 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5316 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005317 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005318
Jeff Laytona3192682015-06-09 19:43:59 -04005319 clp->cl_owner_id = str;
5320 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005321}
5322
Jeff Layton873e3852015-06-09 19:44:00 -04005323static int
5324nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005325{
Jeff Layton873e3852015-06-09 19:44:00 -04005326 size_t len;
5327 char *str;
5328
5329 len = 10 + 10 + 1 + 10 + 1 +
5330 strlen(nfs4_client_id_uniquifier) + 1 +
5331 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5332
5333 if (len > NFS4_OPAQUE_LIMIT + 1)
5334 return -EINVAL;
5335
5336 /*
5337 * Since this string is allocated at mount time, and held until the
5338 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5339 * about a memory-reclaim deadlock.
5340 */
5341 str = kmalloc(len, GFP_KERNEL);
5342 if (!str)
5343 return -ENOMEM;
5344
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005345 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005346 clp->rpc_ops->version, clp->cl_minorversion,
5347 nfs4_client_id_uniquifier,
5348 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005349 clp->cl_owner_id = str;
5350 return 0;
5351}
5352
5353static int
5354nfs4_init_uniform_client_string(struct nfs_client *clp)
5355{
Jeff Layton873e3852015-06-09 19:44:00 -04005356 size_t len;
5357 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005358
5359 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005360 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005361
5362 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005363 return nfs4_init_uniquifier_client_string(clp);
5364
5365 len = 10 + 10 + 1 + 10 + 1 +
5366 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5367
5368 if (len > NFS4_OPAQUE_LIMIT + 1)
5369 return -EINVAL;
5370
5371 /*
5372 * Since this string is allocated at mount time, and held until the
5373 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5374 * about a memory-reclaim deadlock.
5375 */
5376 str = kmalloc(len, GFP_KERNEL);
5377 if (!str)
5378 return -ENOMEM;
5379
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005380 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005381 clp->rpc_ops->version, clp->cl_minorversion,
5382 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005383 clp->cl_owner_id = str;
5384 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005385}
5386
Chuck Lever706cb8d2014-03-12 12:51:47 -04005387/*
5388 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5389 * services. Advertise one based on the address family of the
5390 * clientaddr.
5391 */
5392static unsigned int
5393nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5394{
5395 if (strchr(clp->cl_ipaddr, ':') != NULL)
5396 return scnprintf(buf, len, "tcp6");
5397 else
5398 return scnprintf(buf, len, "tcp");
5399}
5400
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005401static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5402{
5403 struct nfs4_setclientid *sc = calldata;
5404
5405 if (task->tk_status == 0)
5406 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5407}
5408
5409static const struct rpc_call_ops nfs4_setclientid_ops = {
5410 .rpc_call_done = nfs4_setclientid_done,
5411};
5412
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005413/**
5414 * nfs4_proc_setclientid - Negotiate client ID
5415 * @clp: state data structure
5416 * @program: RPC program for NFSv4 callback service
5417 * @port: IP port number for NFS4 callback service
5418 * @cred: RPC credential to use for this call
5419 * @res: where to place the result
5420 *
5421 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5422 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005423int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5424 unsigned short port, struct rpc_cred *cred,
5425 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426{
5427 nfs4_verifier sc_verifier;
5428 struct nfs4_setclientid setclientid = {
5429 .sc_verifier = &sc_verifier,
5430 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005431 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 };
5433 struct rpc_message msg = {
5434 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5435 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005436 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005437 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005439 struct rpc_task *task;
5440 struct rpc_task_setup task_setup_data = {
5441 .rpc_client = clp->cl_rpcclient,
5442 .rpc_message = &msg,
5443 .callback_ops = &nfs4_setclientid_ops,
5444 .callback_data = &setclientid,
5445 .flags = RPC_TASK_TIMEOUT,
5446 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005447 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448
Chuck Leverde734832012-07-11 16:30:50 -04005449 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005450 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005451
5452 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5453 status = nfs4_init_uniform_client_string(clp);
5454 else
Jeff Laytona3192682015-06-09 19:43:59 -04005455 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005456
5457 if (status)
5458 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005459
Chuck Leverde734832012-07-11 16:30:50 -04005460 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005461 setclientid.sc_netid_len =
5462 nfs4_init_callback_netid(clp,
5463 setclientid.sc_netid,
5464 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005465 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005466 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 clp->cl_ipaddr, port >> 8, port & 255);
5468
Jeff Layton3a6bb732015-06-09 19:43:57 -04005469 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005470 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005471 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005472 task = rpc_run_task(&task_setup_data);
5473 if (IS_ERR(task)) {
5474 status = PTR_ERR(task);
5475 goto out;
5476 }
5477 status = task->tk_status;
5478 if (setclientid.sc_cred) {
5479 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5480 put_rpccred(setclientid.sc_cred);
5481 }
5482 rpc_put_task(task);
5483out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005484 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005485 dprintk("NFS reply setclientid: %d\n", status);
5486 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487}
5488
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005489/**
5490 * nfs4_proc_setclientid_confirm - Confirm client ID
5491 * @clp: state data structure
5492 * @res: result of a previous SETCLIENTID
5493 * @cred: RPC credential to use for this call
5494 *
5495 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5496 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005497int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005498 struct nfs4_setclientid_res *arg,
5499 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 struct rpc_message msg = {
5502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005503 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005504 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 int status;
5507
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005508 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5509 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5510 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005511 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005512 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005513 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 return status;
5515}
5516
Trond Myklebustfe650402006-01-03 09:55:18 +01005517struct nfs4_delegreturndata {
5518 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005519 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005520 struct nfs_fh fh;
5521 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005522 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005523 struct {
5524 struct nfs4_layoutreturn_args arg;
5525 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005526 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005527 u32 roc_barrier;
5528 bool roc;
5529 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005530 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005531 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005532 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005533};
5534
Trond Myklebustfe650402006-01-03 09:55:18 +01005535static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5536{
5537 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005538
Trond Myklebust14516c32010-07-31 14:29:06 -04005539 if (!nfs4_sequence_done(task, &data->res.seq_res))
5540 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005541
Trond Myklebustca8acf82013-08-13 10:36:56 -04005542 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005543
5544 /* Handle Layoutreturn errors */
5545 if (data->args.lr_args && task->tk_status != 0) {
5546 switch(data->res.lr_ret) {
5547 default:
5548 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5549 break;
5550 case 0:
5551 data->args.lr_args = NULL;
5552 data->res.lr_res = NULL;
5553 break;
5554 case -NFS4ERR_ADMIN_REVOKED:
5555 case -NFS4ERR_DELEG_REVOKED:
5556 case -NFS4ERR_EXPIRED:
5557 case -NFS4ERR_BAD_STATEID:
5558 case -NFS4ERR_OLD_STATEID:
5559 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5560 case -NFS4ERR_WRONG_CRED:
5561 data->args.lr_args = NULL;
5562 data->res.lr_res = NULL;
5563 data->res.lr_ret = 0;
5564 rpc_restart_call_prepare(task);
5565 return;
5566 }
5567 }
5568
Ricardo Labiaga79708862009-12-07 09:23:21 -05005569 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005570 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005571 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005572 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005573 case -NFS4ERR_ADMIN_REVOKED:
5574 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005575 case -NFS4ERR_EXPIRED:
5576 nfs4_free_revoked_stateid(data->res.server,
5577 data->args.stateid,
5578 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005579 case -NFS4ERR_BAD_STATEID:
5580 case -NFS4ERR_OLD_STATEID:
5581 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005582 task->tk_status = 0;
5583 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005584 case -NFS4ERR_ACCESS:
5585 if (data->args.bitmask) {
5586 data->args.bitmask = NULL;
5587 data->res.fattr = NULL;
5588 task->tk_status = 0;
5589 rpc_restart_call_prepare(task);
5590 return;
5591 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005592 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005593 if (nfs4_async_handle_error(task, data->res.server,
5594 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005595 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005596 return;
5597 }
5598 }
5599 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005600}
5601
5602static void nfs4_delegreturn_release(void *calldata)
5603{
Peng Tao039b7562014-07-03 13:05:02 +08005604 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005605 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005606
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005607 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005608 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005609 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5610 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005611 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005612 nfs_iput_and_deactive(inode);
5613 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005614 kfree(calldata);
5615}
5616
Andy Adamson938e1012009-04-01 09:22:28 -04005617static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5618{
5619 struct nfs4_delegreturndata *d_data;
5620
5621 d_data = (struct nfs4_delegreturndata *)data;
5622
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005623 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005624 return;
5625
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005626 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005627 &d_data->args.seq_args,
5628 &d_data->res.seq_res,
5629 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005630}
Andy Adamson938e1012009-04-01 09:22:28 -04005631
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005632static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005633 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005634 .rpc_call_done = nfs4_delegreturn_done,
5635 .rpc_release = nfs4_delegreturn_release,
5636};
5637
Trond Myklebuste6f81072008-01-24 18:14:34 -05005638static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005639{
5640 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005641 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005642 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005643 struct rpc_message msg = {
5644 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5645 .rpc_cred = cred,
5646 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005647 struct rpc_task_setup task_setup_data = {
5648 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005649 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005650 .callback_ops = &nfs4_delegreturn_ops,
5651 .flags = RPC_TASK_ASYNC,
5652 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005653 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005654
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005655 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005656 if (data == NULL)
5657 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005658 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005659
5660 nfs4_state_protect(server->nfs_client,
5661 NFS_SP4_MACH_CRED_CLEANUP,
5662 &task_setup_data.rpc_client, &msg);
5663
Trond Myklebustfe650402006-01-03 09:55:18 +01005664 data->args.fhandle = &data->fh;
5665 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005666 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005667 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005668 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005669 data->res.fattr = &data->fattr;
5670 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005671 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005672 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005673 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005674 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005675 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005676 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005677 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005678 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005679 if (data->lr.roc) {
5680 data->args.lr_args = &data->lr.arg;
5681 data->res.lr_res = &data->lr.res;
5682 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005683 } else if (data->lr.roc) {
5684 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5685 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005686 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005687
Trond Myklebustc970aa82007-07-14 15:39:59 -04005688 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005689 msg.rpc_argp = &data->args;
5690 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005691 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005692 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005693 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005694 if (!issync)
5695 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005696 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005697 if (status != 0)
5698 goto out;
5699 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005700out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005701 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005702 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703}
5704
Trond Myklebuste6f81072008-01-24 18:14:34 -05005705int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706{
5707 struct nfs_server *server = NFS_SERVER(inode);
5708 struct nfs4_exception exception = { };
5709 int err;
5710 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005711 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005712 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 switch (err) {
5714 case -NFS4ERR_STALE_STATEID:
5715 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 case 0:
5717 return 0;
5718 }
5719 err = nfs4_handle_exception(server, err, &exception);
5720 } while (exception.retry);
5721 return err;
5722}
5723
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5725{
5726 struct inode *inode = state->inode;
5727 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005728 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005729 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005731 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005733 struct nfs_lockt_res res = {
5734 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 };
5736 struct rpc_message msg = {
5737 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005738 .rpc_argp = &arg,
5739 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 .rpc_cred = state->owner->so_cred,
5741 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 struct nfs4_lock_state *lsp;
5743 int status;
5744
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005745 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005746 status = nfs4_set_lock_state(state, request);
5747 if (status != 0)
5748 goto out;
5749 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005750 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005751 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005752 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005753 switch (status) {
5754 case 0:
5755 request->fl_type = F_UNLCK;
5756 break;
5757 case -NFS4ERR_DENIED:
5758 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005760 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005761 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005762out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 return status;
5764}
5765
5766static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5767{
5768 struct nfs4_exception exception = { };
5769 int err;
5770
5771 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005772 err = _nfs4_proc_getlk(state, cmd, request);
5773 trace_nfs4_get_lock(request, state, cmd, err);
5774 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 &exception);
5776 } while (exception.retry);
5777 return err;
5778}
5779
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005780struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005781 struct nfs_locku_args arg;
5782 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005783 struct nfs4_lock_state *lsp;
5784 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005785 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005786 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005787 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005788};
5789
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005790static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5791 struct nfs_open_context *ctx,
5792 struct nfs4_lock_state *lsp,
5793 struct nfs_seqid *seqid)
5794{
5795 struct nfs4_unlockdata *p;
5796 struct inode *inode = lsp->ls_state->inode;
5797
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005798 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005799 if (p == NULL)
5800 return NULL;
5801 p->arg.fh = NFS_FH(inode);
5802 p->arg.fl = &p->fl;
5803 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005804 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005805 p->lsp = lsp;
5806 atomic_inc(&lsp->ls_count);
5807 /* Ensure we don't close file until we're done freeing locks! */
5808 p->ctx = get_nfs_open_context(ctx);
5809 memcpy(&p->fl, fl, sizeof(p->fl));
5810 p->server = NFS_SERVER(inode);
5811 return p;
5812}
5813
Trond Myklebust06f814a2006-01-03 09:55:07 +01005814static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005815{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005816 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005817 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005818 nfs4_put_lock_state(calldata->lsp);
5819 put_nfs_open_context(calldata->ctx);
5820 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005821}
5822
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005823static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005824{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005825 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005826
Trond Myklebust14516c32010-07-31 14:29:06 -04005827 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5828 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005829 switch (task->tk_status) {
5830 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005831 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005832 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005833 if (nfs4_update_lock_stateid(calldata->lsp,
5834 &calldata->res.stateid))
5835 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005836 case -NFS4ERR_ADMIN_REVOKED:
5837 case -NFS4ERR_EXPIRED:
5838 nfs4_free_revoked_stateid(calldata->server,
5839 &calldata->arg.stateid,
5840 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005841 case -NFS4ERR_BAD_STATEID:
5842 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005843 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005844 if (!nfs4_stateid_match(&calldata->arg.stateid,
5845 &calldata->lsp->ls_stateid))
5846 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005847 break;
5848 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005849 if (nfs4_async_handle_error(task, calldata->server,
5850 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005851 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005852 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005853 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005854}
5855
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005856static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005857{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005858 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005860 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005861 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005862 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005863 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005864 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005865 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005866 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005867 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005868 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04005869 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005870 &calldata->res.seq_res,
5871 task) != 0)
5872 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005873 return;
5874out_no_action:
5875 task->tk_action = NULL;
5876out_wait:
5877 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878}
5879
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005880static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005881 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005882 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005883 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005884};
5885
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005886static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5887 struct nfs_open_context *ctx,
5888 struct nfs4_lock_state *lsp,
5889 struct nfs_seqid *seqid)
5890{
5891 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005892 struct rpc_message msg = {
5893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5894 .rpc_cred = ctx->cred,
5895 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005896 struct rpc_task_setup task_setup_data = {
5897 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005898 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005899 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005900 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005901 .flags = RPC_TASK_ASYNC,
5902 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005903
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005904 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5905 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5906
Frank Filz137d6ac2007-07-09 15:32:29 -07005907 /* Ensure this is an unlock - when canceling a lock, the
5908 * canceled lock is passed in, and it won't be an unlock.
5909 */
5910 fl->fl_type = F_UNLCK;
5911
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005912 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5913 if (data == NULL) {
5914 nfs_free_seqid(seqid);
5915 return ERR_PTR(-ENOMEM);
5916 }
5917
Chuck Levera9c92d62013-08-09 12:48:18 -04005918 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005919 msg.rpc_argp = &data->arg;
5920 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005921 task_setup_data.callback_data = data;
5922 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005923}
5924
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5926{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005927 struct inode *inode = state->inode;
5928 struct nfs4_state_owner *sp = state->owner;
5929 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005930 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005931 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005932 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005933 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005934 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005935 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936
Trond Myklebust9b073572006-06-29 16:38:34 -04005937 status = nfs4_set_lock_state(state, request);
5938 /* Unlock _before_ we do the RPC call */
5939 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005940 /* Exclude nfs_delegation_claim_locks() */
5941 mutex_lock(&sp->so_delegreturn_mutex);
5942 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005943 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005944 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005945 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005946 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005947 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005948 }
5949 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005950 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005951 if (status != 0)
5952 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005953 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005954 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005955 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5956 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005957 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5958 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005959 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005960 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005961 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005962 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005963 status = PTR_ERR(task);
5964 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005965 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005966 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005967 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005968out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005969 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005970 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005971 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972}
5973
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005974struct nfs4_lockdata {
5975 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005976 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005977 struct nfs4_lock_state *lsp;
5978 struct nfs_open_context *ctx;
5979 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005980 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005981 int rpc_status;
5982 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005983 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005984};
5985
5986static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005987 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5988 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005989{
5990 struct nfs4_lockdata *p;
5991 struct inode *inode = lsp->ls_state->inode;
5992 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005993 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005994
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005995 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005996 if (p == NULL)
5997 return NULL;
5998
5999 p->arg.fh = NFS_FH(inode);
6000 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006001 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006002 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006003 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006004 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6005 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006006 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006007 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006008 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006009 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006010 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006011 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006012 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006013 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006014 atomic_inc(&lsp->ls_count);
6015 p->ctx = get_nfs_open_context(ctx);
6016 memcpy(&p->fl, fl, sizeof(p->fl));
6017 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006018out_free_seqid:
6019 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006020out_free:
6021 kfree(p);
6022 return NULL;
6023}
6024
6025static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6026{
6027 struct nfs4_lockdata *data = calldata;
6028 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029
Harvey Harrison3110ff82008-05-02 13:42:44 -07006030 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006031 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006032 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006033 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006034 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006035 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006036 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006037 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006038 nfs4_stateid_copy(&data->arg.open_stateid,
6039 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006040 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006041 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006042 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006043 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006044 nfs4_stateid_copy(&data->arg.lock_stateid,
6045 &data->lsp->ls_stateid);
6046 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006047 if (!nfs4_valid_open_stateid(state)) {
6048 data->rpc_status = -EBADF;
6049 task->tk_action = NULL;
6050 goto out_release_open_seqid;
6051 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006052 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006053 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168a2010-06-16 09:52:26 -04006054 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006055 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006056 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006057 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006058out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006059 nfs_release_seqid(data->arg.open_seqid);
6060out_release_lock_seqid:
6061 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006062out_wait:
6063 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006064 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006065}
6066
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006067static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6068{
6069 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006070 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006071
Harvey Harrison3110ff82008-05-02 13:42:44 -07006072 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006073
Trond Myklebust14516c32010-07-31 14:29:06 -04006074 if (!nfs4_sequence_done(task, &data->res.seq_res))
6075 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006076
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006077 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006078 switch (task->tk_status) {
6079 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006080 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006081 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006082 if (data->arg.new_lock) {
6083 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006084 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006085 rpc_restart_call_prepare(task);
6086 break;
6087 }
6088 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006089 if (data->arg.new_lock_owner != 0) {
6090 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6091 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6092 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6093 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6094 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006095 break;
6096 case -NFS4ERR_BAD_STATEID:
6097 case -NFS4ERR_OLD_STATEID:
6098 case -NFS4ERR_STALE_STATEID:
6099 case -NFS4ERR_EXPIRED:
6100 if (data->arg.new_lock_owner != 0) {
6101 if (!nfs4_stateid_match(&data->arg.open_stateid,
6102 &lsp->ls_state->open_stateid))
6103 rpc_restart_call_prepare(task);
6104 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6105 &lsp->ls_stateid))
6106 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006107 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006108 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006109}
6110
6111static void nfs4_lock_release(void *calldata)
6112{
6113 struct nfs4_lockdata *data = calldata;
6114
Harvey Harrison3110ff82008-05-02 13:42:44 -07006115 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006116 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006117 if (data->cancelled != 0) {
6118 struct rpc_task *task;
6119 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6120 data->arg.lock_seqid);
6121 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006122 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006123 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006124 } else
6125 nfs_free_seqid(data->arg.lock_seqid);
6126 nfs4_put_lock_state(data->lsp);
6127 put_nfs_open_context(data->ctx);
6128 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006129 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006130}
6131
6132static const struct rpc_call_ops nfs4_lock_ops = {
6133 .rpc_call_prepare = nfs4_lock_prepare,
6134 .rpc_call_done = nfs4_lock_done,
6135 .rpc_release = nfs4_lock_release,
6136};
6137
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006138static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6139{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006140 switch (error) {
6141 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006142 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006143 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006144 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006145 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006146 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006147 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006148 break;
6149 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006150 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006151 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006152 };
6153}
6154
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006155static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006156{
6157 struct nfs4_lockdata *data;
6158 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006159 struct rpc_message msg = {
6160 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6161 .rpc_cred = state->owner->so_cred,
6162 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006163 struct rpc_task_setup task_setup_data = {
6164 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006165 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006166 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006167 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006168 .flags = RPC_TASK_ASYNC,
6169 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006170 int ret;
6171
Harvey Harrison3110ff82008-05-02 13:42:44 -07006172 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006173 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006174 fl->fl_u.nfs4_fl.owner,
6175 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006176 if (data == NULL)
6177 return -ENOMEM;
6178 if (IS_SETLKW(cmd))
6179 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006180 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006181 msg.rpc_argp = &data->arg;
6182 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006183 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006184 if (recovery_type > NFS_LOCK_NEW) {
6185 if (recovery_type == NFS_LOCK_RECLAIM)
6186 data->arg.reclaim = NFS_LOCK_RECLAIM;
6187 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006188 } else
6189 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006190 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006191 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006192 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006193 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006194 if (ret == 0) {
6195 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006196 if (ret)
6197 nfs4_handle_setlk_error(data->server, data->lsp,
6198 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006199 } else
6200 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006201 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006202 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006203 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006204 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205}
6206
6207static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6208{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006209 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006210 struct nfs4_exception exception = {
6211 .inode = state->inode,
6212 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006213 int err;
6214
6215 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006216 /* Cache the lock if possible... */
6217 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6218 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006219 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006220 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006221 break;
6222 nfs4_handle_exception(server, err, &exception);
6223 } while (exception.retry);
6224 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225}
6226
6227static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6228{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006229 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006230 struct nfs4_exception exception = {
6231 .inode = state->inode,
6232 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006233 int err;
6234
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006235 err = nfs4_set_lock_state(state, request);
6236 if (err != 0)
6237 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006238 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006239 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6240 return 0;
6241 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006242 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006243 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6244 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006245 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006246 switch (err) {
6247 default:
6248 goto out;
6249 case -NFS4ERR_GRACE:
6250 case -NFS4ERR_DELAY:
6251 nfs4_handle_exception(server, err, &exception);
6252 err = 0;
6253 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006254 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006255out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006256 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257}
6258
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006259#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006260static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6261{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006262 struct nfs4_lock_state *lsp;
6263 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006264
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006265 status = nfs4_set_lock_state(state, request);
6266 if (status != 0)
6267 return status;
6268 lsp = request->fl_u.nfs4_fl.owner;
6269 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6270 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6271 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006272 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006273}
6274#endif
6275
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6277{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006278 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006279 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006280 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006281 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282
Trond Myklebust01c3b862006-06-29 16:38:39 -04006283 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006284 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006285 if (status < 0)
6286 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006287 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006288 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006289 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006290 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006291 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006292 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006293 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006294 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006295 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006296 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006297 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006298 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006299 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006300 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006301out:
6302 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303 return status;
6304}
6305
6306static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6307{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006308 struct nfs4_exception exception = {
6309 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006310 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006311 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312 int err;
6313
6314 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006315 err = _nfs4_proc_setlk(state, cmd, request);
6316 if (err == -NFS4ERR_DENIED)
6317 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006319 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320 } while (exception.retry);
6321 return err;
6322}
6323
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006324#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6325#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6326
6327static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006328nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6329 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006330{
6331 int status = -ERESTARTSYS;
6332 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6333
6334 while(!signalled()) {
6335 status = nfs4_proc_setlk(state, cmd, request);
6336 if ((status != -EAGAIN) || IS_SETLK(cmd))
6337 break;
6338 freezable_schedule_timeout_interruptible(timeout);
6339 timeout *= 2;
6340 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6341 status = -ERESTARTSYS;
6342 }
6343 return status;
6344}
6345
Jeff Laytona1d617d82016-09-17 18:17:39 -04006346#ifdef CONFIG_NFS_V4_1
6347struct nfs4_lock_waiter {
6348 struct task_struct *task;
6349 struct inode *inode;
6350 struct nfs_lowner *owner;
6351 bool notified;
6352};
6353
6354static int
6355nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6356{
6357 int ret;
6358 struct cb_notify_lock_args *cbnl = key;
6359 struct nfs4_lock_waiter *waiter = wait->private;
6360 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6361 *wowner = waiter->owner;
6362
6363 /* Only wake if the callback was for the same owner */
6364 if (lowner->clientid != wowner->clientid ||
6365 lowner->id != wowner->id ||
6366 lowner->s_dev != wowner->s_dev)
6367 return 0;
6368
6369 /* Make sure it's for the right inode */
6370 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6371 return 0;
6372
6373 waiter->notified = true;
6374
6375 /* override "private" so we can use default_wake_function */
6376 wait->private = waiter->task;
6377 ret = autoremove_wake_function(wait, mode, flags, key);
6378 wait->private = waiter;
6379 return ret;
6380}
6381
6382static int
6383nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6384{
6385 int status = -ERESTARTSYS;
6386 unsigned long flags;
6387 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6388 struct nfs_server *server = NFS_SERVER(state->inode);
6389 struct nfs_client *clp = server->nfs_client;
6390 wait_queue_head_t *q = &clp->cl_lock_waitq;
6391 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6392 .id = lsp->ls_seqid.owner_id,
6393 .s_dev = server->s_dev };
6394 struct nfs4_lock_waiter waiter = { .task = current,
6395 .inode = state->inode,
6396 .owner = &owner,
6397 .notified = false };
6398 wait_queue_t wait;
6399
6400 /* Don't bother with waitqueue if we don't expect a callback */
6401 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6402 return nfs4_retry_setlk_simple(state, cmd, request);
6403
6404 init_wait(&wait);
6405 wait.private = &waiter;
6406 wait.func = nfs4_wake_lock_waiter;
6407 add_wait_queue(q, &wait);
6408
6409 while(!signalled()) {
6410 status = nfs4_proc_setlk(state, cmd, request);
6411 if ((status != -EAGAIN) || IS_SETLK(cmd))
6412 break;
6413
6414 status = -ERESTARTSYS;
6415 spin_lock_irqsave(&q->lock, flags);
6416 if (waiter.notified) {
6417 spin_unlock_irqrestore(&q->lock, flags);
6418 continue;
6419 }
6420 set_current_state(TASK_INTERRUPTIBLE);
6421 spin_unlock_irqrestore(&q->lock, flags);
6422
6423 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6424 }
6425
6426 finish_wait(q, &wait);
6427 return status;
6428}
6429#else /* !CONFIG_NFS_V4_1 */
6430static inline int
6431nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6432{
6433 return nfs4_retry_setlk_simple(state, cmd, request);
6434}
6435#endif
6436
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437static int
6438nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6439{
6440 struct nfs_open_context *ctx;
6441 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442 int status;
6443
6444 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006445 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446 state = ctx->state;
6447
6448 if (request->fl_start < 0 || request->fl_end < 0)
6449 return -EINVAL;
6450
Trond Myklebustd9531262009-07-21 19:22:38 -04006451 if (IS_GETLK(cmd)) {
6452 if (state != NULL)
6453 return nfs4_proc_getlk(state, F_GETLK, request);
6454 return 0;
6455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456
6457 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6458 return -EINVAL;
6459
Trond Myklebustd9531262009-07-21 19:22:38 -04006460 if (request->fl_type == F_UNLCK) {
6461 if (state != NULL)
6462 return nfs4_proc_unlck(state, cmd, request);
6463 return 0;
6464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465
Trond Myklebustd9531262009-07-21 19:22:38 -04006466 if (state == NULL)
6467 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006468
6469 if ((request->fl_flags & FL_POSIX) &&
6470 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6471 return -ENOLCK;
6472
Trond Myklebust55725512012-04-18 12:48:35 -04006473 /*
6474 * Don't rely on the VFS having checked the file open mode,
6475 * since it won't do this for flock() locks.
6476 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006477 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006478 case F_RDLCK:
6479 if (!(filp->f_mode & FMODE_READ))
6480 return -EBADF;
6481 break;
6482 case F_WRLCK:
6483 if (!(filp->f_mode & FMODE_WRITE))
6484 return -EBADF;
6485 }
6486
Jeff Layton1ea67db2016-09-17 18:17:37 -04006487 status = nfs4_set_lock_state(state, request);
6488 if (status != 0)
6489 return status;
6490
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006491 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492}
6493
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006494int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006495{
6496 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006497 int err;
6498
6499 err = nfs4_set_lock_state(state, fl);
6500 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006501 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006502 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006503 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006504}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006505
Trond Myklebustcf470c32012-03-07 13:49:12 -05006506struct nfs_release_lockowner_data {
6507 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006508 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006509 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006510 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006511 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006512};
6513
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006514static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6515{
6516 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006517 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006518 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6519 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006520 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006521 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006522}
6523
6524static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6525{
6526 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006527 struct nfs_server *server = data->server;
6528
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006529 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006530
6531 switch (task->tk_status) {
6532 case 0:
6533 renew_lease(server, data->timestamp);
6534 break;
6535 case -NFS4ERR_STALE_CLIENTID:
6536 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006537 nfs4_schedule_lease_recovery(server->nfs_client);
6538 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006539 case -NFS4ERR_LEASE_MOVED:
6540 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006541 if (nfs4_async_handle_error(task, server,
6542 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006543 rpc_restart_call_prepare(task);
6544 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006545}
6546
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006547static void nfs4_release_lockowner_release(void *calldata)
6548{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006549 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006550 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006551 kfree(calldata);
6552}
6553
Trond Myklebust17280172012-03-11 13:11:00 -04006554static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006555 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6556 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006557 .rpc_release = nfs4_release_lockowner_release,
6558};
6559
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006560static void
6561nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006562{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006563 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006564 struct rpc_message msg = {
6565 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6566 };
6567
6568 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006569 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006570
Trond Myklebustcf470c32012-03-07 13:49:12 -05006571 data = kmalloc(sizeof(*data), GFP_NOFS);
6572 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006573 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006574 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006575 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006576 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6577 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6578 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006579
Trond Myklebustcf470c32012-03-07 13:49:12 -05006580 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006581 msg.rpc_resp = &data->res;
6582 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006583 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006584}
6585
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006586#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6587
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006588static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006589 struct dentry *unused, struct inode *inode,
6590 const char *key, const void *buf,
6591 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006592{
Al Viro59301222016-05-27 10:19:30 -04006593 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006594}
6595
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006596static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006597 struct dentry *unused, struct inode *inode,
6598 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006599{
Al Virob2968212016-04-10 20:48:24 -04006600 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006601}
6602
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006603static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006604{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006605 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006606}
6607
David Quigleyc9bccef2013-05-22 12:50:45 -04006608#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006609
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006610static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006611 struct dentry *unused, struct inode *inode,
6612 const char *key, const void *buf,
6613 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006614{
6615 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006616 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006617
6618 return -EOPNOTSUPP;
6619}
6620
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006621static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006622 struct dentry *unused, struct inode *inode,
6623 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006624{
6625 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006626 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006627 return -EOPNOTSUPP;
6628}
6629
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006630static ssize_t
6631nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006632{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006633 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006634
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006635 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6636 len = security_inode_listsecurity(inode, list, list_len);
6637 if (list_len && len > list_len)
6638 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006639 }
6640 return len;
6641}
6642
6643static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6644 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006645 .get = nfs4_xattr_get_nfs4_label,
6646 .set = nfs4_xattr_set_nfs4_label,
6647};
David Quigleyc9bccef2013-05-22 12:50:45 -04006648
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006649#else
6650
6651static ssize_t
6652nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6653{
6654 return 0;
6655}
6656
6657#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006658
Andy Adamson533eb462011-06-13 18:25:56 -04006659/*
6660 * nfs_fhget will use either the mounted_on_fileid or the fileid
6661 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006662static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6663{
Andy Adamson533eb462011-06-13 18:25:56 -04006664 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6665 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6666 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006667 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006668 return;
6669
6670 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006671 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006672 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6673 fattr->nlink = 2;
6674}
6675
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006676static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6677 const struct qstr *name,
6678 struct nfs4_fs_locations *fs_locations,
6679 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006680{
6681 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006682 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006683 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006684 };
6685 struct nfs4_fs_locations_arg args = {
6686 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006687 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006688 .page = page,
6689 .bitmask = bitmask,
6690 };
Benny Halevy22958462009-04-01 09:22:02 -04006691 struct nfs4_fs_locations_res res = {
6692 .fs_locations = fs_locations,
6693 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006694 struct rpc_message msg = {
6695 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6696 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006697 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006698 };
6699 int status;
6700
Harvey Harrison3110ff82008-05-02 13:42:44 -07006701 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006702
6703 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6704 * is not supported */
6705 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6706 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6707 else
6708 bitmask[0] |= FATTR4_WORD0_FILEID;
6709
Trond Myklebustc228fd32007-01-13 02:28:11 -05006710 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006711 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006712 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006713 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006714 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006715 return status;
6716}
6717
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006718int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6719 const struct qstr *name,
6720 struct nfs4_fs_locations *fs_locations,
6721 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006722{
6723 struct nfs4_exception exception = { };
6724 int err;
6725 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006726 err = _nfs4_proc_fs_locations(client, dir, name,
6727 fs_locations, page);
6728 trace_nfs4_get_fs_locations(dir, name, err);
6729 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006730 &exception);
6731 } while (exception.retry);
6732 return err;
6733}
6734
Chuck Leverb03d7352013-10-17 14:12:50 -04006735/*
6736 * This operation also signals the server that this client is
6737 * performing migration recovery. The server can stop returning
6738 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6739 * appended to this compound to identify the client ID which is
6740 * performing recovery.
6741 */
6742static int _nfs40_proc_get_locations(struct inode *inode,
6743 struct nfs4_fs_locations *locations,
6744 struct page *page, struct rpc_cred *cred)
6745{
6746 struct nfs_server *server = NFS_SERVER(inode);
6747 struct rpc_clnt *clnt = server->client;
6748 u32 bitmask[2] = {
6749 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6750 };
6751 struct nfs4_fs_locations_arg args = {
6752 .clientid = server->nfs_client->cl_clientid,
6753 .fh = NFS_FH(inode),
6754 .page = page,
6755 .bitmask = bitmask,
6756 .migration = 1, /* skip LOOKUP */
6757 .renew = 1, /* append RENEW */
6758 };
6759 struct nfs4_fs_locations_res res = {
6760 .fs_locations = locations,
6761 .migration = 1,
6762 .renew = 1,
6763 };
6764 struct rpc_message msg = {
6765 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6766 .rpc_argp = &args,
6767 .rpc_resp = &res,
6768 .rpc_cred = cred,
6769 };
6770 unsigned long now = jiffies;
6771 int status;
6772
6773 nfs_fattr_init(&locations->fattr);
6774 locations->server = server;
6775 locations->nlocations = 0;
6776
6777 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6778 nfs4_set_sequence_privileged(&args.seq_args);
6779 status = nfs4_call_sync_sequence(clnt, server, &msg,
6780 &args.seq_args, &res.seq_res);
6781 if (status)
6782 return status;
6783
6784 renew_lease(server, now);
6785 return 0;
6786}
6787
6788#ifdef CONFIG_NFS_V4_1
6789
6790/*
6791 * This operation also signals the server that this client is
6792 * performing migration recovery. The server can stop asserting
6793 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6794 * performing this operation is identified in the SEQUENCE
6795 * operation in this compound.
6796 *
6797 * When the client supports GETATTR(fs_locations_info), it can
6798 * be plumbed in here.
6799 */
6800static int _nfs41_proc_get_locations(struct inode *inode,
6801 struct nfs4_fs_locations *locations,
6802 struct page *page, struct rpc_cred *cred)
6803{
6804 struct nfs_server *server = NFS_SERVER(inode);
6805 struct rpc_clnt *clnt = server->client;
6806 u32 bitmask[2] = {
6807 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6808 };
6809 struct nfs4_fs_locations_arg args = {
6810 .fh = NFS_FH(inode),
6811 .page = page,
6812 .bitmask = bitmask,
6813 .migration = 1, /* skip LOOKUP */
6814 };
6815 struct nfs4_fs_locations_res res = {
6816 .fs_locations = locations,
6817 .migration = 1,
6818 };
6819 struct rpc_message msg = {
6820 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6821 .rpc_argp = &args,
6822 .rpc_resp = &res,
6823 .rpc_cred = cred,
6824 };
6825 int status;
6826
6827 nfs_fattr_init(&locations->fattr);
6828 locations->server = server;
6829 locations->nlocations = 0;
6830
6831 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6832 nfs4_set_sequence_privileged(&args.seq_args);
6833 status = nfs4_call_sync_sequence(clnt, server, &msg,
6834 &args.seq_args, &res.seq_res);
6835 if (status == NFS4_OK &&
6836 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6837 status = -NFS4ERR_LEASE_MOVED;
6838 return status;
6839}
6840
6841#endif /* CONFIG_NFS_V4_1 */
6842
6843/**
6844 * nfs4_proc_get_locations - discover locations for a migrated FSID
6845 * @inode: inode on FSID that is migrating
6846 * @locations: result of query
6847 * @page: buffer
6848 * @cred: credential to use for this operation
6849 *
6850 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6851 * operation failed, or a negative errno if a local error occurred.
6852 *
6853 * On success, "locations" is filled in, but if the server has
6854 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6855 * asserted.
6856 *
6857 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6858 * from this client that require migration recovery.
6859 */
6860int nfs4_proc_get_locations(struct inode *inode,
6861 struct nfs4_fs_locations *locations,
6862 struct page *page, struct rpc_cred *cred)
6863{
6864 struct nfs_server *server = NFS_SERVER(inode);
6865 struct nfs_client *clp = server->nfs_client;
6866 const struct nfs4_mig_recovery_ops *ops =
6867 clp->cl_mvops->mig_recovery_ops;
6868 struct nfs4_exception exception = { };
6869 int status;
6870
6871 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6872 (unsigned long long)server->fsid.major,
6873 (unsigned long long)server->fsid.minor,
6874 clp->cl_hostname);
6875 nfs_display_fhandle(NFS_FH(inode), __func__);
6876
6877 do {
6878 status = ops->get_locations(inode, locations, page, cred);
6879 if (status != -NFS4ERR_DELAY)
6880 break;
6881 nfs4_handle_exception(server, status, &exception);
6882 } while (exception.retry);
6883 return status;
6884}
6885
Chuck Lever44c99932013-10-17 14:13:30 -04006886/*
6887 * This operation also signals the server that this client is
6888 * performing "lease moved" recovery. The server can stop
6889 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6890 * is appended to this compound to identify the client ID which is
6891 * performing recovery.
6892 */
6893static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6894{
6895 struct nfs_server *server = NFS_SERVER(inode);
6896 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6897 struct rpc_clnt *clnt = server->client;
6898 struct nfs4_fsid_present_arg args = {
6899 .fh = NFS_FH(inode),
6900 .clientid = clp->cl_clientid,
6901 .renew = 1, /* append RENEW */
6902 };
6903 struct nfs4_fsid_present_res res = {
6904 .renew = 1,
6905 };
6906 struct rpc_message msg = {
6907 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6908 .rpc_argp = &args,
6909 .rpc_resp = &res,
6910 .rpc_cred = cred,
6911 };
6912 unsigned long now = jiffies;
6913 int status;
6914
6915 res.fh = nfs_alloc_fhandle();
6916 if (res.fh == NULL)
6917 return -ENOMEM;
6918
6919 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6920 nfs4_set_sequence_privileged(&args.seq_args);
6921 status = nfs4_call_sync_sequence(clnt, server, &msg,
6922 &args.seq_args, &res.seq_res);
6923 nfs_free_fhandle(res.fh);
6924 if (status)
6925 return status;
6926
6927 do_renew_lease(clp, now);
6928 return 0;
6929}
6930
6931#ifdef CONFIG_NFS_V4_1
6932
6933/*
6934 * This operation also signals the server that this client is
6935 * performing "lease moved" recovery. The server can stop asserting
6936 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6937 * this operation is identified in the SEQUENCE operation in this
6938 * compound.
6939 */
6940static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6941{
6942 struct nfs_server *server = NFS_SERVER(inode);
6943 struct rpc_clnt *clnt = server->client;
6944 struct nfs4_fsid_present_arg args = {
6945 .fh = NFS_FH(inode),
6946 };
6947 struct nfs4_fsid_present_res res = {
6948 };
6949 struct rpc_message msg = {
6950 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6951 .rpc_argp = &args,
6952 .rpc_resp = &res,
6953 .rpc_cred = cred,
6954 };
6955 int status;
6956
6957 res.fh = nfs_alloc_fhandle();
6958 if (res.fh == NULL)
6959 return -ENOMEM;
6960
6961 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6962 nfs4_set_sequence_privileged(&args.seq_args);
6963 status = nfs4_call_sync_sequence(clnt, server, &msg,
6964 &args.seq_args, &res.seq_res);
6965 nfs_free_fhandle(res.fh);
6966 if (status == NFS4_OK &&
6967 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6968 status = -NFS4ERR_LEASE_MOVED;
6969 return status;
6970}
6971
6972#endif /* CONFIG_NFS_V4_1 */
6973
6974/**
6975 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6976 * @inode: inode on FSID to check
6977 * @cred: credential to use for this operation
6978 *
6979 * Server indicates whether the FSID is present, moved, or not
6980 * recognized. This operation is necessary to clear a LEASE_MOVED
6981 * condition for this client ID.
6982 *
6983 * Returns NFS4_OK if the FSID is present on this server,
6984 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6985 * NFS4ERR code if some error occurred on the server, or a
6986 * negative errno if a local failure occurred.
6987 */
6988int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6989{
6990 struct nfs_server *server = NFS_SERVER(inode);
6991 struct nfs_client *clp = server->nfs_client;
6992 const struct nfs4_mig_recovery_ops *ops =
6993 clp->cl_mvops->mig_recovery_ops;
6994 struct nfs4_exception exception = { };
6995 int status;
6996
6997 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6998 (unsigned long long)server->fsid.major,
6999 (unsigned long long)server->fsid.minor,
7000 clp->cl_hostname);
7001 nfs_display_fhandle(NFS_FH(inode), __func__);
7002
7003 do {
7004 status = ops->fsid_present(inode, cred);
7005 if (status != -NFS4ERR_DELAY)
7006 break;
7007 nfs4_handle_exception(server, status, &exception);
7008 } while (exception.retry);
7009 return status;
7010}
7011
Andy Adamson5ec16a82013-08-08 10:57:55 -04007012/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007013 * If 'use_integrity' is true and the state managment nfs_client
7014 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7015 * and the machine credential as per RFC3530bis and RFC5661 Security
7016 * Considerations sections. Otherwise, just use the user cred with the
7017 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007018 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007019static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007020{
7021 int status;
7022 struct nfs4_secinfo_arg args = {
7023 .dir_fh = NFS_FH(dir),
7024 .name = name,
7025 };
7026 struct nfs4_secinfo_res res = {
7027 .flavors = flavors,
7028 };
7029 struct rpc_message msg = {
7030 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7031 .rpc_argp = &args,
7032 .rpc_resp = &res,
7033 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007034 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007035 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007036
7037 if (use_integrity) {
7038 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007039 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7040 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007041 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007042
7043 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007044
7045 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7046 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7047
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007048 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7049 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007050 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007051
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007052 if (cred)
7053 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007054
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007055 return status;
7056}
7057
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007058int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7059 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007060{
7061 struct nfs4_exception exception = { };
7062 int err;
7063 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007064 err = -NFS4ERR_WRONGSEC;
7065
7066 /* try to use integrity protection with machine cred */
7067 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7068 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7069
7070 /*
7071 * if unable to use integrity protection, or SECINFO with
7072 * integrity protection returns NFS4ERR_WRONGSEC (which is
7073 * disallowed by spec, but exists in deployed servers) use
7074 * the current filesystem's rpc_client and the user cred.
7075 */
7076 if (err == -NFS4ERR_WRONGSEC)
7077 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7078
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007079 trace_nfs4_secinfo(dir, name, err);
7080 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007081 &exception);
7082 } while (exception.retry);
7083 return err;
7084}
7085
Andy Adamson557134a2009-04-01 09:21:53 -04007086#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007087/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007088 * Check the exchange flags returned by the server for invalid flags, having
7089 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7090 * DS flags set.
7091 */
7092static int nfs4_check_cl_exchange_flags(u32 flags)
7093{
7094 if (flags & ~EXCHGID4_FLAG_MASK_R)
7095 goto out_inval;
7096 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7097 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7098 goto out_inval;
7099 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7100 goto out_inval;
7101 return NFS_OK;
7102out_inval:
7103 return -NFS4ERR_INVAL;
7104}
7105
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007106static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007107nfs41_same_server_scope(struct nfs41_server_scope *a,
7108 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007109{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007110 if (a->server_scope_sz != b->server_scope_sz)
7111 return false;
7112 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007113}
7114
Andy Adamson02a95de2016-02-05 16:08:37 -05007115static void
7116nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7117{
7118}
7119
7120static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7121 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7122};
7123
Andy Adamson357f54d2010-12-14 10:11:57 -05007124/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007125 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007126 *
7127 * The 4.1 client currently uses the same TCP connection for the
7128 * fore and backchannel.
7129 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007130static
7131int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7132 struct rpc_xprt *xprt,
7133 struct nfs_client *clp,
7134 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007135{
7136 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007137 struct nfs41_bind_conn_to_session_args args = {
7138 .client = clp,
7139 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7140 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007141 struct nfs41_bind_conn_to_session_res res;
7142 struct rpc_message msg = {
7143 .rpc_proc =
7144 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007145 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007146 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007147 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007148 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007149 struct rpc_task_setup task_setup_data = {
7150 .rpc_client = clnt,
7151 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007152 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007153 .rpc_message = &msg,
7154 .flags = RPC_TASK_TIMEOUT,
7155 };
7156 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007157
Trond Myklebust71a097c2015-02-18 09:27:18 -08007158 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7159 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7160 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007161
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007162 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7163 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7164 args.dir = NFS4_CDFC4_FORE;
7165
7166 task = rpc_run_task(&task_setup_data);
7167 if (!IS_ERR(task)) {
7168 status = task->tk_status;
7169 rpc_put_task(task);
7170 } else
7171 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007172 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007173 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007174 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007175 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7176 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007177 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007178 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007179 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007180 dprintk("NFS: %s: Unexpected direction from server\n",
7181 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007182 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007183 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007184 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007185 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7186 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007187 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007188 }
7189 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007190
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007191 return status;
7192}
7193
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007194struct rpc_bind_conn_calldata {
7195 struct nfs_client *clp;
7196 struct rpc_cred *cred;
7197};
7198
7199static int
7200nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7201 struct rpc_xprt *xprt,
7202 void *calldata)
7203{
7204 struct rpc_bind_conn_calldata *p = calldata;
7205
7206 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7207}
7208
7209int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7210{
7211 struct rpc_bind_conn_calldata data = {
7212 .clp = clp,
7213 .cred = cred,
7214 };
7215 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7216 nfs4_proc_bind_conn_to_session_callback, &data);
7217}
7218
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007219/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007220 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7221 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007222 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007223static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7224 .how = SP4_MACH_CRED,
7225 .enforce.u.words = {
7226 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7227 1 << (OP_EXCHANGE_ID - 32) |
7228 1 << (OP_CREATE_SESSION - 32) |
7229 1 << (OP_DESTROY_SESSION - 32) |
7230 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007231 },
7232 .allow.u.words = {
7233 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007234 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007235 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007236 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007237 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007238 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007239 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007240 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007241 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007242 1 << (OP_FREE_STATEID - 32) |
7243 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007244 }
7245};
7246
7247/*
7248 * Select the state protection mode for client `clp' given the server results
7249 * from exchange_id in `sp'.
7250 *
7251 * Returns 0 on success, negative errno otherwise.
7252 */
7253static int nfs4_sp4_select_mode(struct nfs_client *clp,
7254 struct nfs41_state_protection *sp)
7255{
7256 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7257 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7258 1 << (OP_EXCHANGE_ID - 32) |
7259 1 << (OP_CREATE_SESSION - 32) |
7260 1 << (OP_DESTROY_SESSION - 32) |
7261 1 << (OP_DESTROY_CLIENTID - 32)
7262 };
7263 unsigned int i;
7264
7265 if (sp->how == SP4_MACH_CRED) {
7266 /* Print state protect result */
7267 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7268 for (i = 0; i <= LAST_NFS4_OP; i++) {
7269 if (test_bit(i, sp->enforce.u.longs))
7270 dfprintk(MOUNT, " enforce op %d\n", i);
7271 if (test_bit(i, sp->allow.u.longs))
7272 dfprintk(MOUNT, " allow op %d\n", i);
7273 }
7274
7275 /* make sure nothing is on enforce list that isn't supported */
7276 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7277 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7278 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7279 return -EINVAL;
7280 }
7281 }
7282
7283 /*
7284 * Minimal mode - state operations are allowed to use machine
7285 * credential. Note this already happens by default, so the
7286 * client doesn't have to do anything more than the negotiation.
7287 *
7288 * NOTE: we don't care if EXCHANGE_ID is in the list -
7289 * we're already using the machine cred for exchange_id
7290 * and will never use a different cred.
7291 */
7292 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7293 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7294 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7295 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7296 dfprintk(MOUNT, "sp4_mach_cred:\n");
7297 dfprintk(MOUNT, " minimal mode enabled\n");
7298 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7299 } else {
7300 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7301 return -EINVAL;
7302 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007303
7304 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007305 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7306 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007307 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7308 dfprintk(MOUNT, " cleanup mode enabled\n");
7309 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7310 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007311
Andrew Elble99ade3c2015-12-02 09:39:51 -05007312 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7313 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7314 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7315 &clp->cl_sp4_flags);
7316 }
7317
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007318 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7319 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7320 dfprintk(MOUNT, " secinfo mode enabled\n");
7321 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7322 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007323
7324 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7325 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7326 dfprintk(MOUNT, " stateid mode enabled\n");
7327 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7328 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007329
7330 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7331 dfprintk(MOUNT, " write mode enabled\n");
7332 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7333 }
7334
7335 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7336 dfprintk(MOUNT, " commit mode enabled\n");
7337 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7338 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007339 }
7340
7341 return 0;
7342}
7343
Andy Adamson8d89bd72016-09-09 09:22:18 -04007344struct nfs41_exchange_id_data {
7345 struct nfs41_exchange_id_res res;
7346 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007347 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007348 int rpc_status;
7349};
7350
7351static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007352{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007353 struct nfs41_exchange_id_data *cdata =
7354 (struct nfs41_exchange_id_data *)data;
7355 struct nfs_client *clp = cdata->args.client;
7356 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007357
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007358 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007359
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007360 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007361 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007362
7363 if (cdata->xprt && status == 0) {
7364 status = nfs4_detect_session_trunking(clp, &cdata->res,
7365 cdata->xprt);
7366 goto out;
7367 }
7368
Andy Adamson8d89bd72016-09-09 09:22:18 -04007369 if (status == 0)
7370 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007371
Chuck Lever177313f2012-05-21 22:44:58 -04007372 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007373 clp->cl_clientid = cdata->res.clientid;
7374 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007375 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007376 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007377 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007378 &clp->cl_session->session_state);
7379 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007380 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007381
Chuck Leveracdeb692012-05-21 22:46:16 -04007382 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007383 clp->cl_serverowner = cdata->res.server_owner;
7384 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007385
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007386 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007387 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007388 clp->cl_implid = cdata->res.impl_id;
7389 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007390
Chuck Lever177313f2012-05-21 22:44:58 -04007391 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007392 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007393 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007394 dprintk("%s: server_scope mismatch detected\n",
7395 __func__);
7396 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007397 kfree(clp->cl_serverscope);
7398 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007399 }
7400
Chuck Lever177313f2012-05-21 22:44:58 -04007401 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007402 clp->cl_serverscope = cdata->res.server_scope;
7403 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007404 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007405 /* Save the EXCHANGE_ID verifier session trunk tests */
7406 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7407 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007408 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007409out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007410 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007411 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007412}
7413
7414static void nfs4_exchange_id_release(void *data)
7415{
7416 struct nfs41_exchange_id_data *cdata =
7417 (struct nfs41_exchange_id_data *)data;
7418
Andy Adamsonad0849a2016-09-09 09:22:28 -04007419 if (cdata->xprt) {
7420 xprt_put(cdata->xprt);
7421 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7422 }
Olga Kornievskaia63513232017-03-13 10:36:19 -04007423 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007424 kfree(cdata->res.impl_id);
7425 kfree(cdata->res.server_scope);
7426 kfree(cdata->res.server_owner);
7427 kfree(cdata);
7428}
7429
7430static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7431 .rpc_call_done = nfs4_exchange_id_done,
7432 .rpc_release = nfs4_exchange_id_release,
7433};
7434
Benny Halevy99fe60d2009-04-01 09:22:29 -04007435/*
7436 * _nfs4_proc_exchange_id()
7437 *
7438 * Wrapper for EXCHANGE_ID operation.
7439 */
7440static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007441 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007442{
7443 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007444 struct rpc_message msg = {
7445 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007446 .rpc_cred = cred,
7447 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007448 struct rpc_task_setup task_setup_data = {
7449 .rpc_client = clp->cl_rpcclient,
7450 .callback_ops = &nfs4_exchange_id_call_ops,
7451 .rpc_message = &msg,
7452 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7453 };
7454 struct nfs41_exchange_id_data *calldata;
7455 struct rpc_task *task;
7456 int status = -EIO;
7457
7458 if (!atomic_inc_not_zero(&clp->cl_count))
7459 goto out;
7460
7461 status = -ENOMEM;
7462 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7463 if (!calldata)
7464 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007465
Andy Adamsonad0849a2016-09-09 09:22:28 -04007466 if (!xprt)
7467 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007468
7469 status = nfs4_init_uniform_client_string(clp);
7470 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007471 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007472
7473 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7474 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7475 clp->cl_owner_id);
7476
Andy Adamson8d89bd72016-09-09 09:22:18 -04007477 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7478 GFP_NOFS);
7479 status = -ENOMEM;
7480 if (unlikely(calldata->res.server_owner == NULL))
7481 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007482
Andy Adamson8d89bd72016-09-09 09:22:18 -04007483 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007484 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007485 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007486 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007487
Andy Adamson8d89bd72016-09-09 09:22:18 -04007488 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7489 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007490 goto out_server_scope;
7491
7492 switch (sp4_how) {
7493 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007494 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007495 break;
7496
7497 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007498 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007499 break;
7500
7501 default:
7502 /* unsupported! */
7503 WARN_ON_ONCE(1);
7504 status = -EINVAL;
7505 goto out_impl_id;
7506 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007507 if (xprt) {
7508 calldata->xprt = xprt;
7509 task_setup_data.rpc_xprt = xprt;
7510 task_setup_data.flags =
7511 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7512 calldata->args.verifier = &clp->cl_confirm;
7513 } else {
7514 calldata->args.verifier = &verifier;
7515 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007516 calldata->args.client = clp;
7517#ifdef CONFIG_NFS_V4_1_MIGRATION
7518 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7519 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7520 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7521#else
7522 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7523 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7524#endif
7525 msg.rpc_argp = &calldata->args;
7526 msg.rpc_resp = &calldata->res;
7527 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007528
Andy Adamson8d89bd72016-09-09 09:22:18 -04007529 task = rpc_run_task(&task_setup_data);
Olga Kornievskaia63513232017-03-13 10:36:19 -04007530 if (IS_ERR(task))
7531 return PTR_ERR(task);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007532
Andy Adamsonad0849a2016-09-09 09:22:28 -04007533 if (!xprt) {
7534 status = rpc_wait_for_completion_task(task);
7535 if (!status)
7536 status = calldata->rpc_status;
7537 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007538 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007539
Andy Adamson8d89bd72016-09-09 09:22:18 -04007540 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007541out:
Chuck Lever177313f2012-05-21 22:44:58 -04007542 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007543 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007544 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007545 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007546 clp->cl_implid->date.seconds,
7547 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007548 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007549 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007550
7551out_impl_id:
7552 kfree(calldata->res.impl_id);
7553out_server_scope:
7554 kfree(calldata->res.server_scope);
7555out_server_owner:
7556 kfree(calldata->res.server_owner);
7557out_calldata:
7558 kfree(calldata);
Olga Kornievskaia63513232017-03-13 10:36:19 -04007559 nfs_put_client(clp);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007560 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007561}
7562
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007563/*
7564 * nfs4_proc_exchange_id()
7565 *
7566 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7567 *
7568 * Since the clientid has expired, all compounds using sessions
7569 * associated with the stale clientid will be returning
7570 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7571 * be in some phase of session reset.
7572 *
7573 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7574 */
7575int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7576{
7577 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7578 int status;
7579
7580 /* try SP4_MACH_CRED if krb5i/p */
7581 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7582 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007583 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007584 if (!status)
7585 return 0;
7586 }
7587
7588 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007589 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007590}
7591
Andy Adamson04fa2c62016-09-09 09:22:29 -04007592/**
7593 * nfs4_test_session_trunk
7594 *
7595 * This is an add_xprt_test() test function called from
7596 * rpc_clnt_setup_test_and_add_xprt.
7597 *
7598 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7599 * and is dereferrenced in nfs4_exchange_id_release
7600 *
7601 * Upon success, add the new transport to the rpc_clnt
7602 *
7603 * @clnt: struct rpc_clnt to get new transport
7604 * @xprt: the rpc_xprt to test
7605 * @data: call data for _nfs4_proc_exchange_id.
7606 */
7607int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7608 void *data)
7609{
7610 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7611 u32 sp4_how;
7612
7613 dprintk("--> %s try %s\n", __func__,
7614 xprt->address_strings[RPC_DISPLAY_ADDR]);
7615
7616 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7617
7618 /* Test connection for session trunking. Async exchange_id call */
7619 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7620}
7621EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7622
Trond Myklebust66245532012-05-25 17:18:09 -04007623static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7624 struct rpc_cred *cred)
7625{
7626 struct rpc_message msg = {
7627 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7628 .rpc_argp = clp,
7629 .rpc_cred = cred,
7630 };
7631 int status;
7632
7633 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007634 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007635 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007636 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007637 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7638 return status;
7639}
7640
7641static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7642 struct rpc_cred *cred)
7643{
7644 unsigned int loop;
7645 int ret;
7646
7647 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7648 ret = _nfs4_proc_destroy_clientid(clp, cred);
7649 switch (ret) {
7650 case -NFS4ERR_DELAY:
7651 case -NFS4ERR_CLIENTID_BUSY:
7652 ssleep(1);
7653 break;
7654 default:
7655 return ret;
7656 }
7657 }
7658 return 0;
7659}
7660
7661int nfs4_destroy_clientid(struct nfs_client *clp)
7662{
7663 struct rpc_cred *cred;
7664 int ret = 0;
7665
7666 if (clp->cl_mvops->minor_version < 1)
7667 goto out;
7668 if (clp->cl_exchange_flags == 0)
7669 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007670 if (clp->cl_preserve_clid)
7671 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007672 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007673 ret = nfs4_proc_destroy_clientid(clp, cred);
7674 if (cred)
7675 put_rpccred(cred);
7676 switch (ret) {
7677 case 0:
7678 case -NFS4ERR_STALE_CLIENTID:
7679 clp->cl_exchange_flags = 0;
7680 }
7681out:
7682 return ret;
7683}
7684
Andy Adamson2050f0c2009-04-01 09:22:30 -04007685struct nfs4_get_lease_time_data {
7686 struct nfs4_get_lease_time_args *args;
7687 struct nfs4_get_lease_time_res *res;
7688 struct nfs_client *clp;
7689};
7690
7691static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7692 void *calldata)
7693{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007694 struct nfs4_get_lease_time_data *data =
7695 (struct nfs4_get_lease_time_data *)calldata;
7696
7697 dprintk("--> %s\n", __func__);
7698 /* just setup sequence, do not trigger session recovery
7699 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007700 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007701 &data->args->la_seq_args,
7702 &data->res->lr_seq_res,
7703 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007704 dprintk("<-- %s\n", __func__);
7705}
7706
7707/*
7708 * Called from nfs4_state_manager thread for session setup, so don't recover
7709 * from sequence operation or clientid errors.
7710 */
7711static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7712{
7713 struct nfs4_get_lease_time_data *data =
7714 (struct nfs4_get_lease_time_data *)calldata;
7715
7716 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007717 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7718 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007719 switch (task->tk_status) {
7720 case -NFS4ERR_DELAY:
7721 case -NFS4ERR_GRACE:
7722 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7723 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7724 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007725 /* fall through */
7726 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007727 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007728 return;
7729 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007730 dprintk("<-- %s\n", __func__);
7731}
7732
Trond Myklebust17280172012-03-11 13:11:00 -04007733static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007734 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7735 .rpc_call_done = nfs4_get_lease_time_done,
7736};
7737
7738int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7739{
7740 struct rpc_task *task;
7741 struct nfs4_get_lease_time_args args;
7742 struct nfs4_get_lease_time_res res = {
7743 .lr_fsinfo = fsinfo,
7744 };
7745 struct nfs4_get_lease_time_data data = {
7746 .args = &args,
7747 .res = &res,
7748 .clp = clp,
7749 };
7750 struct rpc_message msg = {
7751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7752 .rpc_argp = &args,
7753 .rpc_resp = &res,
7754 };
7755 struct rpc_task_setup task_setup = {
7756 .rpc_client = clp->cl_rpcclient,
7757 .rpc_message = &msg,
7758 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007759 .callback_data = &data,
7760 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007761 };
7762 int status;
7763
Chuck Levera9c92d62013-08-09 12:48:18 -04007764 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007765 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007766 dprintk("--> %s\n", __func__);
7767 task = rpc_run_task(&task_setup);
7768
7769 if (IS_ERR(task))
7770 status = PTR_ERR(task);
7771 else {
7772 status = task->tk_status;
7773 rpc_put_task(task);
7774 }
7775 dprintk("<-- %s return %d\n", __func__, status);
7776
7777 return status;
7778}
7779
Andy Adamsonfc931582009-04-01 09:22:31 -04007780/*
7781 * Initialize the values to be used by the client in CREATE_SESSION
7782 * If nfs4_init_session set the fore channel request and response sizes,
7783 * use them.
7784 *
7785 * Set the back channel max_resp_sz_cached to zero to force the client to
7786 * always set csa_cachethis to FALSE because the current implementation
7787 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7788 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007789static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7790 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007791{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007792 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007793 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007794
Andy Adamson18aad3d2013-06-26 12:21:49 -04007795 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7796 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7797
Andy Adamsonfc931582009-04-01 09:22:31 -04007798 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007799 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7800 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007801 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007802 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007803
7804 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007805 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007806 __func__,
7807 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007808 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007809
7810 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007811 args->bc_attrs.max_rqst_sz = max_bc_payload;
7812 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007813 args->bc_attrs.max_resp_sz_cached = 0;
7814 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007815 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007816
7817 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7818 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7819 __func__,
7820 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7821 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7822 args->bc_attrs.max_reqs);
7823}
7824
Trond Myklebust79969dd2015-02-18 11:30:18 -08007825static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7826 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007827{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007828 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007829 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007830
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007831 if (rcvd->max_resp_sz > sent->max_resp_sz)
7832 return -EINVAL;
7833 /*
7834 * Our requested max_ops is the minimum we need; we're not
7835 * prepared to break up compounds into smaller pieces than that.
7836 * So, no point even trying to continue if the server won't
7837 * cooperate:
7838 */
7839 if (rcvd->max_ops < sent->max_ops)
7840 return -EINVAL;
7841 if (rcvd->max_reqs == 0)
7842 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007843 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7844 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007845 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007846}
7847
Trond Myklebust79969dd2015-02-18 11:30:18 -08007848static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7849 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007850{
7851 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007852 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007853
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007854 if (!(res->flags & SESSION4_BACK_CHAN))
7855 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007856 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7857 return -EINVAL;
7858 if (rcvd->max_resp_sz < sent->max_resp_sz)
7859 return -EINVAL;
7860 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7861 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007862 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007863 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007864 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007865 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007866out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007867 return 0;
7868}
Andy Adamson8d353012009-04-01 09:22:32 -04007869
Andy Adamson8d353012009-04-01 09:22:32 -04007870static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007871 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007872{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007873 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007874
Trond Myklebust79969dd2015-02-18 11:30:18 -08007875 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007876 if (ret)
7877 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007878 return nfs4_verify_back_channel_attrs(args, res);
7879}
7880
7881static void nfs4_update_session(struct nfs4_session *session,
7882 struct nfs41_create_session_res *res)
7883{
7884 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007885 /* Mark client id and session as being confirmed */
7886 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7887 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007888 session->flags = res->flags;
7889 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007890 if (res->flags & SESSION4_BACK_CHAN)
7891 memcpy(&session->bc_attrs, &res->bc_attrs,
7892 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007893}
7894
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007895static int _nfs4_proc_create_session(struct nfs_client *clp,
7896 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007897{
7898 struct nfs4_session *session = clp->cl_session;
7899 struct nfs41_create_session_args args = {
7900 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007901 .clientid = clp->cl_clientid,
7902 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007903 .cb_program = NFS4_CALLBACK,
7904 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007905 struct nfs41_create_session_res res;
7906
Andy Adamsonfc931582009-04-01 09:22:31 -04007907 struct rpc_message msg = {
7908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7909 .rpc_argp = &args,
7910 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007911 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007912 };
7913 int status;
7914
Chuck Lever6b26cc82016-05-02 14:40:40 -04007915 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007916 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007917
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007918 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007919 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007920
Trond Myklebustb519d402016-09-11 14:50:01 -04007921 switch (status) {
7922 case -NFS4ERR_STALE_CLIENTID:
7923 case -NFS4ERR_DELAY:
7924 case -ETIMEDOUT:
7925 case -EACCES:
7926 case -EAGAIN:
7927 goto out;
7928 };
7929
7930 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007931 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007932 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007933 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007934 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007935 if (status)
7936 goto out;
7937 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007938 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007939out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007940 return status;
7941}
7942
7943/*
7944 * Issues a CREATE_SESSION operation to the server.
7945 * It is the responsibility of the caller to verify the session is
7946 * expired before calling this routine.
7947 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007948int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007949{
7950 int status;
7951 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007952 struct nfs4_session *session = clp->cl_session;
7953
7954 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7955
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007956 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007957 if (status)
7958 goto out;
7959
Andy Adamsonaacd5532011-11-09 13:58:21 -05007960 /* Init or reset the session slot tables */
7961 status = nfs4_setup_session_slot_tables(session);
7962 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007963 if (status)
7964 goto out;
7965
7966 ptr = (unsigned *)&session->sess_id.data[0];
7967 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7968 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007969out:
7970 dprintk("<-- %s\n", __func__);
7971 return status;
7972}
7973
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007974/*
7975 * Issue the over-the-wire RPC DESTROY_SESSION.
7976 * The caller must serialize access to this routine.
7977 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007978int nfs4_proc_destroy_session(struct nfs4_session *session,
7979 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007980{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007981 struct rpc_message msg = {
7982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7983 .rpc_argp = session,
7984 .rpc_cred = cred,
7985 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007986 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007987
7988 dprintk("--> nfs4_proc_destroy_session\n");
7989
7990 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007991 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7992 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007993
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007994 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007995 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007996
7997 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007998 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007999 "Session has been destroyed regardless...\n", status);
8000
8001 dprintk("<-- nfs4_proc_destroy_session\n");
8002 return status;
8003}
8004
Trond Myklebust7b38c362012-05-23 13:23:31 -04008005/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008006 * Renew the cl_session lease.
8007 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008008struct nfs4_sequence_data {
8009 struct nfs_client *clp;
8010 struct nfs4_sequence_args args;
8011 struct nfs4_sequence_res res;
8012};
8013
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008014static void nfs41_sequence_release(void *data)
8015{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008016 struct nfs4_sequence_data *calldata = data;
8017 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008018
Alexandros Batsakis71358402010-02-05 03:45:05 -08008019 if (atomic_read(&clp->cl_count) > 1)
8020 nfs4_schedule_state_renewal(clp);
8021 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008022 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008023}
8024
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008025static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8026{
8027 switch(task->tk_status) {
8028 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008029 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8030 return -EAGAIN;
8031 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008032 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008033 }
8034 return 0;
8035}
8036
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008037static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008038{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008039 struct nfs4_sequence_data *calldata = data;
8040 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008041
Trond Myklebust14516c32010-07-31 14:29:06 -04008042 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8043 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008044
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008045 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008046 if (task->tk_status < 0) {
8047 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008048 if (atomic_read(&clp->cl_count) == 1)
8049 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008050
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008051 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8052 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008053 return;
8054 }
8055 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008056 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008057out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008058 dprintk("<-- %s\n", __func__);
8059}
8060
8061static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8062{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008063 struct nfs4_sequence_data *calldata = data;
8064 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008065 struct nfs4_sequence_args *args;
8066 struct nfs4_sequence_res *res;
8067
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008068 args = task->tk_msg.rpc_argp;
8069 res = task->tk_msg.rpc_resp;
8070
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008071 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008072}
8073
8074static const struct rpc_call_ops nfs41_sequence_ops = {
8075 .rpc_call_done = nfs41_sequence_call_done,
8076 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008077 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008078};
8079
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008080static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8081 struct rpc_cred *cred,
8082 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008083{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008084 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008085 struct rpc_message msg = {
8086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8087 .rpc_cred = cred,
8088 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008089 struct rpc_task_setup task_setup_data = {
8090 .rpc_client = clp->cl_rpcclient,
8091 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008092 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008093 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008094 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008095
Alexandros Batsakis71358402010-02-05 03:45:05 -08008096 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008097 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008098 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008099 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008100 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008101 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008102 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008103 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008104 if (is_privileged)
8105 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008106 msg.rpc_argp = &calldata->args;
8107 msg.rpc_resp = &calldata->res;
8108 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008109 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008110
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008111 return rpc_run_task(&task_setup_data);
8112}
8113
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008114static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008115{
8116 struct rpc_task *task;
8117 int ret = 0;
8118
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008119 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008120 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008121 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008122 if (IS_ERR(task))
8123 ret = PTR_ERR(task);
8124 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008125 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008126 dprintk("<-- %s status=%d\n", __func__, ret);
8127 return ret;
8128}
8129
8130static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8131{
8132 struct rpc_task *task;
8133 int ret;
8134
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008135 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008136 if (IS_ERR(task)) {
8137 ret = PTR_ERR(task);
8138 goto out;
8139 }
8140 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008141 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008142 ret = task->tk_status;
8143 rpc_put_task(task);
8144out:
8145 dprintk("<-- %s status=%d\n", __func__, ret);
8146 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008147}
8148
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008149struct nfs4_reclaim_complete_data {
8150 struct nfs_client *clp;
8151 struct nfs41_reclaim_complete_args arg;
8152 struct nfs41_reclaim_complete_res res;
8153};
8154
8155static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8156{
8157 struct nfs4_reclaim_complete_data *calldata = data;
8158
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008159 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008160 &calldata->arg.seq_args,
8161 &calldata->res.seq_res,
8162 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008163}
8164
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008165static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8166{
8167 switch(task->tk_status) {
8168 case 0:
8169 case -NFS4ERR_COMPLETE_ALREADY:
8170 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8171 break;
8172 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008173 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008174 /* fall through */
8175 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008176 return -EAGAIN;
8177 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008178 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008179 }
8180 return 0;
8181}
8182
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008183static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8184{
8185 struct nfs4_reclaim_complete_data *calldata = data;
8186 struct nfs_client *clp = calldata->clp;
8187 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8188
8189 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008190 if (!nfs41_sequence_done(task, res))
8191 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008192
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008193 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008194 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8195 rpc_restart_call_prepare(task);
8196 return;
8197 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008198 dprintk("<-- %s\n", __func__);
8199}
8200
8201static void nfs4_free_reclaim_complete_data(void *data)
8202{
8203 struct nfs4_reclaim_complete_data *calldata = data;
8204
8205 kfree(calldata);
8206}
8207
8208static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8209 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8210 .rpc_call_done = nfs4_reclaim_complete_done,
8211 .rpc_release = nfs4_free_reclaim_complete_data,
8212};
8213
8214/*
8215 * Issue a global reclaim complete.
8216 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008217static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8218 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008219{
8220 struct nfs4_reclaim_complete_data *calldata;
8221 struct rpc_task *task;
8222 struct rpc_message msg = {
8223 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008224 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008225 };
8226 struct rpc_task_setup task_setup_data = {
8227 .rpc_client = clp->cl_rpcclient,
8228 .rpc_message = &msg,
8229 .callback_ops = &nfs4_reclaim_complete_call_ops,
8230 .flags = RPC_TASK_ASYNC,
8231 };
8232 int status = -ENOMEM;
8233
8234 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008235 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008236 if (calldata == NULL)
8237 goto out;
8238 calldata->clp = clp;
8239 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008240
Chuck Levera9c92d62013-08-09 12:48:18 -04008241 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008242 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008243 msg.rpc_argp = &calldata->arg;
8244 msg.rpc_resp = &calldata->res;
8245 task_setup_data.callback_data = calldata;
8246 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008247 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008248 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008249 goto out;
8250 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008251 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008252 if (status == 0)
8253 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008254 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008255 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008256out:
8257 dprintk("<-- %s status=%d\n", __func__, status);
8258 return status;
8259}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008260
8261static void
8262nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8263{
8264 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008265 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008266
8267 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008268 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008269 &lgp->res.seq_res, task);
8270 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008271}
8272
8273static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8274{
8275 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008276
8277 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008278 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008279 dprintk("<-- %s\n", __func__);
8280}
8281
8282static int
8283nfs4_layoutget_handle_exception(struct rpc_task *task,
8284 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8285{
Trond Myklebustee314c22012-10-01 17:25:48 -07008286 struct inode *inode = lgp->args.inode;
8287 struct nfs_server *server = NFS_SERVER(inode);
8288 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008289 int nfs4err = task->tk_status;
8290 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008291 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008292
Boaz Harroshed7e5422014-01-22 20:34:54 +02008293 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008294
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008295 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008296 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008297 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008298
8299 /*
8300 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8301 * on the file. set tk_status to -ENODATA to tell upper layer to
8302 * retry go inband.
8303 */
8304 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008305 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008306 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008307 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008308 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8309 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8310 */
8311 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008312 status = -EOVERFLOW;
8313 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008314 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008315 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008316 * (or clients) writing to the same RAID stripe except when
8317 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008318 *
8319 * Treat it like we would RECALLCONFLICT -- we retry for a little
8320 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008321 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008322 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008323 if (lgp->args.minlength == 0) {
8324 status = -EOVERFLOW;
8325 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008326 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008327 status = -EBUSY;
8328 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008329 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008330 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008331 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008332 case -NFS4ERR_DELEG_REVOKED:
8333 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008334 case -NFS4ERR_EXPIRED:
8335 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008336 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008337 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008338 lo = NFS_I(inode)->layout;
8339 /* If the open stateid was bad, then recover it. */
8340 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8341 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008342 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008343 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008344 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008345 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008346 break;
8347 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008348
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008349 /*
8350 * Mark the bad layout state as invalid, then retry
8351 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008352 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008353 spin_unlock(&inode->i_lock);
8354 pnfs_free_lseg_list(&head);
8355 status = -EAGAIN;
8356 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008357 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008358
Trond Myklebust8ac09252017-01-23 22:44:12 -05008359 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008360 err = nfs4_handle_exception(server, nfs4err, exception);
8361 if (!status) {
8362 if (exception->retry)
8363 status = -EAGAIN;
8364 else
8365 status = err;
8366 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008367out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008368 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008369 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008370}
8371
Idan Kedar85541162012-08-02 11:47:10 +03008372static size_t max_response_pages(struct nfs_server *server)
8373{
8374 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8375 return nfs_page_array_len(0, max_resp_sz);
8376}
8377
8378static void nfs4_free_pages(struct page **pages, size_t size)
8379{
8380 int i;
8381
8382 if (!pages)
8383 return;
8384
8385 for (i = 0; i < size; i++) {
8386 if (!pages[i])
8387 break;
8388 __free_page(pages[i]);
8389 }
8390 kfree(pages);
8391}
8392
8393static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8394{
8395 struct page **pages;
8396 int i;
8397
8398 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8399 if (!pages) {
8400 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8401 return NULL;
8402 }
8403
8404 for (i = 0; i < size; i++) {
8405 pages[i] = alloc_page(gfp_flags);
8406 if (!pages[i]) {
8407 dprintk("%s: failed to allocate page\n", __func__);
8408 nfs4_free_pages(pages, size);
8409 return NULL;
8410 }
8411 }
8412
8413 return pages;
8414}
8415
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008416static void nfs4_layoutget_release(void *calldata)
8417{
8418 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008419 struct inode *inode = lgp->args.inode;
8420 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008421 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008422
8423 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008424 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008425 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008426 put_nfs_open_context(lgp->args.ctx);
8427 kfree(calldata);
8428 dprintk("<-- %s\n", __func__);
8429}
8430
8431static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8432 .rpc_call_prepare = nfs4_layoutget_prepare,
8433 .rpc_call_done = nfs4_layoutget_done,
8434 .rpc_release = nfs4_layoutget_release,
8435};
8436
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008437struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008438nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008439{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008440 struct inode *inode = lgp->args.inode;
8441 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008442 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008443 struct rpc_task *task;
8444 struct rpc_message msg = {
8445 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8446 .rpc_argp = &lgp->args,
8447 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008448 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008449 };
8450 struct rpc_task_setup task_setup_data = {
8451 .rpc_client = server->client,
8452 .rpc_message = &msg,
8453 .callback_ops = &nfs4_layoutget_call_ops,
8454 .callback_data = lgp,
8455 .flags = RPC_TASK_ASYNC,
8456 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008457 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008458 struct nfs4_exception exception = {
8459 .inode = inode,
8460 .timeout = *timeout,
8461 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008462 int status = 0;
8463
8464 dprintk("--> %s\n", __func__);
8465
Peng Tao4bd5a9802014-11-17 11:05:17 +08008466 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8467 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8468
Idan Kedar85541162012-08-02 11:47:10 +03008469 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8470 if (!lgp->args.layout.pages) {
8471 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008472 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008473 }
8474 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8475
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008476 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008477 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008478 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008479
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008480 task = rpc_run_task(&task_setup_data);
8481 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008482 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008483 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008484 if (status == 0) {
8485 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8486 *timeout = exception.timeout;
8487 }
8488
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008489 trace_nfs4_layoutget(lgp->args.ctx,
8490 &lgp->args.range,
8491 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008492 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008493 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008494
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008495 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8496 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008497 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008498 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008499 rpc_put_task(task);
8500 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008501 if (status)
8502 return ERR_PTR(status);
8503 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008504}
8505
Benny Halevycbe82602011-05-22 19:52:37 +03008506static void
8507nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8508{
8509 struct nfs4_layoutreturn *lrp = calldata;
8510
8511 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008512 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008513 &lrp->args.seq_args,
8514 &lrp->res.seq_res,
8515 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008516}
8517
8518static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8519{
8520 struct nfs4_layoutreturn *lrp = calldata;
8521 struct nfs_server *server;
8522
8523 dprintk("--> %s\n", __func__);
8524
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008525 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008526 return;
8527
8528 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008529 switch (task->tk_status) {
8530 default:
8531 task->tk_status = 0;
8532 case 0:
8533 break;
8534 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008535 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008536 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008537 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008538 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008539 return;
8540 }
Benny Halevycbe82602011-05-22 19:52:37 +03008541 dprintk("<-- %s\n", __func__);
8542}
8543
8544static void nfs4_layoutreturn_release(void *calldata)
8545{
8546 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008547 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008548
8549 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008550 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008551 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008552 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008553 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8554 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008555 pnfs_put_layout_hdr(lrp->args.layout);
8556 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008557 kfree(calldata);
8558 dprintk("<-- %s\n", __func__);
8559}
8560
8561static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8562 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8563 .rpc_call_done = nfs4_layoutreturn_done,
8564 .rpc_release = nfs4_layoutreturn_release,
8565};
8566
Peng Tao6c166052014-11-17 09:30:40 +08008567int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008568{
8569 struct rpc_task *task;
8570 struct rpc_message msg = {
8571 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8572 .rpc_argp = &lrp->args,
8573 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008574 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008575 };
8576 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008577 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008578 .rpc_message = &msg,
8579 .callback_ops = &nfs4_layoutreturn_call_ops,
8580 .callback_data = lrp,
8581 };
Peng Tao6c166052014-11-17 09:30:40 +08008582 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008583
Andrew Elble99ade3c2015-12-02 09:39:51 -05008584 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8585 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8586 &task_setup_data.rpc_client, &msg);
8587
Benny Halevycbe82602011-05-22 19:52:37 +03008588 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008589 if (!sync) {
8590 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8591 if (!lrp->inode) {
8592 nfs4_layoutreturn_release(lrp);
8593 return -EAGAIN;
8594 }
8595 task_setup_data.flags |= RPC_TASK_ASYNC;
8596 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008597 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008598 task = rpc_run_task(&task_setup_data);
8599 if (IS_ERR(task))
8600 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008601 if (sync)
8602 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008603 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008604 dprintk("<-- %s status=%d\n", __func__, status);
8605 rpc_put_task(task);
8606 return status;
8607}
8608
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008609static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008610_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8611 struct pnfs_device *pdev,
8612 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008613{
8614 struct nfs4_getdeviceinfo_args args = {
8615 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008616 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8617 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008618 };
8619 struct nfs4_getdeviceinfo_res res = {
8620 .pdev = pdev,
8621 };
8622 struct rpc_message msg = {
8623 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8624 .rpc_argp = &args,
8625 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008626 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008627 };
8628 int status;
8629
8630 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008631 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008632 if (res.notification & ~args.notify_types)
8633 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008634 if (res.notification != args.notify_types)
8635 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008636
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008637 dprintk("<-- %s status=%d\n", __func__, status);
8638
8639 return status;
8640}
8641
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008642int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8643 struct pnfs_device *pdev,
8644 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008645{
8646 struct nfs4_exception exception = { };
8647 int err;
8648
8649 do {
8650 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008651 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008652 &exception);
8653 } while (exception.retry);
8654 return err;
8655}
8656EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8657
Andy Adamson863a3c62011-03-23 13:27:54 +00008658static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8659{
8660 struct nfs4_layoutcommit_data *data = calldata;
8661 struct nfs_server *server = NFS_SERVER(data->args.inode);
8662
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008663 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008664 &data->args.seq_args,
8665 &data->res.seq_res,
8666 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008667}
8668
8669static void
8670nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8671{
8672 struct nfs4_layoutcommit_data *data = calldata;
8673 struct nfs_server *server = NFS_SERVER(data->args.inode);
8674
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008675 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008676 return;
8677
8678 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008679 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8680 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8681 case -NFS4ERR_BADLAYOUT: /* no layout */
8682 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008683 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008684 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008685 break;
8686 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008687 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008688 rpc_restart_call_prepare(task);
8689 return;
8690 }
8691 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008692}
8693
8694static void nfs4_layoutcommit_release(void *calldata)
8695{
8696 struct nfs4_layoutcommit_data *data = calldata;
8697
Andy Adamsondb29c082011-07-30 20:52:38 -04008698 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008699 nfs_post_op_update_inode_force_wcc(data->args.inode,
8700 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008701 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008702 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008703 kfree(data);
8704}
8705
8706static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8707 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8708 .rpc_call_done = nfs4_layoutcommit_done,
8709 .rpc_release = nfs4_layoutcommit_release,
8710};
8711
8712int
Andy Adamsonef311532011-03-12 02:58:10 -05008713nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008714{
8715 struct rpc_message msg = {
8716 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8717 .rpc_argp = &data->args,
8718 .rpc_resp = &data->res,
8719 .rpc_cred = data->cred,
8720 };
8721 struct rpc_task_setup task_setup_data = {
8722 .task = &data->task,
8723 .rpc_client = NFS_CLIENT(data->args.inode),
8724 .rpc_message = &msg,
8725 .callback_ops = &nfs4_layoutcommit_ops,
8726 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008727 };
8728 struct rpc_task *task;
8729 int status = 0;
8730
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008731 dprintk("NFS: initiating layoutcommit call. sync %d "
8732 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008733 data->args.lastbytewritten,
8734 data->args.inode->i_ino);
8735
Trond Myklebust472e2592015-02-05 16:50:30 -05008736 if (!sync) {
8737 data->inode = nfs_igrab_and_active(data->args.inode);
8738 if (data->inode == NULL) {
8739 nfs4_layoutcommit_release(data);
8740 return -EAGAIN;
8741 }
8742 task_setup_data.flags = RPC_TASK_ASYNC;
8743 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008744 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008745 task = rpc_run_task(&task_setup_data);
8746 if (IS_ERR(task))
8747 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008748 if (sync)
8749 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008750 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008751 dprintk("%s: status %d\n", __func__, status);
8752 rpc_put_task(task);
8753 return status;
8754}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008755
Andy Adamson97431202013-08-08 10:57:56 -04008756/**
8757 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8758 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8759 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008760static int
8761_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008762 struct nfs_fsinfo *info,
8763 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008764{
8765 struct nfs41_secinfo_no_name_args args = {
8766 .style = SECINFO_STYLE_CURRENT_FH,
8767 };
8768 struct nfs4_secinfo_res res = {
8769 .flavors = flavors,
8770 };
8771 struct rpc_message msg = {
8772 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8773 .rpc_argp = &args,
8774 .rpc_resp = &res,
8775 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008776 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008777 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008778 int status;
8779
8780 if (use_integrity) {
8781 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008782 cred = nfs4_get_clid_cred(server->nfs_client);
8783 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008784 }
8785
8786 dprintk("--> %s\n", __func__);
8787 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8788 &res.seq_res, 0);
8789 dprintk("<-- %s status=%d\n", __func__, status);
8790
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008791 if (cred)
8792 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008793
8794 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008795}
8796
8797static int
8798nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8799 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8800{
8801 struct nfs4_exception exception = { };
8802 int err;
8803 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008804 /* first try using integrity protection */
8805 err = -NFS4ERR_WRONGSEC;
8806
8807 /* try to use integrity protection with machine cred */
8808 if (_nfs4_is_integrity_protected(server->nfs_client))
8809 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8810 flavors, true);
8811
8812 /*
8813 * if unable to use integrity protection, or SECINFO with
8814 * integrity protection returns NFS4ERR_WRONGSEC (which is
8815 * disallowed by spec, but exists in deployed servers) use
8816 * the current filesystem's rpc_client and the user cred.
8817 */
8818 if (err == -NFS4ERR_WRONGSEC)
8819 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8820 flavors, false);
8821
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008822 switch (err) {
8823 case 0:
8824 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008825 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008826 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008827 default:
8828 err = nfs4_handle_exception(server, err, &exception);
8829 }
8830 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008831out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008832 return err;
8833}
8834
8835static int
8836nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8837 struct nfs_fsinfo *info)
8838{
8839 int err;
8840 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008841 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008842 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008843 struct nfs4_secinfo4 *secinfo;
8844 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008845
8846 page = alloc_page(GFP_KERNEL);
8847 if (!page) {
8848 err = -ENOMEM;
8849 goto out;
8850 }
8851
8852 flavors = page_address(page);
8853 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8854
8855 /*
8856 * Fall back on "guess and check" method if
8857 * the server doesn't support SECINFO_NO_NAME
8858 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008859 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008860 err = nfs4_find_root_sec(server, fhandle, info);
8861 goto out_freepage;
8862 }
8863 if (err)
8864 goto out_freepage;
8865
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008866 for (i = 0; i < flavors->num_flavors; i++) {
8867 secinfo = &flavors->flavors[i];
8868
8869 switch (secinfo->flavor) {
8870 case RPC_AUTH_NULL:
8871 case RPC_AUTH_UNIX:
8872 case RPC_AUTH_GSS:
8873 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8874 &secinfo->flavor_info);
8875 break;
8876 default:
8877 flavor = RPC_AUTH_MAXFLAVOR;
8878 break;
8879 }
8880
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008881 if (!nfs_auth_info_match(&server->auth_info, flavor))
8882 flavor = RPC_AUTH_MAXFLAVOR;
8883
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008884 if (flavor != RPC_AUTH_MAXFLAVOR) {
8885 err = nfs4_lookup_root_sec(server, fhandle,
8886 info, flavor);
8887 if (!err)
8888 break;
8889 }
8890 }
8891
8892 if (flavor == RPC_AUTH_MAXFLAVOR)
8893 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008894
8895out_freepage:
8896 put_page(page);
8897 if (err == -EACCES)
8898 return -EPERM;
8899out:
8900 return err;
8901}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008902
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008903static int _nfs41_test_stateid(struct nfs_server *server,
8904 nfs4_stateid *stateid,
8905 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008906{
8907 int status;
8908 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008909 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008910 };
8911 struct nfs41_test_stateid_res res;
8912 struct rpc_message msg = {
8913 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8914 .rpc_argp = &args,
8915 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008916 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008917 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008918 struct rpc_clnt *rpc_client = server->client;
8919
8920 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8921 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008922
Chuck Lever38527b12012-07-11 16:30:23 -04008923 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008924 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008925 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008926 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008927 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008928 if (status != NFS_OK) {
8929 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008930 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008931 }
8932 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008933 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008934}
8935
Trond Myklebust43912bb2016-09-22 13:38:56 -04008936static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8937 int err, struct nfs4_exception *exception)
8938{
8939 exception->retry = 0;
8940 switch(err) {
8941 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008942 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008943 nfs4_handle_exception(server, err, exception);
8944 break;
8945 case -NFS4ERR_BADSESSION:
8946 case -NFS4ERR_BADSLOT:
8947 case -NFS4ERR_BAD_HIGH_SLOT:
8948 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8949 case -NFS4ERR_DEADSESSION:
8950 nfs4_do_handle_exception(server, err, exception);
8951 }
8952}
8953
Chuck Lever38527b12012-07-11 16:30:23 -04008954/**
8955 * nfs41_test_stateid - perform a TEST_STATEID operation
8956 *
8957 * @server: server / transport on which to perform the operation
8958 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008959 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008960 *
8961 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8962 * Otherwise a negative NFS4ERR value is returned if the operation
8963 * failed or the state ID is not currently valid.
8964 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008965static int nfs41_test_stateid(struct nfs_server *server,
8966 nfs4_stateid *stateid,
8967 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008968{
8969 struct nfs4_exception exception = { };
8970 int err;
8971 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008972 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04008973 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008974 } while (exception.retry);
8975 return err;
8976}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008977
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008978struct nfs_free_stateid_data {
8979 struct nfs_server *server;
8980 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008981 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008982};
8983
8984static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8985{
8986 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008987 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008988 &data->args.seq_args,
8989 &data->res.seq_res,
8990 task);
8991}
8992
8993static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8994{
8995 struct nfs_free_stateid_data *data = calldata;
8996
8997 nfs41_sequence_done(task, &data->res.seq_res);
8998
8999 switch (task->tk_status) {
9000 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009001 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009002 rpc_restart_call_prepare(task);
9003 }
9004}
9005
9006static void nfs41_free_stateid_release(void *calldata)
9007{
9008 kfree(calldata);
9009}
9010
Trond Myklebust17f26b12013-08-21 15:48:42 -04009011static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009012 .rpc_call_prepare = nfs41_free_stateid_prepare,
9013 .rpc_call_done = nfs41_free_stateid_done,
9014 .rpc_release = nfs41_free_stateid_release,
9015};
9016
9017static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009018 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009019 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009020 bool privileged)
9021{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009022 struct rpc_message msg = {
9023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009024 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009025 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009026 struct rpc_task_setup task_setup = {
9027 .rpc_client = server->client,
9028 .rpc_message = &msg,
9029 .callback_ops = &nfs41_free_stateid_ops,
9030 .flags = RPC_TASK_ASYNC,
9031 };
9032 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009033
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009034 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9035 &task_setup.rpc_client, &msg);
9036
Chuck Lever38527b12012-07-11 16:30:23 -04009037 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009038 data = kmalloc(sizeof(*data), GFP_NOFS);
9039 if (!data)
9040 return ERR_PTR(-ENOMEM);
9041 data->server = server;
9042 nfs4_stateid_copy(&data->args.stateid, stateid);
9043
9044 task_setup.callback_data = data;
9045
9046 msg.rpc_argp = &data->args;
9047 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009048 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009049 if (privileged)
9050 nfs4_set_sequence_privileged(&data->args.seq_args);
9051
9052 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009053}
9054
Chuck Lever38527b12012-07-11 16:30:23 -04009055/**
9056 * nfs41_free_stateid - perform a FREE_STATEID operation
9057 *
9058 * @server: server / transport on which to perform the operation
9059 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009060 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009061 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009062 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009063 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009064 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009065static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009066 const nfs4_stateid *stateid,
9067 struct rpc_cred *cred,
9068 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009069{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009070 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009071
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009072 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009073 if (IS_ERR(task))
9074 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009075 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009076 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009077}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009078
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009079static void
9080nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009081{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009082 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009083
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009084 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009085 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009086}
9087
Trond Myklebust36281ca2012-03-04 18:13:56 -05009088static bool nfs41_match_stateid(const nfs4_stateid *s1,
9089 const nfs4_stateid *s2)
9090{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009091 if (s1->type != s2->type)
9092 return false;
9093
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009094 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009095 return false;
9096
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009097 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009098 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009099
Anna Schumaker045c5512017-01-11 16:59:48 -05009100 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009101}
9102
Andy Adamson557134a2009-04-01 09:21:53 -04009103#endif /* CONFIG_NFS_V4_1 */
9104
Trond Myklebust36281ca2012-03-04 18:13:56 -05009105static bool nfs4_match_stateid(const nfs4_stateid *s1,
9106 const nfs4_stateid *s2)
9107{
Trond Myklebustf597c532012-03-04 18:13:56 -05009108 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009109}
9110
9111
Trond Myklebust17280172012-03-11 13:11:00 -04009112static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009113 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009114 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009115 .recover_open = nfs4_open_reclaim,
9116 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009117 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009118 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009119};
9120
Andy Adamson591d71c2009-04-01 09:22:47 -04009121#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009122static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009123 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9124 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9125 .recover_open = nfs4_open_reclaim,
9126 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009127 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009128 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009129 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009130};
9131#endif /* CONFIG_NFS_V4_1 */
9132
Trond Myklebust17280172012-03-11 13:11:00 -04009133static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009134 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009135 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009136 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009137 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009138 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009139};
9140
Andy Adamson591d71c2009-04-01 09:22:47 -04009141#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009142static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009143 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9144 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009145 .recover_open = nfs41_open_expired,
9146 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009147 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009148};
9149#endif /* CONFIG_NFS_V4_1 */
9150
Trond Myklebust17280172012-03-11 13:11:00 -04009151static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009152 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009153 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009154 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009155};
9156
9157#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009158static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009159 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009160 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009161 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009162};
9163#endif
9164
Chuck Leverec011fe2013-10-17 14:12:39 -04009165static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009166 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009167 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009168};
9169
9170#if defined(CONFIG_NFS_V4_1)
9171static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009172 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009173 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009174};
9175#endif /* CONFIG_NFS_V4_1 */
9176
Trond Myklebust97dc1352010-06-16 09:52:26 -04009177static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9178 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009179 .init_caps = NFS_CAP_READDIRPLUS
9180 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009181 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009182 .init_client = nfs40_init_client,
9183 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009184 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009185 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009186 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009187 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009188 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009189 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009190 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9191 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9192 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009193 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009194};
9195
9196#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009197static struct nfs_seqid *
9198nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9199{
9200 return NULL;
9201}
9202
Trond Myklebust97dc1352010-06-16 09:52:26 -04009203static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9204 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009205 .init_caps = NFS_CAP_READDIRPLUS
9206 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009207 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009208 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009209 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009210 .init_client = nfs41_init_client,
9211 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009212 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009213 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009214 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009215 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009216 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009217 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009218 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009219 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9220 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9221 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009222 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009223};
9224#endif
9225
Steve Dickson42c2c422013-05-22 12:50:38 -04009226#if defined(CONFIG_NFS_V4_2)
9227static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9228 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009229 .init_caps = NFS_CAP_READDIRPLUS
9230 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009231 | NFS_CAP_POSIX_LOCK
9232 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009233 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009234 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009235 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009236 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009237 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009238 | NFS_CAP_LAYOUTSTATS
9239 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009240 .init_client = nfs41_init_client,
9241 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009242 .match_stateid = nfs41_match_stateid,
9243 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009244 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009245 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009246 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009247 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009248 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009249 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9250 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9251 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009252 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009253};
9254#endif
9255
Trond Myklebust97dc1352010-06-16 09:52:26 -04009256const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9257 [0] = &nfs_v4_0_minor_ops,
9258#if defined(CONFIG_NFS_V4_1)
9259 [1] = &nfs_v4_1_minor_ops,
9260#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009261#if defined(CONFIG_NFS_V4_2)
9262 [2] = &nfs_v4_2_minor_ops,
9263#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009264};
9265
Trond Myklebust13997822016-07-24 17:10:52 -04009266static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009267{
9268 ssize_t error, error2;
9269
9270 error = generic_listxattr(dentry, list, size);
9271 if (error < 0)
9272 return error;
9273 if (list) {
9274 list += error;
9275 size -= error;
9276 }
9277
9278 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9279 if (error2 < 0)
9280 return error2;
9281 return error + error2;
9282}
9283
Trond Myklebust17f26b12013-08-21 15:48:42 -04009284static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009285 .create = nfs_create,
9286 .lookup = nfs_lookup,
9287 .atomic_open = nfs_atomic_open,
9288 .link = nfs_link,
9289 .unlink = nfs_unlink,
9290 .symlink = nfs_symlink,
9291 .mkdir = nfs_mkdir,
9292 .rmdir = nfs_rmdir,
9293 .mknod = nfs_mknod,
9294 .rename = nfs_rename,
9295 .permission = nfs_permission,
9296 .getattr = nfs_getattr,
9297 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009298 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009299};
9300
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009301static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009302 .permission = nfs_permission,
9303 .getattr = nfs_getattr,
9304 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009305 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009306};
9307
David Howells509de812006-08-22 20:06:11 -04009308const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009309 .version = 4, /* protocol version */
9310 .dentry_ops = &nfs4_dentry_operations,
9311 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009312 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009313 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009314 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009315 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009316 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009317 .getattr = nfs4_proc_getattr,
9318 .setattr = nfs4_proc_setattr,
9319 .lookup = nfs4_proc_lookup,
9320 .access = nfs4_proc_access,
9321 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009322 .create = nfs4_proc_create,
9323 .remove = nfs4_proc_remove,
9324 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009325 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009326 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009327 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009328 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009329 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009330 .link = nfs4_proc_link,
9331 .symlink = nfs4_proc_symlink,
9332 .mkdir = nfs4_proc_mkdir,
9333 .rmdir = nfs4_proc_remove,
9334 .readdir = nfs4_proc_readdir,
9335 .mknod = nfs4_proc_mknod,
9336 .statfs = nfs4_proc_statfs,
9337 .fsinfo = nfs4_proc_fsinfo,
9338 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009339 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009340 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009341 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009342 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009343 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009344 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009345 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009346 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009347 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009348 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009349 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009350 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009351 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009352 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009353 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009354 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009355 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009356 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009357 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009358 .create_server = nfs4_create_server,
9359 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009360};
9361
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009362static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009363 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009364 .list = nfs4_xattr_list_nfs4_acl,
9365 .get = nfs4_xattr_get_nfs4_acl,
9366 .set = nfs4_xattr_set_nfs4_acl,
9367};
9368
9369const struct xattr_handler *nfs4_xattr_handlers[] = {
9370 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009371#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9372 &nfs4_xattr_nfs4_label_handler,
9373#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009374 NULL
9375};
9376
Linus Torvalds1da177e2005-04-16 15:20:36 -07009377/*
9378 * Local variables:
9379 * c-basic-offset: 8
9380 * End:
9381 */