blob: 1b183686c6d4f06c3b1d4ed044c527bff6ba4a83 [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 Myklebust035168ab2010-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 Schumaker9dd9107f2017-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 Schumaker9dd9107f2017-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 Myklebust6926afd12012-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 Myklebust6926afd12012-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 Myklebust6926afd12012-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 Myklebust6926afd12012-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
2261 /* even though OPEN succeeded, access is denied. Close the file */
2262 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002263 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002264}
2265
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002266/*
2267 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2268 */
2269static int _nfs4_proc_open(struct nfs4_opendata *data)
2270{
David Howells2b0143b2015-03-17 22:25:59 +00002271 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002272 struct nfs_server *server = NFS_SERVER(dir);
2273 struct nfs_openargs *o_arg = &data->o_arg;
2274 struct nfs_openres *o_res = &data->o_res;
2275 int status;
2276
2277 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002278 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002279 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002280 if (status != 0) {
2281 if (status == -NFS4ERR_BADNAME &&
2282 !(o_arg->open_flags & O_CREAT))
2283 return -ENOENT;
2284 return status;
2285 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002286
Trond Myklebust6926afd12012-01-07 13:22:46 -05002287 nfs_fattr_map_and_free_names(server, &data->f_attr);
2288
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002289 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002290 if (o_arg->open_flags & O_EXCL)
2291 data->file_created = 1;
2292 else if (o_res->cinfo.before != o_res->cinfo.after)
2293 data->file_created = 1;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002294 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002295 update_changeattr(dir, &o_res->cinfo,
2296 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002297 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002298 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2299 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002301 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002303 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 }
2305 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002306 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308}
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310/*
2311 * OPEN_EXPIRED:
2312 * reclaim state on the server after a network partition.
2313 * Assumes caller holds the appropriate lock
2314 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002315static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002317 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002318 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002320 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002321 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002322 if (IS_ERR(opendata))
2323 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002324 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002325 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002326 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002327 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002328 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002331static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002332{
Trond Myklebust539cd032007-06-05 11:46:42 -04002333 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002334 struct nfs4_exception exception = { };
2335 int err;
2336
2337 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002338 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002339 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002340 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2341 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002342 switch (err) {
2343 default:
2344 goto out;
2345 case -NFS4ERR_GRACE:
2346 case -NFS4ERR_DELAY:
2347 nfs4_handle_exception(server, err, &exception);
2348 err = 0;
2349 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002350 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002351out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002352 return err;
2353}
2354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2356{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002358 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Trond Myklebust864472e2006-01-03 09:55:15 +01002360 ctx = nfs4_state_find_open_context(state);
2361 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002362 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002363 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002364 put_nfs_open_context(ctx);
2365 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
2367
Trond Myklebust41020b62016-09-22 13:38:58 -04002368static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2369 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002370{
Trond Myklebust41020b62016-09-22 13:38:58 -04002371 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002372 write_seqlock(&state->seqlock);
2373 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2374 write_sequnlock(&state->seqlock);
2375 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2376}
2377
2378static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2379{
2380 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002381 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002382}
2383
2384static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2385{
2386 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2387 nfs40_clear_delegation_stateid(state);
2388 return nfs4_open_expired(sp, state);
2389}
2390
Trond Myklebust45870d62016-09-22 13:38:59 -04002391static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2392 nfs4_stateid *stateid,
2393 struct rpc_cred *cred)
2394{
2395 return -NFS4ERR_BAD_STATEID;
2396}
2397
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002398#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002399static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2400 nfs4_stateid *stateid,
2401 struct rpc_cred *cred)
2402{
2403 int status;
2404
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002405 switch (stateid->type) {
2406 default:
2407 break;
2408 case NFS4_INVALID_STATEID_TYPE:
2409 case NFS4_SPECIAL_STATEID_TYPE:
2410 return -NFS4ERR_BAD_STATEID;
2411 case NFS4_REVOKED_STATEID_TYPE:
2412 goto out_free;
2413 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002414
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002415 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002416 switch (status) {
2417 case -NFS4ERR_EXPIRED:
2418 case -NFS4ERR_ADMIN_REVOKED:
2419 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002420 break;
2421 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002422 return status;
2423 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002424out_free:
2425 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002426 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002427 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002428}
2429
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002430static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002431{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002432 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002433 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002434 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002435 struct rpc_cred *cred;
2436 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002437
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002438 /* Get the delegation credential for use by test/free_stateid */
2439 rcu_read_lock();
2440 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002441 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002442 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002443 return;
2444 }
2445
2446 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002447 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2448 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
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002453 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2454 rcu_read_unlock();
2455 return;
2456 }
2457
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002458 cred = get_rpccred(delegation->cred);
2459 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002460 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002461 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002462 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002463 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002464
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002465 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002466}
2467
2468/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002469 * nfs41_check_expired_locks - possibly free a lock stateid
2470 *
2471 * @state: NFSv4 state for an inode
2472 *
2473 * Returns NFS_OK if recovery for this stateid is now finished.
2474 * Otherwise a negative NFS4ERR value is returned.
2475 */
2476static int nfs41_check_expired_locks(struct nfs4_state *state)
2477{
2478 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002479 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002480 struct nfs_server *server = NFS_SERVER(state->inode);
2481
2482 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2483 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002484
2485 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002486 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2487 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2488 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2489
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002490 atomic_inc(&lsp->ls_count);
2491 spin_unlock(&state->state_lock);
2492
2493 nfs4_put_lock_state(prev);
2494 prev = lsp;
2495
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002496 status = nfs41_test_and_free_expired_stateid(server,
2497 &lsp->ls_stateid,
2498 cred);
2499 trace_nfs4_test_lock_stateid(state, lsp, status);
2500 if (status == -NFS4ERR_EXPIRED ||
2501 status == -NFS4ERR_BAD_STATEID) {
2502 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002503 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002504 if (!recover_lost_locks)
2505 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2506 } else if (status != NFS_OK) {
2507 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002508 nfs4_put_lock_state(prev);
2509 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002510 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002511 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002512 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002513 }
2514 spin_unlock(&state->state_lock);
2515 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002516out:
2517 return ret;
2518}
2519
2520/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002521 * nfs41_check_open_stateid - possibly free an open stateid
2522 *
2523 * @state: NFSv4 state for an inode
2524 *
2525 * Returns NFS_OK if recovery for this stateid is now finished.
2526 * Otherwise a negative NFS4ERR value is returned.
2527 */
2528static int nfs41_check_open_stateid(struct nfs4_state *state)
2529{
2530 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002531 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002532 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002533 int status;
2534
Trond Myklebustb134fc42016-09-22 13:39:16 -04002535 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002536 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2537 if (nfs4_have_delegation(state->inode, state->state))
2538 return NFS_OK;
2539 return -NFS4ERR_OPENMODE;
2540 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002541 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002542 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002543 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002544 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002545 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002546 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2547 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2548 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002549 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002550 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002551 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002552 if (status != NFS_OK)
2553 return status;
2554 if (nfs_open_stateid_recover_openmode(state))
2555 return -NFS4ERR_OPENMODE;
2556 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002557}
2558
2559static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2560{
Chuck Levereb64cf92012-07-11 16:30:05 -04002561 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002562
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002563 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002564 status = nfs41_check_expired_locks(state);
2565 if (status != NFS_OK)
2566 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002567 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002568 if (status != NFS_OK)
2569 status = nfs4_open_expired(sp, state);
2570 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002571}
2572#endif
2573
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002575 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2576 * fields corresponding to attributes that were used to store the verifier.
2577 * Make sure we clobber those fields in the later setattr call
2578 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002579static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2580 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002581{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002582 const u32 *attrset = opendata->o_res.attrset;
2583
2584 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002585 !(sattr->ia_valid & ATTR_ATIME_SET))
2586 sattr->ia_valid |= ATTR_ATIME;
2587
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002588 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002589 !(sattr->ia_valid & ATTR_MTIME_SET))
2590 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002591
2592 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002593 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2594 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002595 sattr->ia_valid &= ~ATTR_MODE;
2596
2597 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2598 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002599}
2600
Trond Myklebustc21443c2013-02-07 14:26:21 -05002601static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2602 fmode_t fmode,
2603 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002604 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002605{
2606 struct nfs4_state_owner *sp = opendata->owner;
2607 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002608 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002609 struct nfs4_state *state;
2610 unsigned int seq;
2611 int ret;
2612
2613 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2614
2615 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002616 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002617 goto out;
2618
2619 state = nfs4_opendata_to_nfs4_state(opendata);
2620 ret = PTR_ERR(state);
2621 if (IS_ERR(state))
2622 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002623 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002624 if (server->caps & NFS_CAP_POSIX_LOCK)
2625 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002626 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2627 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002628
Trond Myklebust275bb302013-05-29 13:11:28 -04002629 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002630 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002631 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002632 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002633 alias = d_exact_alias(dentry, state->inode);
2634 if (!alias)
2635 alias = d_splice_alias(igrab(state->inode), dentry);
2636 /* d_splice_alias() can't fail here - it's a non-directory */
2637 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002638 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002639 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002640 }
2641 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002642 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002643 }
2644
Trond Myklebustc21443c2013-02-07 14:26:21 -05002645 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2646 if (ret != 0)
2647 goto out;
2648
David Howells2b0143b2015-03-17 22:25:59 +00002649 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002650 nfs_inode_attach_open_context(ctx);
2651 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2652 nfs4_schedule_stateid_recovery(server, state);
2653 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002654out:
2655 return ret;
2656}
2657
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002658/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002659 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 */
Andy Adamson82be4172012-05-23 05:02:35 -04002661static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002662 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002663 int flags,
2664 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002665 struct nfs4_label *label,
2666 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
2668 struct nfs4_state_owner *sp;
2669 struct nfs4_state *state = NULL;
2670 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002671 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002672 struct dentry *dentry = ctx->dentry;
2673 struct rpc_cred *cred = ctx->cred;
2674 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2675 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002676 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002677 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002678 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
2680 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002682 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2683 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2685 goto out_err;
2686 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002687 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002688 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002689 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002690 if (d_really_is_positive(dentry))
2691 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002692 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002693 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002694 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002695 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002696 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002697 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002698 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
David Quigley14c43f72013-05-22 12:50:43 -04002700 if (label) {
2701 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2702 if (IS_ERR(olabel)) {
2703 status = PTR_ERR(olabel);
2704 goto err_opendata_put;
2705 }
2706 }
2707
Trond Myklebuste911b812014-03-26 13:24:37 -07002708 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2709 if (!opendata->f_attr.mdsthreshold) {
2710 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2711 if (!opendata->f_attr.mdsthreshold)
2712 goto err_free_label;
2713 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002714 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002715 }
David Howells2b0143b2015-03-17 22:25:59 +00002716 if (d_really_is_positive(dentry))
2717 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002718
Trond Myklebust3efb9722013-05-29 13:17:04 -04002719 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002720 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002721 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002722 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002723
NeilBrownefcbc042015-07-30 13:00:56 +10002724 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002725 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002726 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002727 /*
2728 * send create attributes which was not set by open
2729 * with an extra setattr.
2730 */
2731 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2732 nfs_fattr_init(opendata->o_res.f_attr);
2733 status = nfs4_do_setattr(state->inode, cred,
2734 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002735 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002736 if (status == 0) {
2737 nfs_setattr_update_inode(state->inode, sattr,
2738 opendata->o_res.f_attr);
2739 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2740 }
David Quigley1775fd32013-05-22 12:50:42 -04002741 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002742 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002743 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002744 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002745
Trond Myklebuste911b812014-03-26 13:24:37 -07002746 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002747 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002748 opendata->f_attr.mdsthreshold = NULL;
2749 }
Andy Adamson82be4172012-05-23 05:02:35 -04002750
David Quigley14c43f72013-05-22 12:50:43 -04002751 nfs4_label_free(olabel);
2752
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002753 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002756err_free_label:
2757 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002758err_opendata_put:
2759 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002760err_put_state_owner:
2761 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 return status;
2764}
2765
2766
Andy Adamson82be4172012-05-23 05:02:35 -04002767static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002768 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002769 int flags,
2770 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002771 struct nfs4_label *label,
2772 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002774 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 struct nfs4_exception exception = { };
2776 struct nfs4_state *res;
2777 int status;
2778
2779 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002780 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002781 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002782 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 if (status == 0)
2784 break;
2785 /* NOTE: BAD_SEQID means the server and client disagree about the
2786 * book-keeping w.r.t. state-changing operations
2787 * (OPEN/CLOSE/LOCK/LOCKU...)
2788 * It is actually a sign of a bug on the client or on the server.
2789 *
2790 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002791 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 * have unhashed the old state_owner for us, and that we can
2793 * therefore safely retry using a new one. We should still warn
2794 * the user though...
2795 */
2796 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002797 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002798 " returned a bad sequence-id error!\n",
2799 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 exception.retry = 1;
2801 continue;
2802 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002803 /*
2804 * BAD_STATEID on OPEN means that the server cancelled our
2805 * state before it received the OPEN_CONFIRM.
2806 * Recover by retrying the request as per the discussion
2807 * on Page 181 of RFC3530.
2808 */
2809 if (status == -NFS4ERR_BAD_STATEID) {
2810 exception.retry = 1;
2811 continue;
2812 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002813 if (status == -EAGAIN) {
2814 /* We must have found a delegation */
2815 exception.retry = 1;
2816 continue;
2817 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002818 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2819 continue;
2820 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 status, &exception));
2822 } while (exception.retry);
2823 return res;
2824}
2825
Trond Myklebust8487c472016-06-26 08:44:35 -04002826static int _nfs4_do_setattr(struct inode *inode,
2827 struct nfs_setattrargs *arg,
2828 struct nfs_setattrres *res,
2829 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002830 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002831{
2832 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002833 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04002834 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2835 .rpc_argp = arg,
2836 .rpc_resp = res,
2837 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002838 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002839 struct rpc_cred *delegation_cred = NULL;
2840 unsigned long timestamp = jiffies;
2841 fmode_t fmode;
2842 bool truncate;
2843 int status;
2844
2845 nfs_fattr_init(res->fattr);
2846
2847 /* Servers should only apply open mode checks for file size changes */
2848 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2849 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2850
2851 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2852 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002853 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002854 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002855 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002856 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002857 l_ctx = nfs_get_lock_context(ctx);
2858 if (IS_ERR(l_ctx))
2859 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002860 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2861 &arg->stateid, &delegation_cred);
2862 nfs_put_lock_context(l_ctx);
2863 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002864 return -EBADF;
2865 } else
2866 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2867 if (delegation_cred)
2868 msg.rpc_cred = delegation_cred;
2869
2870 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2871
2872 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002873 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002874 renew_lease(server, timestamp);
2875 trace_nfs4_setattr(inode, &arg->stateid, status);
2876 return status;
2877}
2878
2879static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2880 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002881 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002882 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002884 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002885 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002886 struct nfs_setattrargs arg = {
2887 .fh = NFS_FH(inode),
2888 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 .server = server,
2890 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002891 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002892 };
2893 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002895 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002897 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002898 struct nfs4_exception exception = {
2899 .state = state,
2900 .inode = inode,
2901 .stateid = &arg.stateid,
2902 };
2903 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
David Quigleyaa9c2662013-05-22 12:50:44 -04002905 arg.bitmask = nfs4_bitmask(server, ilabel);
2906 if (ilabel)
2907 arg.bitmask = nfs4_bitmask(server, olabel);
2908
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 do {
NeilBrown29b59f92016-10-13 15:26:47 +11002910 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04002911 switch (err) {
2912 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002913 if (!(sattr->ia_valid & ATTR_SIZE)) {
2914 pr_warn_once("NFSv4: server %s is incorrectly "
2915 "applying open mode checks to "
2916 "a SETATTR that is not "
2917 "changing file size.\n",
2918 server->nfs_client->cl_hostname);
2919 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002920 if (state && !(state->state & FMODE_WRITE)) {
2921 err = -EBADF;
2922 if (sattr->ia_valid & ATTR_OPEN)
2923 err = -EACCES;
2924 goto out;
2925 }
2926 }
2927 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002929out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 return err;
2931}
2932
Peng Tao500d7012015-09-22 11:35:22 +08002933static bool
2934nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2935{
2936 if (inode == NULL || !nfs_have_layout(inode))
2937 return false;
2938
2939 return pnfs_wait_on_layoutreturn(inode, task);
2940}
2941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942struct nfs4_closedata {
2943 struct inode *inode;
2944 struct nfs4_state *state;
2945 struct nfs_closeargs arg;
2946 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05002947 struct {
2948 struct nfs4_layoutreturn_args arg;
2949 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04002950 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05002951 u32 roc_barrier;
2952 bool roc;
2953 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002954 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002955 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956};
2957
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002958static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002959{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002960 struct nfs4_closedata *calldata = data;
2961 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002962 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002963
Trond Myklebustcf805162016-11-15 14:56:07 -05002964 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05002965 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
2966 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07002967 nfs4_put_open_state(calldata->state);
2968 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002969 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002970 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002971 kfree(calldata);
2972}
2973
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002974static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002976 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002979 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Chuck Levera3ca5652012-03-01 17:00:40 -05002981 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002982 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2983 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002984 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05002985
2986 /* Handle Layoutreturn errors */
2987 if (calldata->arg.lr_args && task->tk_status != 0) {
2988 switch (calldata->res.lr_ret) {
2989 default:
2990 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
2991 break;
2992 case 0:
2993 calldata->arg.lr_args = NULL;
2994 calldata->res.lr_res = NULL;
2995 break;
2996 case -NFS4ERR_ADMIN_REVOKED:
2997 case -NFS4ERR_DELEG_REVOKED:
2998 case -NFS4ERR_EXPIRED:
2999 case -NFS4ERR_BAD_STATEID:
3000 case -NFS4ERR_OLD_STATEID:
3001 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3002 case -NFS4ERR_WRONG_CRED:
3003 calldata->arg.lr_args = NULL;
3004 calldata->res.lr_res = NULL;
3005 calldata->res.lr_ret = 0;
3006 rpc_restart_call_prepare(task);
3007 return;
3008 }
3009 }
3010
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003011 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 * the state_owner. we keep this around to process errors
3013 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 switch (task->tk_status) {
3015 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003016 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003017 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003018 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003019 case -NFS4ERR_ACCESS:
3020 if (calldata->arg.bitmask != NULL) {
3021 calldata->arg.bitmask = NULL;
3022 calldata->res.fattr = NULL;
3023 task->tk_status = 0;
3024 rpc_restart_call_prepare(task);
3025 goto out_release;
3026
3027 }
3028 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003029 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003031 case -NFS4ERR_EXPIRED:
3032 nfs4_free_revoked_stateid(server,
3033 &calldata->arg.stateid,
3034 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003035 case -NFS4ERR_OLD_STATEID:
3036 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003037 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003038 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003039 rpc_restart_call_prepare(task);
3040 goto out_release;
3041 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003042 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003043 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003045 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003046 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003047 goto out_release;
3048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003050 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3051 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003052out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003053 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003054 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003055 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056}
3057
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003058static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003060 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003061 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003062 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003063 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003064 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003065
Chuck Levera3ca5652012-03-01 17:00:40 -05003066 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003067 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003068 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003069
Trond Myklebust88069f72009-12-08 08:33:16 -05003070 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003071 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003072 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3073 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3074 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003075 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003076 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003077 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003078 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003079 if (state->n_rdonly == 0)
3080 call_close |= is_rdonly;
3081 else if (is_rdonly)
3082 calldata->arg.fmode |= FMODE_READ;
3083 if (state->n_wronly == 0)
3084 call_close |= is_wronly;
3085 else if (is_wronly)
3086 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003087 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3088 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003089 } else if (is_rdwr)
3090 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3091
Trond Myklebust5cc78612016-11-14 11:19:56 -05003092 if (!nfs4_valid_open_stateid(state) ||
3093 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003094 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003095 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003096
3097 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003098 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003099 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003100 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003101
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003102 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003103 nfs_release_seqid(calldata->arg.seqid);
3104 goto out_wait;
3105 }
3106
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003107 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003108 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003109
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003110 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003111 /* Close-to-open cache consistency revalidation */
3112 if (!nfs4_have_delegation(inode, FMODE_READ))
3113 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3114 else
3115 calldata->arg.bitmask = NULL;
3116 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003117
Trond Myklebust6ae37332015-01-30 14:21:14 -05003118 calldata->arg.share_access =
3119 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3120 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003121
Trond Myklebustd8d84982016-12-19 12:14:44 -05003122 if (calldata->res.fattr == NULL)
3123 calldata->arg.bitmask = NULL;
3124 else if (calldata->arg.bitmask == NULL)
3125 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003126 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003127 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003128 &calldata->arg.seq_args,
3129 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003130 task) != 0)
3131 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003132 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003133 return;
3134out_no_action:
3135 task->tk_action = NULL;
3136out_wait:
3137 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138}
3139
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003140static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003141 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003142 .rpc_call_done = nfs4_close_done,
3143 .rpc_release = nfs4_free_closedata,
3144};
3145
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146/*
3147 * It is possible for data to be read/written from a mem-mapped file
3148 * after the sys_close call (which hits the vfs layer as a flush).
3149 * This means that we can't safely call nfsv4 close on a file until
3150 * the inode is cleared. This in turn means that we are not good
3151 * NFSv4 citizens - we do not indicate to the server to update the file's
3152 * share state even when we are done with one of the three share
3153 * stateid's in the inode.
3154 *
3155 * NOTE: Caller must be holding the sp->so_owner semaphore!
3156 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003157int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003159 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003160 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003162 struct nfs4_state_owner *sp = state->owner;
3163 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003164 struct rpc_message msg = {
3165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3166 .rpc_cred = state->owner->so_cred,
3167 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003168 struct rpc_task_setup task_setup_data = {
3169 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003170 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003171 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003172 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003173 .flags = RPC_TASK_ASYNC,
3174 };
Trond Myklebust95121352005-10-18 14:20:12 -07003175 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003177 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3178 &task_setup_data.rpc_client, &msg);
3179
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003180 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003182 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003183 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003184 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003186 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003188 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3189 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003190 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003191 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003192 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003193 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003194 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003195 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003196 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003197 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003198 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003199 calldata->lr.roc = pnfs_roc(state->inode,
3200 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3201 if (calldata->lr.roc) {
3202 calldata->arg.lr_args = &calldata->lr.arg;
3203 calldata->res.lr_res = &calldata->lr.res;
3204 }
Al Viro643168c2011-06-22 18:20:23 -04003205 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003206
Trond Myklebust1174dd12010-12-21 10:52:24 -05003207 msg.rpc_argp = &calldata->arg;
3208 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003209 task_setup_data.callback_data = calldata;
3210 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003211 if (IS_ERR(task))
3212 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003213 status = 0;
3214 if (wait)
3215 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003216 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003217 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003218out_free_calldata:
3219 kfree(calldata);
3220out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003221 nfs4_put_open_state(state);
3222 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003223 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224}
3225
Trond Myklebust2b484292010-09-17 10:56:51 -04003226static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003227nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3228 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003231 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3232
3233 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Trond Myklebust565277f2007-10-15 18:17:53 -04003235 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003236 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003237
3238 nfs4_label_release_security(label);
3239
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003240 if (IS_ERR(state))
3241 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003242 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243}
3244
Trond Myklebust1185a552009-12-03 15:54:02 -05003245static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003246{
3247 if (ctx->state == NULL)
3248 return;
3249 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003250 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003251 else
Al Viro643168c2011-06-22 18:20:23 -04003252 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003253}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
Trond Myklebustb944dba2013-11-04 15:20:20 -05003255#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3256#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003257#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003258
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3260{
Kinglong Mee8c612822015-08-26 21:12:58 +08003261 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003262 struct nfs4_server_caps_arg args = {
3263 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003264 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003265 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 struct nfs4_server_caps_res res = {};
3267 struct rpc_message msg = {
3268 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003269 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 .rpc_resp = &res,
3271 };
3272 int status;
3273
Kinglong Mee8c612822015-08-26 21:12:58 +08003274 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3275 FATTR4_WORD0_FH_EXPIRE_TYPE |
3276 FATTR4_WORD0_LINK_SUPPORT |
3277 FATTR4_WORD0_SYMLINK_SUPPORT |
3278 FATTR4_WORD0_ACLSUPPORT;
3279 if (minorversion)
3280 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3281
Bryan Schumaker7c513052011-03-24 17:12:24 +00003282 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003284 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003285 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003286 case 0:
3287 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3288 res.attr_bitmask[2] = 0;
3289 break;
3290 case 1:
3291 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3292 break;
3293 case 2:
3294 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003297 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3298 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3299 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3300 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003301 NFS_CAP_CTIME|NFS_CAP_MTIME|
3302 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003303 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3304 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 server->caps |= NFS_CAP_ACLS;
3306 if (res.has_links != 0)
3307 server->caps |= NFS_CAP_HARDLINKS;
3308 if (res.has_symlinks != 0)
3309 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003310 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3311 server->caps |= NFS_CAP_FILEID;
3312 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3313 server->caps |= NFS_CAP_MODE;
3314 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3315 server->caps |= NFS_CAP_NLINK;
3316 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3317 server->caps |= NFS_CAP_OWNER;
3318 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3319 server->caps |= NFS_CAP_OWNER_GROUP;
3320 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3321 server->caps |= NFS_CAP_ATIME;
3322 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3323 server->caps |= NFS_CAP_CTIME;
3324 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3325 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003326#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3327 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3328 server->caps |= NFS_CAP_SECURITY_LABEL;
3329#endif
3330 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3331 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003332 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003333
Trond Myklebusta65318b2009-03-11 14:10:28 -04003334 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3335 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3336 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003337 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003338 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3339 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003341 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 return status;
3345}
3346
Trond Myklebust55a97592006-06-09 09:34:19 -04003347int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
3349 struct nfs4_exception exception = { };
3350 int err;
3351 do {
3352 err = nfs4_handle_exception(server,
3353 _nfs4_server_capabilities(server, fhandle),
3354 &exception);
3355 } while (exception.retry);
3356 return err;
3357}
3358
3359static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3360 struct nfs_fsinfo *info)
3361{
David Quigleyaa9c2662013-05-22 12:50:44 -04003362 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003364 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 };
3366 struct nfs4_lookup_res res = {
3367 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003368 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 .fh = fhandle,
3370 };
3371 struct rpc_message msg = {
3372 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3373 .rpc_argp = &args,
3374 .rpc_resp = &res,
3375 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003376
David Quigleyaa9c2662013-05-22 12:50:44 -04003377 bitmask[0] = nfs4_fattr_bitmap[0];
3378 bitmask[1] = nfs4_fattr_bitmap[1];
3379 /*
3380 * Process the label in the upcoming getfattr
3381 */
3382 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3383
Trond Myklebust0e574af2005-10-27 22:12:38 -04003384 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003385 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386}
3387
3388static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3389 struct nfs_fsinfo *info)
3390{
3391 struct nfs4_exception exception = { };
3392 int err;
3393 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003394 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003395 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003396 switch (err) {
3397 case 0:
3398 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003399 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003400 default:
3401 err = nfs4_handle_exception(server, err, &exception);
3402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003404out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 return err;
3406}
3407
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003408static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3409 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3410{
Trond Myklebustc2190662013-08-26 19:23:04 -04003411 struct rpc_auth_create_args auth_args = {
3412 .pseudoflavor = flavor,
3413 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003414 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003415
Trond Myklebustc2190662013-08-26 19:23:04 -04003416 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003417 if (IS_ERR(auth))
3418 return -EACCES;
3419 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003420}
3421
Chuck Lever9a744ba2013-03-16 15:56:02 -04003422/*
3423 * Retry pseudoroot lookup with various security flavors. We do this when:
3424 *
3425 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3426 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3427 *
3428 * Returns zero on success, or a negative NFS4ERR value, or a
3429 * negative errno value.
3430 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003431static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003432 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003434 /* Per 3530bis 15.33.5 */
3435 static const rpc_authflavor_t flav_array[] = {
3436 RPC_AUTH_GSS_KRB5P,
3437 RPC_AUTH_GSS_KRB5I,
3438 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003439 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003440 RPC_AUTH_NULL,
3441 };
3442 int status = -EPERM;
3443 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003445 if (server->auth_info.flavor_len > 0) {
3446 /* try each flavor specified by user */
3447 for (i = 0; i < server->auth_info.flavor_len; i++) {
3448 status = nfs4_lookup_root_sec(server, fhandle, info,
3449 server->auth_info.flavors[i]);
3450 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3451 continue;
3452 break;
3453 }
3454 } else {
3455 /* no flavors specified by user, try default list */
3456 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3457 status = nfs4_lookup_root_sec(server, fhandle, info,
3458 flav_array[i]);
3459 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3460 continue;
3461 break;
3462 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003463 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003464
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003465 /*
3466 * -EACCESS could mean that the user doesn't have correct permissions
3467 * to access the mount. It could also mean that we tried to mount
3468 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3469 * existing mount programs don't handle -EACCES very well so it should
3470 * be mapped to -EPERM instead.
3471 */
3472 if (status == -EACCES)
3473 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003474 return status;
3475}
3476
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003477/**
3478 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3479 * @server: initialized nfs_server handle
3480 * @fhandle: we fill in the pseudo-fs root file handle
3481 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003482 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003483 *
3484 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003485 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003486int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003487 struct nfs_fsinfo *info,
3488 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003489{
Andre Przywarac7757072015-04-23 17:17:40 +01003490 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003491
Andre Przywarac7757072015-04-23 17:17:40 +01003492 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003493 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003494
3495 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003496 status = server->nfs_client->cl_mvops->find_root_sec(server,
3497 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003498
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 if (status == 0)
3500 status = nfs4_server_capabilities(server, fhandle);
3501 if (status == 0)
3502 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003503
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003504 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505}
3506
Bryan Schumakerbae36242012-05-10 15:07:31 -04003507static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3508 struct nfs_fsinfo *info)
3509{
3510 int error;
3511 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003512 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003513
3514 error = nfs4_server_capabilities(server, mntfh);
3515 if (error < 0) {
3516 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3517 return error;
3518 }
3519
David Quigley14c43f72013-05-22 12:50:43 -04003520 label = nfs4_label_alloc(server, GFP_KERNEL);
3521 if (IS_ERR(label))
3522 return PTR_ERR(label);
3523
David Quigley1775fd32013-05-22 12:50:42 -04003524 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003525 if (error < 0) {
3526 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003527 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003528 }
3529
3530 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3531 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3532 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3533
David Quigley14c43f72013-05-22 12:50:43 -04003534err_free_label:
3535 nfs4_label_free(label);
3536
Bryan Schumakerbae36242012-05-10 15:07:31 -04003537 return error;
3538}
3539
Manoj Naik6b97fd32006-06-09 09:34:29 -04003540/*
3541 * Get locations and (maybe) other attributes of a referral.
3542 * Note that we'll actually follow the referral later when
3543 * we detect fsid mismatch in inode revalidation
3544 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003545static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3546 const struct qstr *name, struct nfs_fattr *fattr,
3547 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003548{
3549 int status = -ENOMEM;
3550 struct page *page = NULL;
3551 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003552
3553 page = alloc_page(GFP_KERNEL);
3554 if (page == NULL)
3555 goto out;
3556 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3557 if (locations == NULL)
3558 goto out;
3559
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003560 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003561 if (status != 0)
3562 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003563
3564 /*
3565 * If the fsid didn't change, this is a migration event, not a
3566 * referral. Cause us to drop into the exception handler, which
3567 * will kick off migration recovery.
3568 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003569 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003570 dprintk("%s: server did not return a different fsid for"
3571 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003572 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003573 goto out;
3574 }
Andy Adamson533eb462011-06-13 18:25:56 -04003575 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3576 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003577
Andy Adamson533eb462011-06-13 18:25:56 -04003578 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003579 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003580 memset(fhandle, 0, sizeof(struct nfs_fh));
3581out:
3582 if (page)
3583 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003584 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003585 return status;
3586}
3587
David Quigley1775fd32013-05-22 12:50:42 -04003588static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3589 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590{
3591 struct nfs4_getattr_arg args = {
3592 .fh = fhandle,
3593 .bitmask = server->attr_bitmask,
3594 };
3595 struct nfs4_getattr_res res = {
3596 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003597 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 .server = server,
3599 };
3600 struct rpc_message msg = {
3601 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3602 .rpc_argp = &args,
3603 .rpc_resp = &res,
3604 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003605
3606 args.bitmask = nfs4_bitmask(server, label);
3607
Trond Myklebust0e574af2005-10-27 22:12:38 -04003608 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003609 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610}
3611
David Quigley1775fd32013-05-22 12:50:42 -04003612static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3613 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614{
3615 struct nfs4_exception exception = { };
3616 int err;
3617 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003618 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3619 trace_nfs4_getattr(server, fhandle, fattr, err);
3620 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 &exception);
3622 } while (exception.retry);
3623 return err;
3624}
3625
3626/*
3627 * The file is not closed if it is opened due to the a request to change
3628 * the size of the file. The open call will not be needed once the
3629 * VFS layer lookup-intents are implemented.
3630 *
3631 * Close is called when the inode is destroyed.
3632 * If we haven't opened the file for O_WRONLY, we
3633 * need to in the size_change case to obtain a stateid.
3634 *
3635 * Got race?
3636 * Because OPEN is always done by name in nfsv4, it is
3637 * possible that we opened a different file by the same
3638 * name. We can recognize this race condition, but we
3639 * can't do anything about it besides returning an error.
3640 *
3641 * This will be fixed with VFS changes (lookup-intent).
3642 */
3643static int
3644nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3645 struct iattr *sattr)
3646{
David Howells2b0143b2015-03-17 22:25:59 +00003647 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003648 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003649 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003650 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 int status;
3652
Peng Tao88ac8152014-09-12 11:04:10 +08003653 if (pnfs_ld_layoutret_on_setattr(inode) &&
3654 sattr->ia_valid & ATTR_SIZE &&
3655 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003656 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003657
Trond Myklebust0e574af2005-10-27 22:12:38 -04003658 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659
Andy Adamson26699402012-05-30 16:12:24 -04003660 /* Deal with open(O_TRUNC) */
3661 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003662 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003663
3664 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003665 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003666 return 0;
3667
Trond Myklebustd5308382005-11-04 15:33:38 -05003668 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003669 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003670
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003671 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003672 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003673 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003674 }
3675
David Quigley14c43f72013-05-22 12:50:43 -04003676 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3677 if (IS_ERR(label))
3678 return PTR_ERR(label);
3679
NeilBrown29b59f92016-10-13 15:26:47 +11003680 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003681 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003682 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003683 nfs_setsecurity(inode, fattr, label);
3684 }
David Quigley14c43f72013-05-22 12:50:43 -04003685 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 return status;
3687}
3688
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003689static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3690 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003691 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003692{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003693 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003694 int status;
3695 struct nfs4_lookup_arg args = {
3696 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003697 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003698 .name = name,
3699 };
3700 struct nfs4_lookup_res res = {
3701 .server = server,
3702 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003703 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003704 .fh = fhandle,
3705 };
3706 struct rpc_message msg = {
3707 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3708 .rpc_argp = &args,
3709 .rpc_resp = &res,
3710 };
3711
David Quigleyaa9c2662013-05-22 12:50:44 -04003712 args.bitmask = nfs4_bitmask(server, label);
3713
David Howells2b3de442006-08-22 20:06:09 -04003714 nfs_fattr_init(fattr);
3715
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003717 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 dprintk("NFS reply lookup: %d\n", status);
3719 return status;
3720}
3721
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003722static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003723{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003724 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003725 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003726 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3727 fattr->nlink = 2;
3728}
3729
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003730static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003731 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003732 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003733{
3734 struct nfs4_exception exception = { };
3735 struct rpc_clnt *client = *clnt;
3736 int err;
3737 do {
David Quigley1775fd32013-05-22 12:50:42 -04003738 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003739 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003740 switch (err) {
3741 case -NFS4ERR_BADNAME:
3742 err = -ENOENT;
3743 goto out;
3744 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003745 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003746 if (err == -NFS4ERR_MOVED)
3747 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003748 goto out;
3749 case -NFS4ERR_WRONGSEC:
3750 err = -EPERM;
3751 if (client != *clnt)
3752 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003753 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003754 if (IS_ERR(client))
3755 return PTR_ERR(client);
3756
3757 exception.retry = 1;
3758 break;
3759 default:
3760 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3761 }
3762 } while (exception.retry);
3763
3764out:
3765 if (err == 0)
3766 *clnt = client;
3767 else if (client != *clnt)
3768 rpc_shutdown_client(client);
3769
3770 return err;
3771}
3772
Al Virobeffb8f2016-07-20 16:34:42 -04003773static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003774 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3775 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003777 int status;
3778 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003779
David Quigley1775fd32013-05-22 12:50:42 -04003780 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003781 if (client != NFS_CLIENT(dir)) {
3782 rpc_shutdown_client(client);
3783 nfs_fixup_secinfo_attributes(fattr);
3784 }
3785 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786}
3787
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003788struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003789nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003790 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3791{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003792 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003793 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003794
David Quigley1775fd32013-05-22 12:50:42 -04003795 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003796 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003797 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003798 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003799}
3800
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3802{
Trond Myklebust76b32992007-08-10 17:45:11 -04003803 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 struct nfs4_accessargs args = {
3805 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003806 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003808 struct nfs4_accessres res = {
3809 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003810 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 struct rpc_message msg = {
3812 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3813 .rpc_argp = &args,
3814 .rpc_resp = &res,
3815 .rpc_cred = entry->cred,
3816 };
3817 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003818 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819
3820 /*
3821 * Determine which access bits we want to ask for...
3822 */
3823 if (mode & MAY_READ)
3824 args.access |= NFS4_ACCESS_READ;
3825 if (S_ISDIR(inode->i_mode)) {
3826 if (mode & MAY_WRITE)
3827 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3828 if (mode & MAY_EXEC)
3829 args.access |= NFS4_ACCESS_LOOKUP;
3830 } else {
3831 if (mode & MAY_WRITE)
3832 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3833 if (mode & MAY_EXEC)
3834 args.access |= NFS4_ACCESS_EXECUTE;
3835 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003836
3837 res.fattr = nfs_alloc_fattr();
3838 if (res.fattr == NULL)
3839 return -ENOMEM;
3840
Bryan Schumaker7c513052011-03-24 17:12:24 +00003841 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003843 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003844 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003846 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 return status;
3848}
3849
3850static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3851{
3852 struct nfs4_exception exception = { };
3853 int err;
3854 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003855 err = _nfs4_proc_access(inode, entry);
3856 trace_nfs4_access(inode, err);
3857 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 &exception);
3859 } while (exception.retry);
3860 return err;
3861}
3862
3863/*
3864 * TODO: For the time being, we don't try to get any attributes
3865 * along with any of the zero-copy operations READ, READDIR,
3866 * READLINK, WRITE.
3867 *
3868 * In the case of the first three, we want to put the GETATTR
3869 * after the read-type operation -- this is because it is hard
3870 * to predict the length of a GETATTR response in v4, and thus
3871 * align the READ data correctly. This means that the GETATTR
3872 * may end up partially falling into the page cache, and we should
3873 * shift it into the 'tail' of the xdr_buf before processing.
3874 * To do this efficiently, we need to know the total length
3875 * of data received, which doesn't seem to be available outside
3876 * of the RPC layer.
3877 *
3878 * In the case of WRITE, we also want to put the GETATTR after
3879 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003880 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 *
3882 * Both of these changes to the XDR layer would in fact be quite
3883 * minor, but I decided to leave them for a subsequent patch.
3884 */
3885static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3886 unsigned int pgbase, unsigned int pglen)
3887{
3888 struct nfs4_readlink args = {
3889 .fh = NFS_FH(inode),
3890 .pgbase = pgbase,
3891 .pglen = pglen,
3892 .pages = &page,
3893 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003894 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 struct rpc_message msg = {
3896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3897 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003898 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 };
3900
Bryan Schumaker7c513052011-03-24 17:12:24 +00003901 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 -07003902}
3903
3904static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3905 unsigned int pgbase, unsigned int pglen)
3906{
3907 struct nfs4_exception exception = { };
3908 int err;
3909 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003910 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3911 trace_nfs4_readlink(inode, err);
3912 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 &exception);
3914 } while (exception.retry);
3915 return err;
3916}
3917
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003919 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921static int
3922nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003923 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003925 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04003926 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003927 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 int status = 0;
3930
NeilBrown532d4de2016-10-13 15:26:47 +11003931 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003932 if (IS_ERR(ctx))
3933 return PTR_ERR(ctx);
3934
David Quigleyaa9c2662013-05-22 12:50:44 -04003935 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3936
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003937 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
3938 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003939 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 if (IS_ERR(state)) {
3941 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003942 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003945 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003946 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 return status;
3948}
3949
Al Virobeffb8f2016-07-20 16:34:42 -04003950static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951{
Trond Myklebust16e42952005-10-27 22:12:44 -04003952 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003953 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003955 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003957 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003958 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003959 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003961 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3962 .rpc_argp = &args,
3963 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05003965 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04003966 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003967
Bryan Schumaker7c513052011-03-24 17:12:24 +00003968 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003969 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05003970 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 return status;
3972}
3973
Al Virobeffb8f2016-07-20 16:34:42 -04003974static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975{
3976 struct nfs4_exception exception = { };
3977 int err;
3978 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003979 err = _nfs4_proc_remove(dir, name);
3980 trace_nfs4_remove(dir, name, err);
3981 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 &exception);
3983 } while (exception.retry);
3984 return err;
3985}
3986
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003987static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003989 struct nfs_server *server = NFS_SERVER(dir);
3990 struct nfs_removeargs *args = msg->rpc_argp;
3991 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003993 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003995 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003996
3997 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998}
3999
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004000static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4001{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004002 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004003 &data->args.seq_args,
4004 &data->res.seq_res,
4005 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006}
4007
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004008static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004010 struct nfs_unlinkdata *data = task->tk_calldata;
4011 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004012
Trond Myklebust14516c32010-07-31 14:29:06 -04004013 if (!nfs4_sequence_done(task, &res->seq_res))
4014 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004015 if (nfs4_async_handle_error(task, res->server, NULL,
4016 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004017 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004018 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004019 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004020 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021}
4022
Jeff Laytond3d41522010-09-17 17:31:57 -04004023static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4024{
4025 struct nfs_server *server = NFS_SERVER(dir);
4026 struct nfs_renameargs *arg = msg->rpc_argp;
4027 struct nfs_renameres *res = msg->rpc_resp;
4028
4029 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004030 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004031 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004032}
4033
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004034static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4035{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004036 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004037 &data->args.seq_args,
4038 &data->res.seq_res,
4039 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004040}
4041
4042static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4043 struct inode *new_dir)
4044{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004045 struct nfs_renamedata *data = task->tk_calldata;
4046 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004047
4048 if (!nfs4_sequence_done(task, &res->seq_res))
4049 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004050 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004051 return 0;
4052
Trond Myklebustc733c492017-01-11 12:32:26 -05004053 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004054 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004055 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004056 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004057 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004058 return 1;
4059}
4060
Al Virobeffb8f2016-07-20 16:34:42 -04004061static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004063 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 struct nfs4_link_arg arg = {
4065 .fh = NFS_FH(inode),
4066 .dir_fh = NFS_FH(dir),
4067 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004068 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004070 struct nfs4_link_res res = {
4071 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004072 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004073 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 struct rpc_message msg = {
4075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4076 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004077 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004079 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080
Trond Myklebust136f2622010-04-16 16:22:49 -04004081 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004082 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004083 goto out;
4084
David Quigley14c43f72013-05-22 12:50:43 -04004085 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4086 if (IS_ERR(res.label)) {
4087 status = PTR_ERR(res.label);
4088 goto out;
4089 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004090 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004091
Bryan Schumaker7c513052011-03-24 17:12:24 +00004092 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004093 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004094 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004095 status = nfs_post_op_update_inode(inode, res.fattr);
4096 if (!status)
4097 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004098 }
David Quigley14c43f72013-05-22 12:50:43 -04004099
4100
4101 nfs4_label_free(res.label);
4102
Trond Myklebust136f2622010-04-16 16:22:49 -04004103out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004104 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 return status;
4106}
4107
Al Virobeffb8f2016-07-20 16:34:42 -04004108static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109{
4110 struct nfs4_exception exception = { };
4111 int err;
4112 do {
4113 err = nfs4_handle_exception(NFS_SERVER(inode),
4114 _nfs4_proc_link(inode, dir, name),
4115 &exception);
4116 } while (exception.retry);
4117 return err;
4118}
4119
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004120struct nfs4_createdata {
4121 struct rpc_message msg;
4122 struct nfs4_create_arg arg;
4123 struct nfs4_create_res res;
4124 struct nfs_fh fh;
4125 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004126 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004127};
4128
4129static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004130 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004131{
4132 struct nfs4_createdata *data;
4133
4134 data = kzalloc(sizeof(*data), GFP_KERNEL);
4135 if (data != NULL) {
4136 struct nfs_server *server = NFS_SERVER(dir);
4137
David Quigley14c43f72013-05-22 12:50:43 -04004138 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4139 if (IS_ERR(data->label))
4140 goto out_free;
4141
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004142 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4143 data->msg.rpc_argp = &data->arg;
4144 data->msg.rpc_resp = &data->res;
4145 data->arg.dir_fh = NFS_FH(dir);
4146 data->arg.server = server;
4147 data->arg.name = name;
4148 data->arg.attrs = sattr;
4149 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004150 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004151 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004152 data->res.server = server;
4153 data->res.fh = &data->fh;
4154 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004155 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004156 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004157 }
4158 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004159out_free:
4160 kfree(data);
4161 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004162}
4163
4164static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4165{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004166 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004167 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004168 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004169 update_changeattr(dir, &data->res.dir_cinfo,
4170 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004171 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004172 }
4173 return status;
4174}
4175
4176static void nfs4_free_createdata(struct nfs4_createdata *data)
4177{
David Quigley14c43f72013-05-22 12:50:43 -04004178 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004179 kfree(data);
4180}
4181
Chuck Lever4f390c12006-08-22 20:06:22 -04004182static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004183 struct page *page, unsigned int len, struct iattr *sattr,
4184 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004186 struct nfs4_createdata *data;
4187 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
Chuck Lever94a6d752006-08-22 20:06:23 -04004189 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004190 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004191
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004192 status = -ENOMEM;
4193 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4194 if (data == NULL)
4195 goto out;
4196
4197 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4198 data->arg.u.symlink.pages = &page;
4199 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004200 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004202 status = nfs4_do_create(dir, dentry, data);
4203
4204 nfs4_free_createdata(data);
4205out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 return status;
4207}
4208
Chuck Lever4f390c12006-08-22 20:06:22 -04004209static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004210 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211{
4212 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004213 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004215
4216 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4217
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004219 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4220 trace_nfs4_symlink(dir, &dentry->d_name, err);
4221 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 &exception);
4223 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004224
4225 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 return err;
4227}
4228
4229static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004230 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004232 struct nfs4_createdata *data;
4233 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004235 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4236 if (data == NULL)
4237 goto out;
4238
David Quigley1775fd32013-05-22 12:50:42 -04004239 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004240 status = nfs4_do_create(dir, dentry, data);
4241
4242 nfs4_free_createdata(data);
4243out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 return status;
4245}
4246
4247static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4248 struct iattr *sattr)
4249{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004250 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004252 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004254
David Quigleyaa9c2662013-05-22 12:50:44 -04004255 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4256
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004257 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4258 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004260 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4261 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4262 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 &exception);
4264 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004265 nfs4_label_release_security(label);
4266
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 return err;
4268}
4269
4270static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004271 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272{
David Howells2b0143b2015-03-17 22:25:59 +00004273 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 struct nfs4_readdir_arg args = {
4275 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004276 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 .pgbase = 0,
4278 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004279 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004280 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 };
4282 struct nfs4_readdir_res res;
4283 struct rpc_message msg = {
4284 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4285 .rpc_argp = &args,
4286 .rpc_resp = &res,
4287 .rpc_cred = cred,
4288 };
4289 int status;
4290
Al Viro6de14722013-09-16 10:53:17 -04004291 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4292 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004293 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004294 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004296 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 -05004297 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004298 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004299 status += args.pgbase;
4300 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004301
4302 nfs_invalidate_atime(dir);
4303
Harvey Harrison3110ff82008-05-02 13:42:44 -07004304 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 return status;
4306}
4307
4308static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004309 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310{
4311 struct nfs4_exception exception = { };
4312 int err;
4313 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004314 err = _nfs4_proc_readdir(dentry, cred, cookie,
4315 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004316 trace_nfs4_readdir(d_inode(dentry), err);
4317 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 &exception);
4319 } while (exception.retry);
4320 return err;
4321}
4322
4323static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004324 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004326 struct nfs4_createdata *data;
4327 int mode = sattr->ia_mode;
4328 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004330 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4331 if (data == NULL)
4332 goto out;
4333
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004335 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004337 data->arg.ftype = NF4BLK;
4338 data->arg.u.device.specdata1 = MAJOR(rdev);
4339 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 }
4341 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004342 data->arg.ftype = NF4CHR;
4343 data->arg.u.device.specdata1 = MAJOR(rdev);
4344 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004345 } else if (!S_ISSOCK(mode)) {
4346 status = -EINVAL;
4347 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 }
David Quigley1775fd32013-05-22 12:50:42 -04004349
David Quigleyaa9c2662013-05-22 12:50:44 -04004350 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004351 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004352out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004353 nfs4_free_createdata(data);
4354out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 return status;
4356}
4357
4358static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4359 struct iattr *sattr, dev_t rdev)
4360{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004361 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004363 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004365
David Quigleyaa9c2662013-05-22 12:50:44 -04004366 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4367
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004368 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4369 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004371 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4372 trace_nfs4_mknod(dir, &dentry->d_name, err);
4373 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 &exception);
4375 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004376
4377 nfs4_label_release_security(label);
4378
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 return err;
4380}
4381
4382static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4383 struct nfs_fsstat *fsstat)
4384{
4385 struct nfs4_statfs_arg args = {
4386 .fh = fhandle,
4387 .bitmask = server->attr_bitmask,
4388 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004389 struct nfs4_statfs_res res = {
4390 .fsstat = fsstat,
4391 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 struct rpc_message msg = {
4393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4394 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004395 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 };
4397
Trond Myklebust0e574af2005-10-27 22:12:38 -04004398 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004399 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400}
4401
4402static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4403{
4404 struct nfs4_exception exception = { };
4405 int err;
4406 do {
4407 err = nfs4_handle_exception(server,
4408 _nfs4_proc_statfs(server, fhandle, fsstat),
4409 &exception);
4410 } while (exception.retry);
4411 return err;
4412}
4413
4414static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4415 struct nfs_fsinfo *fsinfo)
4416{
4417 struct nfs4_fsinfo_arg args = {
4418 .fh = fhandle,
4419 .bitmask = server->attr_bitmask,
4420 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004421 struct nfs4_fsinfo_res res = {
4422 .fsinfo = fsinfo,
4423 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 struct rpc_message msg = {
4425 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4426 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004427 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 };
4429
Bryan Schumaker7c513052011-03-24 17:12:24 +00004430 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431}
4432
4433static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4434{
4435 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004436 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 int err;
4438
4439 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004440 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004441 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004442 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004443 nfs4_set_lease_period(server->nfs_client,
4444 fsinfo->lease_time * HZ,
4445 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004446 break;
4447 }
4448 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 } while (exception.retry);
4450 return err;
4451}
4452
4453static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4454{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004455 int error;
4456
Trond Myklebust0e574af2005-10-27 22:12:38 -04004457 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004458 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004459 if (error == 0) {
4460 /* block layout checks this! */
4461 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004462 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004463 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004464
4465 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466}
4467
4468static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4469 struct nfs_pathconf *pathconf)
4470{
4471 struct nfs4_pathconf_arg args = {
4472 .fh = fhandle,
4473 .bitmask = server->attr_bitmask,
4474 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004475 struct nfs4_pathconf_res res = {
4476 .pathconf = pathconf,
4477 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 struct rpc_message msg = {
4479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4480 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004481 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 };
4483
4484 /* None of the pathconf attributes are mandatory to implement */
4485 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4486 memset(pathconf, 0, sizeof(*pathconf));
4487 return 0;
4488 }
4489
Trond Myklebust0e574af2005-10-27 22:12:38 -04004490 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004491 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492}
4493
4494static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4495 struct nfs_pathconf *pathconf)
4496{
4497 struct nfs4_exception exception = { };
4498 int err;
4499
4500 do {
4501 err = nfs4_handle_exception(server,
4502 _nfs4_proc_pathconf(server, fhandle, pathconf),
4503 &exception);
4504 } while (exception.retry);
4505 return err;
4506}
4507
Trond Myklebust5521abf2013-03-16 20:54:34 -04004508int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004509 const struct nfs_open_context *ctx,
4510 const struct nfs_lock_context *l_ctx,
4511 fmode_t fmode)
4512{
NeilBrown17393472016-10-13 15:26:47 +11004513 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004514}
4515EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4516
Trond Myklebust5521abf2013-03-16 20:54:34 -04004517static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4518 const struct nfs_open_context *ctx,
4519 const struct nfs_lock_context *l_ctx,
4520 fmode_t fmode)
4521{
4522 nfs4_stateid current_stateid;
4523
Trond Myklebuste1253be2014-03-05 08:44:23 -05004524 /* If the current stateid represents a lost lock, then exit */
4525 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4526 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004527 return nfs4_stateid_match(stateid, &current_stateid);
4528}
4529
4530static bool nfs4_error_stateid_expired(int err)
4531{
4532 switch (err) {
4533 case -NFS4ERR_DELEG_REVOKED:
4534 case -NFS4ERR_ADMIN_REVOKED:
4535 case -NFS4ERR_BAD_STATEID:
4536 case -NFS4ERR_STALE_STATEID:
4537 case -NFS4ERR_OLD_STATEID:
4538 case -NFS4ERR_OPENMODE:
4539 case -NFS4ERR_EXPIRED:
4540 return true;
4541 }
4542 return false;
4543}
4544
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004545static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004547 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004549 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004550 if (task->tk_status < 0) {
4551 struct nfs4_exception exception = {
4552 .inode = hdr->inode,
4553 .state = hdr->args.context->state,
4554 .stateid = &hdr->args.stateid,
4555 };
4556 task->tk_status = nfs4_async_handle_exception(task,
4557 server, task->tk_status, &exception);
4558 if (exception.retry) {
4559 rpc_restart_call_prepare(task);
4560 return -EAGAIN;
4561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004563
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004565 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004566 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567}
4568
Trond Myklebust5521abf2013-03-16 20:54:34 -04004569static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004570 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004571{
4572
4573 if (!nfs4_error_stateid_expired(task->tk_status) ||
4574 nfs4_stateid_is_current(&args->stateid,
4575 args->context,
4576 args->lock_context,
4577 FMODE_READ))
4578 return false;
4579 rpc_restart_call_prepare(task);
4580 return true;
4581}
4582
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004583static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004584{
4585
4586 dprintk("--> %s\n", __func__);
4587
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004588 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004589 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004590 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004591 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004592 if (task->tk_status > 0)
4593 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004594 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4595 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004596}
4597
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004598static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4599 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004601 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004602 if (!hdr->pgio_done_cb)
4603 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004604 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004605 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606}
4607
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004608static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4609 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004610{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004611 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004612 &hdr->args.seq_args,
4613 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004614 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004615 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004616 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4617 hdr->args.lock_context,
4618 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004619 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004620 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004621 return -EIO;
4622 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623}
4624
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004625static int nfs4_write_done_cb(struct rpc_task *task,
4626 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004628 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004629
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004630 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004631 if (task->tk_status < 0) {
4632 struct nfs4_exception exception = {
4633 .inode = hdr->inode,
4634 .state = hdr->args.context->state,
4635 .stateid = &hdr->args.stateid,
4636 };
4637 task->tk_status = nfs4_async_handle_exception(task,
4638 NFS_SERVER(inode), task->tk_status,
4639 &exception);
4640 if (exception.retry) {
4641 rpc_restart_call_prepare(task);
4642 return -EAGAIN;
4643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004645 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004646 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004647 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004648 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004649 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650}
4651
Trond Myklebust5521abf2013-03-16 20:54:34 -04004652static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004653 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004654{
4655
4656 if (!nfs4_error_stateid_expired(task->tk_status) ||
4657 nfs4_stateid_is_current(&args->stateid,
4658 args->context,
4659 args->lock_context,
4660 FMODE_WRITE))
4661 return false;
4662 rpc_restart_call_prepare(task);
4663 return true;
4664}
4665
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004666static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004667{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004668 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004669 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004670 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004671 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004672 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4673 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004674}
4675
Trond Myklebust5a37f852012-04-28 14:55:16 -04004676static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004677bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004678{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004679 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004680 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004681 return false;
4682 /* Otherwise, request attributes if and only if we don't hold
4683 * a delegation
4684 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004685 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004686}
Fred Isamana69aef12011-03-03 15:13:47 +00004687
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004688static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4689 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004691 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004692
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004693 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4694 hdr->args.bitmask = NULL;
4695 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004696 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004697 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004698
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004699 if (!hdr->pgio_done_cb)
4700 hdr->pgio_done_cb = nfs4_write_done_cb;
4701 hdr->res.server = server;
4702 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004704 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004705 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706}
4707
Fred Isaman0b7c0152012-04-20 14:47:39 -04004708static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4709{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004710 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004711 &data->args.seq_args,
4712 &data->res.seq_res,
4713 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714}
4715
Fred Isaman0b7c0152012-04-20 14:47:39 -04004716static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004719
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004720 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004721 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4722 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004723 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004724 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004726 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727}
4728
Fred Isaman0b7c0152012-04-20 14:47:39 -04004729static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004730{
4731 if (!nfs4_sequence_done(task, &data->res.seq_res))
4732 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004733 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004734}
4735
Fred Isaman0b7c0152012-04-20 14:47:39 -04004736static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004738 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004739
Fred Isaman0b7c0152012-04-20 14:47:39 -04004740 if (data->commit_done_cb == NULL)
4741 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004742 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004743 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004744 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745}
4746
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004747struct nfs4_renewdata {
4748 struct nfs_client *client;
4749 unsigned long timestamp;
4750};
4751
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752/*
4753 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4754 * standalone procedure for queueing an asynchronous RENEW.
4755 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004756static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004757{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004758 struct nfs4_renewdata *data = calldata;
4759 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004760
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004761 if (atomic_read(&clp->cl_count) > 1)
4762 nfs4_schedule_state_renewal(clp);
4763 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004764 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004765}
4766
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004767static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004769 struct nfs4_renewdata *data = calldata;
4770 struct nfs_client *clp = data->client;
4771 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004773 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004774 switch (task->tk_status) {
4775 case 0:
4776 break;
4777 case -NFS4ERR_LEASE_MOVED:
4778 nfs4_schedule_lease_moved_recovery(clp);
4779 break;
4780 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004781 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004782 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4783 return;
4784 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004785 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004786 return;
4787 }
4788 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004790 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791}
4792
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004793static const struct rpc_call_ops nfs4_renew_ops = {
4794 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004795 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004796};
4797
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004798static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799{
4800 struct rpc_message msg = {
4801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4802 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004803 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004805 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004807 if (renew_flags == 0)
4808 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004809 if (!atomic_inc_not_zero(&clp->cl_count))
4810 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004811 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004812 if (data == NULL)
4813 return -ENOMEM;
4814 data->client = clp;
4815 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004816 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004817 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818}
4819
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004820static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821{
4822 struct rpc_message msg = {
4823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4824 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004825 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 };
4827 unsigned long now = jiffies;
4828 int status;
4829
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004830 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 if (status < 0)
4832 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004833 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 return 0;
4835}
4836
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004837static inline int nfs4_server_supports_acls(struct nfs_server *server)
4838{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004839 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004840}
4841
Trond Myklebust21f498c2012-08-24 10:59:25 -04004842/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4843 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004844 * the stack.
4845 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004846#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004847
Neil Hormane9e3d722011-03-04 19:26:03 -05004848static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004849 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004850{
4851 struct page *newpage, **spages;
4852 int rc = 0;
4853 size_t len;
4854 spages = pages;
4855
4856 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004857 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004858 newpage = alloc_page(GFP_KERNEL);
4859
4860 if (newpage == NULL)
4861 goto unwind;
4862 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05004863 buf += len;
4864 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05004865 *pages++ = newpage;
4866 rc++;
4867 } while (buflen != 0);
4868
4869 return rc;
4870
4871unwind:
4872 for(; rc > 0; rc--)
4873 __free_page(spages[rc-1]);
4874 return -ENOMEM;
4875}
4876
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004877struct nfs4_cached_acl {
4878 int cached;
4879 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004880 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004881};
4882
4883static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004884{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004885 struct nfs_inode *nfsi = NFS_I(inode);
4886
4887 spin_lock(&inode->i_lock);
4888 kfree(nfsi->nfs4_acl);
4889 nfsi->nfs4_acl = acl;
4890 spin_unlock(&inode->i_lock);
4891}
4892
4893static void nfs4_zap_acl_attr(struct inode *inode)
4894{
4895 nfs4_set_cached_acl(inode, NULL);
4896}
4897
4898static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4899{
4900 struct nfs_inode *nfsi = NFS_I(inode);
4901 struct nfs4_cached_acl *acl;
4902 int ret = -ENOENT;
4903
4904 spin_lock(&inode->i_lock);
4905 acl = nfsi->nfs4_acl;
4906 if (acl == NULL)
4907 goto out;
4908 if (buf == NULL) /* user is just asking for length */
4909 goto out_len;
4910 if (acl->cached == 0)
4911 goto out;
4912 ret = -ERANGE; /* see getxattr(2) man page */
4913 if (acl->len > buflen)
4914 goto out;
4915 memcpy(buf, acl->data, acl->len);
4916out_len:
4917 ret = acl->len;
4918out:
4919 spin_unlock(&inode->i_lock);
4920 return ret;
4921}
4922
Sachin Prabhu5794d212012-04-17 14:36:40 +01004923static 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 +00004924{
4925 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004926 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004927
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004928 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004929 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004930 if (acl == NULL)
4931 goto out;
4932 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004933 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004934 } else {
4935 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4936 if (acl == NULL)
4937 goto out;
4938 acl->cached = 0;
4939 }
4940 acl->len = acl_len;
4941out:
4942 nfs4_set_cached_acl(inode, acl);
4943}
4944
Andy Adamsonbf118a32011-12-07 11:55:27 -05004945/*
4946 * The getxattr API returns the required buffer length when called with a
4947 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4948 * the required buf. On a NULL buf, we send a page of data to the server
4949 * guessing that the ACL request can be serviced by a page. If so, we cache
4950 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4951 * the cache. If not so, we throw away the page, and cache the required
4952 * length. The next getxattr call will then produce another round trip to
4953 * the server, this time with the input buf of the required size.
4954 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004955static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004956{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05004957 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004958 struct nfs_getaclargs args = {
4959 .fh = NFS_FH(inode),
4960 .acl_pages = pages,
4961 .acl_len = buflen,
4962 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004963 struct nfs_getaclres res = {
4964 .acl_len = buflen,
4965 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004966 struct rpc_message msg = {
4967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4968 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004969 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004970 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05004971 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004972 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004973
Trond Myklebust21f498c2012-08-24 10:59:25 -04004974 if (npages > ARRAY_SIZE(pages))
4975 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004976
Andy Adamsonbf118a32011-12-07 11:55:27 -05004977 for (i = 0; i < npages; i++) {
4978 pages[i] = alloc_page(GFP_KERNEL);
4979 if (!pages[i])
4980 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004981 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004982
4983 /* for decoding across pages */
4984 res.acl_scratch = alloc_page(GFP_KERNEL);
4985 if (!res.acl_scratch)
4986 goto out_free;
4987
Andy Adamsonbf118a32011-12-07 11:55:27 -05004988 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004989
Peng Taode040be2012-01-10 22:42:47 +08004990 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004991 __func__, buf, buflen, npages, args.acl_len);
4992 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4993 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004994 if (ret)
4995 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004996
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004997 /* Handle the case where the passed-in buffer is too short */
4998 if (res.acl_flags & NFS4_ACL_TRUNC) {
4999 /* Did the user only issue a request for the acl length? */
5000 if (buf == NULL)
5001 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005002 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005003 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005004 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005005 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005006 if (buf) {
5007 if (res.acl_len > buflen) {
5008 ret = -ERANGE;
5009 goto out_free;
5010 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005011 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005012 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005013out_ok:
5014 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005015out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005016 for (i = 0; i < npages; i++)
5017 if (pages[i])
5018 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005019 if (res.acl_scratch)
5020 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005021 return ret;
5022}
5023
Trond Myklebust16b42892006-08-24 12:27:15 -04005024static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5025{
5026 struct nfs4_exception exception = { };
5027 ssize_t ret;
5028 do {
5029 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005030 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005031 if (ret >= 0)
5032 break;
5033 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5034 } while (exception.retry);
5035 return ret;
5036}
5037
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005038static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5039{
5040 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005041 int ret;
5042
5043 if (!nfs4_server_supports_acls(server))
5044 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005045 ret = nfs_revalidate_inode(server, inode);
5046 if (ret < 0)
5047 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005048 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5049 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005050 ret = nfs4_read_cached_acl(inode, buf, buflen);
5051 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005052 /* -ENOENT is returned if there is no ACL or if there is an ACL
5053 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005054 return ret;
5055 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005056}
5057
Trond Myklebust16b42892006-08-24 12:27:15 -04005058static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005059{
5060 struct nfs_server *server = NFS_SERVER(inode);
5061 struct page *pages[NFS4ACL_MAXPAGES];
5062 struct nfs_setaclargs arg = {
5063 .fh = NFS_FH(inode),
5064 .acl_pages = pages,
5065 .acl_len = buflen,
5066 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005067 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005068 struct rpc_message msg = {
5069 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5070 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005071 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005072 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005073 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005074 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005075
5076 if (!nfs4_server_supports_acls(server))
5077 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005078 if (npages > ARRAY_SIZE(pages))
5079 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005080 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005081 if (i < 0)
5082 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005083 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005084 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005085
5086 /*
5087 * Free each page after tx, so the only ref left is
5088 * held by the network stack
5089 */
5090 for (; i > 0; i--)
5091 put_page(pages[i-1]);
5092
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005093 /*
5094 * Acl update can result in inode attribute update.
5095 * so mark the attribute cache invalid.
5096 */
5097 spin_lock(&inode->i_lock);
5098 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5099 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005100 nfs_access_zap_cache(inode);
5101 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005102 return ret;
5103}
5104
Trond Myklebust16b42892006-08-24 12:27:15 -04005105static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5106{
5107 struct nfs4_exception exception = { };
5108 int err;
5109 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005110 err = __nfs4_proc_set_acl(inode, buf, buflen);
5111 trace_nfs4_set_acl(inode, err);
5112 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005113 &exception);
5114 } while (exception.retry);
5115 return err;
5116}
5117
David Quigleyaa9c2662013-05-22 12:50:44 -04005118#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5119static int _nfs4_get_security_label(struct inode *inode, void *buf,
5120 size_t buflen)
5121{
5122 struct nfs_server *server = NFS_SERVER(inode);
5123 struct nfs_fattr fattr;
5124 struct nfs4_label label = {0, 0, buflen, buf};
5125
5126 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005127 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005128 .fh = NFS_FH(inode),
5129 .bitmask = bitmask,
5130 };
5131 struct nfs4_getattr_res res = {
5132 .fattr = &fattr,
5133 .label = &label,
5134 .server = server,
5135 };
5136 struct rpc_message msg = {
5137 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005138 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005139 .rpc_resp = &res,
5140 };
5141 int ret;
5142
5143 nfs_fattr_init(&fattr);
5144
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005145 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005146 if (ret)
5147 return ret;
5148 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5149 return -ENOENT;
5150 if (buflen < label.len)
5151 return -ERANGE;
5152 return 0;
5153}
5154
5155static int nfs4_get_security_label(struct inode *inode, void *buf,
5156 size_t buflen)
5157{
5158 struct nfs4_exception exception = { };
5159 int err;
5160
5161 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5162 return -EOPNOTSUPP;
5163
5164 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005165 err = _nfs4_get_security_label(inode, buf, buflen);
5166 trace_nfs4_get_security_label(inode, err);
5167 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005168 &exception);
5169 } while (exception.retry);
5170 return err;
5171}
5172
5173static int _nfs4_do_set_security_label(struct inode *inode,
5174 struct nfs4_label *ilabel,
5175 struct nfs_fattr *fattr,
5176 struct nfs4_label *olabel)
5177{
5178
5179 struct iattr sattr = {0};
5180 struct nfs_server *server = NFS_SERVER(inode);
5181 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005182 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005183 .fh = NFS_FH(inode),
5184 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005185 .server = server,
5186 .bitmask = bitmask,
5187 .label = ilabel,
5188 };
5189 struct nfs_setattrres res = {
5190 .fattr = fattr,
5191 .label = olabel,
5192 .server = server,
5193 };
5194 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5196 .rpc_argp = &arg,
5197 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005198 };
5199 int status;
5200
Jeff Layton12207f62013-11-01 10:49:32 -04005201 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005202
Jeff Layton12207f62013-11-01 10:49:32 -04005203 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005204 if (status)
5205 dprintk("%s failed: %d\n", __func__, status);
5206
5207 return status;
5208}
5209
5210static int nfs4_do_set_security_label(struct inode *inode,
5211 struct nfs4_label *ilabel,
5212 struct nfs_fattr *fattr,
5213 struct nfs4_label *olabel)
5214{
5215 struct nfs4_exception exception = { };
5216 int err;
5217
5218 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005219 err = _nfs4_do_set_security_label(inode, ilabel,
5220 fattr, olabel);
5221 trace_nfs4_set_security_label(inode, err);
5222 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005223 &exception);
5224 } while (exception.retry);
5225 return err;
5226}
5227
5228static int
Al Viro59301222016-05-27 10:19:30 -04005229nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005230{
5231 struct nfs4_label ilabel, *olabel = NULL;
5232 struct nfs_fattr fattr;
5233 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005234 int status;
5235
5236 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5237 return -EOPNOTSUPP;
5238
5239 nfs_fattr_init(&fattr);
5240
5241 ilabel.pi = 0;
5242 ilabel.lfs = 0;
5243 ilabel.label = (char *)buf;
5244 ilabel.len = buflen;
5245
5246 cred = rpc_lookup_cred();
5247 if (IS_ERR(cred))
5248 return PTR_ERR(cred);
5249
5250 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5251 if (IS_ERR(olabel)) {
5252 status = -PTR_ERR(olabel);
5253 goto out;
5254 }
5255
5256 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5257 if (status == 0)
5258 nfs_setsecurity(inode, &fattr, olabel);
5259
5260 nfs4_label_free(olabel);
5261out:
5262 put_rpccred(cred);
5263 return status;
5264}
5265#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5266
5267
Chuck Leverf0920752012-05-21 22:45:41 -04005268static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5269 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005270{
5271 __be32 verf[2];
5272
Chuck Lever2c820d92012-05-21 22:45:33 -04005273 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5274 /* An impossible timestamp guarantees this value
5275 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005276 verf[0] = cpu_to_be32(U32_MAX);
5277 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005278 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005279 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005280 u64 ns = ktime_to_ns(nn->boot_time);
5281
5282 verf[0] = cpu_to_be32(ns >> 32);
5283 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005284 }
Chuck Levercd937102012-03-02 17:14:31 -05005285 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5286}
5287
Jeff Laytona3192682015-06-09 19:43:59 -04005288static int
5289nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005290{
Jeff Laytona3192682015-06-09 19:43:59 -04005291 size_t len;
5292 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005293
Trond Myklebustceb3a162015-01-03 15:16:04 -05005294 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005295 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005296
Jeff Laytona3192682015-06-09 19:43:59 -04005297 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005298 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005299 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5300 1 +
5301 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5302 1;
5303 rcu_read_unlock();
5304
5305 if (len > NFS4_OPAQUE_LIMIT + 1)
5306 return -EINVAL;
5307
5308 /*
5309 * Since this string is allocated at mount time, and held until the
5310 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5311 * about a memory-reclaim deadlock.
5312 */
5313 str = kmalloc(len, GFP_KERNEL);
5314 if (!str)
5315 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005316
Chuck Levere984a552012-09-14 17:24:21 -04005317 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005318 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005319 clp->cl_ipaddr,
5320 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5321 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005322 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005323
Jeff Laytona3192682015-06-09 19:43:59 -04005324 clp->cl_owner_id = str;
5325 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005326}
5327
Jeff Layton873e3852015-06-09 19:44:00 -04005328static int
5329nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005330{
Jeff Layton873e3852015-06-09 19:44:00 -04005331 size_t len;
5332 char *str;
5333
5334 len = 10 + 10 + 1 + 10 + 1 +
5335 strlen(nfs4_client_id_uniquifier) + 1 +
5336 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5337
5338 if (len > NFS4_OPAQUE_LIMIT + 1)
5339 return -EINVAL;
5340
5341 /*
5342 * Since this string is allocated at mount time, and held until the
5343 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5344 * about a memory-reclaim deadlock.
5345 */
5346 str = kmalloc(len, GFP_KERNEL);
5347 if (!str)
5348 return -ENOMEM;
5349
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005350 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005351 clp->rpc_ops->version, clp->cl_minorversion,
5352 nfs4_client_id_uniquifier,
5353 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005354 clp->cl_owner_id = str;
5355 return 0;
5356}
5357
5358static int
5359nfs4_init_uniform_client_string(struct nfs_client *clp)
5360{
Jeff Layton873e3852015-06-09 19:44:00 -04005361 size_t len;
5362 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005363
5364 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005365 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005366
5367 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005368 return nfs4_init_uniquifier_client_string(clp);
5369
5370 len = 10 + 10 + 1 + 10 + 1 +
5371 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5372
5373 if (len > NFS4_OPAQUE_LIMIT + 1)
5374 return -EINVAL;
5375
5376 /*
5377 * Since this string is allocated at mount time, and held until the
5378 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5379 * about a memory-reclaim deadlock.
5380 */
5381 str = kmalloc(len, GFP_KERNEL);
5382 if (!str)
5383 return -ENOMEM;
5384
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005385 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005386 clp->rpc_ops->version, clp->cl_minorversion,
5387 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005388 clp->cl_owner_id = str;
5389 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005390}
5391
Chuck Lever706cb8d2014-03-12 12:51:47 -04005392/*
5393 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5394 * services. Advertise one based on the address family of the
5395 * clientaddr.
5396 */
5397static unsigned int
5398nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5399{
5400 if (strchr(clp->cl_ipaddr, ':') != NULL)
5401 return scnprintf(buf, len, "tcp6");
5402 else
5403 return scnprintf(buf, len, "tcp");
5404}
5405
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005406static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5407{
5408 struct nfs4_setclientid *sc = calldata;
5409
5410 if (task->tk_status == 0)
5411 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5412}
5413
5414static const struct rpc_call_ops nfs4_setclientid_ops = {
5415 .rpc_call_done = nfs4_setclientid_done,
5416};
5417
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005418/**
5419 * nfs4_proc_setclientid - Negotiate client ID
5420 * @clp: state data structure
5421 * @program: RPC program for NFSv4 callback service
5422 * @port: IP port number for NFS4 callback service
5423 * @cred: RPC credential to use for this call
5424 * @res: where to place the result
5425 *
5426 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5427 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005428int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5429 unsigned short port, struct rpc_cred *cred,
5430 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431{
5432 nfs4_verifier sc_verifier;
5433 struct nfs4_setclientid setclientid = {
5434 .sc_verifier = &sc_verifier,
5435 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005436 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 };
5438 struct rpc_message msg = {
5439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5440 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005441 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005442 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005444 struct rpc_task *task;
5445 struct rpc_task_setup task_setup_data = {
5446 .rpc_client = clp->cl_rpcclient,
5447 .rpc_message = &msg,
5448 .callback_ops = &nfs4_setclientid_ops,
5449 .callback_data = &setclientid,
5450 .flags = RPC_TASK_TIMEOUT,
5451 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005452 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453
Chuck Leverde734832012-07-11 16:30:50 -04005454 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005455 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005456
5457 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5458 status = nfs4_init_uniform_client_string(clp);
5459 else
Jeff Laytona3192682015-06-09 19:43:59 -04005460 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005461
5462 if (status)
5463 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005464
Chuck Leverde734832012-07-11 16:30:50 -04005465 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005466 setclientid.sc_netid_len =
5467 nfs4_init_callback_netid(clp,
5468 setclientid.sc_netid,
5469 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005470 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005471 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 clp->cl_ipaddr, port >> 8, port & 255);
5473
Jeff Layton3a6bb732015-06-09 19:43:57 -04005474 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005475 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005476 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005477 task = rpc_run_task(&task_setup_data);
5478 if (IS_ERR(task)) {
5479 status = PTR_ERR(task);
5480 goto out;
5481 }
5482 status = task->tk_status;
5483 if (setclientid.sc_cred) {
5484 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5485 put_rpccred(setclientid.sc_cred);
5486 }
5487 rpc_put_task(task);
5488out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005489 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005490 dprintk("NFS reply setclientid: %d\n", status);
5491 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492}
5493
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005494/**
5495 * nfs4_proc_setclientid_confirm - Confirm client ID
5496 * @clp: state data structure
5497 * @res: result of a previous SETCLIENTID
5498 * @cred: RPC credential to use for this call
5499 *
5500 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5501 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005502int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005503 struct nfs4_setclientid_res *arg,
5504 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 struct rpc_message msg = {
5507 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005508 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005509 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 int status;
5512
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005513 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5514 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5515 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005516 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005517 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005518 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 return status;
5520}
5521
Trond Myklebustfe650402006-01-03 09:55:18 +01005522struct nfs4_delegreturndata {
5523 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005524 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005525 struct nfs_fh fh;
5526 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005527 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005528 struct {
5529 struct nfs4_layoutreturn_args arg;
5530 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005531 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005532 u32 roc_barrier;
5533 bool roc;
5534 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005535 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005536 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005537 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005538};
5539
Trond Myklebustfe650402006-01-03 09:55:18 +01005540static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5541{
5542 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005543
Trond Myklebust14516c32010-07-31 14:29:06 -04005544 if (!nfs4_sequence_done(task, &data->res.seq_res))
5545 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005546
Trond Myklebustca8acf82013-08-13 10:36:56 -04005547 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005548
5549 /* Handle Layoutreturn errors */
5550 if (data->args.lr_args && task->tk_status != 0) {
5551 switch(data->res.lr_ret) {
5552 default:
5553 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5554 break;
5555 case 0:
5556 data->args.lr_args = NULL;
5557 data->res.lr_res = NULL;
5558 break;
5559 case -NFS4ERR_ADMIN_REVOKED:
5560 case -NFS4ERR_DELEG_REVOKED:
5561 case -NFS4ERR_EXPIRED:
5562 case -NFS4ERR_BAD_STATEID:
5563 case -NFS4ERR_OLD_STATEID:
5564 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5565 case -NFS4ERR_WRONG_CRED:
5566 data->args.lr_args = NULL;
5567 data->res.lr_res = NULL;
5568 data->res.lr_ret = 0;
5569 rpc_restart_call_prepare(task);
5570 return;
5571 }
5572 }
5573
Ricardo Labiaga79708862009-12-07 09:23:21 -05005574 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005575 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005576 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005577 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005578 case -NFS4ERR_ADMIN_REVOKED:
5579 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005580 case -NFS4ERR_EXPIRED:
5581 nfs4_free_revoked_stateid(data->res.server,
5582 data->args.stateid,
5583 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005584 case -NFS4ERR_BAD_STATEID:
5585 case -NFS4ERR_OLD_STATEID:
5586 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005587 task->tk_status = 0;
5588 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005589 case -NFS4ERR_ACCESS:
5590 if (data->args.bitmask) {
5591 data->args.bitmask = NULL;
5592 data->res.fattr = NULL;
5593 task->tk_status = 0;
5594 rpc_restart_call_prepare(task);
5595 return;
5596 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005597 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005598 if (nfs4_async_handle_error(task, data->res.server,
5599 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005600 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005601 return;
5602 }
5603 }
5604 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005605}
5606
5607static void nfs4_delegreturn_release(void *calldata)
5608{
Peng Tao039b7562014-07-03 13:05:02 +08005609 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005610 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005611
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005612 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005613 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005614 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5615 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005616 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005617 nfs_iput_and_deactive(inode);
5618 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005619 kfree(calldata);
5620}
5621
Andy Adamson938e1012009-04-01 09:22:28 -04005622static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5623{
5624 struct nfs4_delegreturndata *d_data;
5625
5626 d_data = (struct nfs4_delegreturndata *)data;
5627
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005628 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005629 return;
5630
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005631 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005632 &d_data->args.seq_args,
5633 &d_data->res.seq_res,
5634 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005635}
Andy Adamson938e1012009-04-01 09:22:28 -04005636
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005637static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005638 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005639 .rpc_call_done = nfs4_delegreturn_done,
5640 .rpc_release = nfs4_delegreturn_release,
5641};
5642
Trond Myklebuste6f81072008-01-24 18:14:34 -05005643static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005644{
5645 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005646 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005647 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005648 struct rpc_message msg = {
5649 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5650 .rpc_cred = cred,
5651 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005652 struct rpc_task_setup task_setup_data = {
5653 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005654 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005655 .callback_ops = &nfs4_delegreturn_ops,
5656 .flags = RPC_TASK_ASYNC,
5657 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005658 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005659
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005660 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005661 if (data == NULL)
5662 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005663 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005664
5665 nfs4_state_protect(server->nfs_client,
5666 NFS_SP4_MACH_CRED_CLEANUP,
5667 &task_setup_data.rpc_client, &msg);
5668
Trond Myklebustfe650402006-01-03 09:55:18 +01005669 data->args.fhandle = &data->fh;
5670 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005671 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005672 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005673 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005674 data->res.fattr = &data->fattr;
5675 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005676 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005677 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005678 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005679 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005680 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005681 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005682 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005683 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005684 if (data->lr.roc) {
5685 data->args.lr_args = &data->lr.arg;
5686 data->res.lr_res = &data->lr.res;
5687 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005688 } else if (data->lr.roc) {
5689 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5690 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005691 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005692
Trond Myklebustc970aa82007-07-14 15:39:59 -04005693 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005694 msg.rpc_argp = &data->args;
5695 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005696 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005697 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005698 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005699 if (!issync)
5700 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005701 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005702 if (status != 0)
5703 goto out;
5704 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005705out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005706 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005707 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708}
5709
Trond Myklebuste6f81072008-01-24 18:14:34 -05005710int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711{
5712 struct nfs_server *server = NFS_SERVER(inode);
5713 struct nfs4_exception exception = { };
5714 int err;
5715 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005716 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005717 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 switch (err) {
5719 case -NFS4ERR_STALE_STATEID:
5720 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 case 0:
5722 return 0;
5723 }
5724 err = nfs4_handle_exception(server, err, &exception);
5725 } while (exception.retry);
5726 return err;
5727}
5728
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5730{
5731 struct inode *inode = state->inode;
5732 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005733 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005734 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005736 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005738 struct nfs_lockt_res res = {
5739 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 };
5741 struct rpc_message msg = {
5742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005743 .rpc_argp = &arg,
5744 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 .rpc_cred = state->owner->so_cred,
5746 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 struct nfs4_lock_state *lsp;
5748 int status;
5749
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005750 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005751 status = nfs4_set_lock_state(state, request);
5752 if (status != 0)
5753 goto out;
5754 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005755 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005756 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005757 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005758 switch (status) {
5759 case 0:
5760 request->fl_type = F_UNLCK;
5761 break;
5762 case -NFS4ERR_DENIED:
5763 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005765 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005766 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005767out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 return status;
5769}
5770
5771static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5772{
5773 struct nfs4_exception exception = { };
5774 int err;
5775
5776 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005777 err = _nfs4_proc_getlk(state, cmd, request);
5778 trace_nfs4_get_lock(request, state, cmd, err);
5779 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 &exception);
5781 } while (exception.retry);
5782 return err;
5783}
5784
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005785struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005786 struct nfs_locku_args arg;
5787 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005788 struct nfs4_lock_state *lsp;
5789 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005790 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005791 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005792 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005793};
5794
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005795static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5796 struct nfs_open_context *ctx,
5797 struct nfs4_lock_state *lsp,
5798 struct nfs_seqid *seqid)
5799{
5800 struct nfs4_unlockdata *p;
5801 struct inode *inode = lsp->ls_state->inode;
5802
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005803 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005804 if (p == NULL)
5805 return NULL;
5806 p->arg.fh = NFS_FH(inode);
5807 p->arg.fl = &p->fl;
5808 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005809 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005810 p->lsp = lsp;
5811 atomic_inc(&lsp->ls_count);
5812 /* Ensure we don't close file until we're done freeing locks! */
5813 p->ctx = get_nfs_open_context(ctx);
5814 memcpy(&p->fl, fl, sizeof(p->fl));
5815 p->server = NFS_SERVER(inode);
5816 return p;
5817}
5818
Trond Myklebust06f814a2006-01-03 09:55:07 +01005819static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005820{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005821 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005822 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005823 nfs4_put_lock_state(calldata->lsp);
5824 put_nfs_open_context(calldata->ctx);
5825 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005826}
5827
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005828static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005829{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005830 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005831
Trond Myklebust14516c32010-07-31 14:29:06 -04005832 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5833 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005834 switch (task->tk_status) {
5835 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005836 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005837 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005838 if (nfs4_update_lock_stateid(calldata->lsp,
5839 &calldata->res.stateid))
5840 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005841 case -NFS4ERR_ADMIN_REVOKED:
5842 case -NFS4ERR_EXPIRED:
5843 nfs4_free_revoked_stateid(calldata->server,
5844 &calldata->arg.stateid,
5845 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005846 case -NFS4ERR_BAD_STATEID:
5847 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005848 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005849 if (!nfs4_stateid_match(&calldata->arg.stateid,
5850 &calldata->lsp->ls_stateid))
5851 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005852 break;
5853 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005854 if (nfs4_async_handle_error(task, calldata->server,
5855 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005856 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005857 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005858 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005859}
5860
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005861static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005862{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005863 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005865 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005866 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005867 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005868 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005869 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005870 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005871 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005872 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005873 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04005874 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005875 &calldata->res.seq_res,
5876 task) != 0)
5877 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005878 return;
5879out_no_action:
5880 task->tk_action = NULL;
5881out_wait:
5882 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883}
5884
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005885static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005886 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005887 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005888 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005889};
5890
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005891static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5892 struct nfs_open_context *ctx,
5893 struct nfs4_lock_state *lsp,
5894 struct nfs_seqid *seqid)
5895{
5896 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005897 struct rpc_message msg = {
5898 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5899 .rpc_cred = ctx->cred,
5900 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005901 struct rpc_task_setup task_setup_data = {
5902 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005903 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005904 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005905 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005906 .flags = RPC_TASK_ASYNC,
5907 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005908
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005909 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5910 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5911
Frank Filz137d6ac2007-07-09 15:32:29 -07005912 /* Ensure this is an unlock - when canceling a lock, the
5913 * canceled lock is passed in, and it won't be an unlock.
5914 */
5915 fl->fl_type = F_UNLCK;
5916
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005917 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5918 if (data == NULL) {
5919 nfs_free_seqid(seqid);
5920 return ERR_PTR(-ENOMEM);
5921 }
5922
Chuck Levera9c92d62013-08-09 12:48:18 -04005923 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005924 msg.rpc_argp = &data->arg;
5925 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005926 task_setup_data.callback_data = data;
5927 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005928}
5929
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5931{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005932 struct inode *inode = state->inode;
5933 struct nfs4_state_owner *sp = state->owner;
5934 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005935 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005936 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005937 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005938 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005939 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005940 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941
Trond Myklebust9b073572006-06-29 16:38:34 -04005942 status = nfs4_set_lock_state(state, request);
5943 /* Unlock _before_ we do the RPC call */
5944 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005945 /* Exclude nfs_delegation_claim_locks() */
5946 mutex_lock(&sp->so_delegreturn_mutex);
5947 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005948 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005949 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005950 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005951 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005952 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005953 }
5954 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005955 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005956 if (status != 0)
5957 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005958 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005959 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005960 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5961 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005962 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5963 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005964 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005965 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005966 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005967 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005968 status = PTR_ERR(task);
5969 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005970 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005971 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005972 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005973out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005974 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005975 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005976 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977}
5978
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005979struct nfs4_lockdata {
5980 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005981 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005982 struct nfs4_lock_state *lsp;
5983 struct nfs_open_context *ctx;
5984 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005985 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005986 int rpc_status;
5987 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005988 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005989};
5990
5991static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005992 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5993 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005994{
5995 struct nfs4_lockdata *p;
5996 struct inode *inode = lsp->ls_state->inode;
5997 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005998 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005999
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006000 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006001 if (p == NULL)
6002 return NULL;
6003
6004 p->arg.fh = NFS_FH(inode);
6005 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006006 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006007 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006008 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006009 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6010 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006011 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006012 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006013 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006014 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006015 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006016 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006017 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006018 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006019 atomic_inc(&lsp->ls_count);
6020 p->ctx = get_nfs_open_context(ctx);
6021 memcpy(&p->fl, fl, sizeof(p->fl));
6022 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006023out_free_seqid:
6024 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006025out_free:
6026 kfree(p);
6027 return NULL;
6028}
6029
6030static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6031{
6032 struct nfs4_lockdata *data = calldata;
6033 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034
Harvey Harrison3110ff82008-05-02 13:42:44 -07006035 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006036 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006037 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006038 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006039 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006040 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006041 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006042 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006043 nfs4_stateid_copy(&data->arg.open_stateid,
6044 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006045 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006046 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006047 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006048 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006049 nfs4_stateid_copy(&data->arg.lock_stateid,
6050 &data->lsp->ls_stateid);
6051 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006052 if (!nfs4_valid_open_stateid(state)) {
6053 data->rpc_status = -EBADF;
6054 task->tk_action = NULL;
6055 goto out_release_open_seqid;
6056 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006057 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006058 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006059 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006060 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006061 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006062 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006063out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006064 nfs_release_seqid(data->arg.open_seqid);
6065out_release_lock_seqid:
6066 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006067out_wait:
6068 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006069 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006070}
6071
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006072static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6073{
6074 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006075 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006076
Harvey Harrison3110ff82008-05-02 13:42:44 -07006077 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006078
Trond Myklebust14516c32010-07-31 14:29:06 -04006079 if (!nfs4_sequence_done(task, &data->res.seq_res))
6080 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006081
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006082 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006083 switch (task->tk_status) {
6084 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006085 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006086 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006087 if (data->arg.new_lock) {
6088 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006089 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006090 rpc_restart_call_prepare(task);
6091 break;
6092 }
6093 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006094 if (data->arg.new_lock_owner != 0) {
6095 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6096 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6097 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6098 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6099 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006100 break;
6101 case -NFS4ERR_BAD_STATEID:
6102 case -NFS4ERR_OLD_STATEID:
6103 case -NFS4ERR_STALE_STATEID:
6104 case -NFS4ERR_EXPIRED:
6105 if (data->arg.new_lock_owner != 0) {
6106 if (!nfs4_stateid_match(&data->arg.open_stateid,
6107 &lsp->ls_state->open_stateid))
6108 rpc_restart_call_prepare(task);
6109 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6110 &lsp->ls_stateid))
6111 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006112 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006113 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006114}
6115
6116static void nfs4_lock_release(void *calldata)
6117{
6118 struct nfs4_lockdata *data = calldata;
6119
Harvey Harrison3110ff82008-05-02 13:42:44 -07006120 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006121 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006122 if (data->cancelled != 0) {
6123 struct rpc_task *task;
6124 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6125 data->arg.lock_seqid);
6126 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006127 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006128 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006129 } else
6130 nfs_free_seqid(data->arg.lock_seqid);
6131 nfs4_put_lock_state(data->lsp);
6132 put_nfs_open_context(data->ctx);
6133 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006134 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006135}
6136
6137static const struct rpc_call_ops nfs4_lock_ops = {
6138 .rpc_call_prepare = nfs4_lock_prepare,
6139 .rpc_call_done = nfs4_lock_done,
6140 .rpc_release = nfs4_lock_release,
6141};
6142
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006143static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6144{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006145 switch (error) {
6146 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006147 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006148 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006149 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006150 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006151 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006152 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006153 break;
6154 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006155 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006156 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006157 };
6158}
6159
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006160static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006161{
6162 struct nfs4_lockdata *data;
6163 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006164 struct rpc_message msg = {
6165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6166 .rpc_cred = state->owner->so_cred,
6167 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006168 struct rpc_task_setup task_setup_data = {
6169 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006170 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006171 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006172 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006173 .flags = RPC_TASK_ASYNC,
6174 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006175 int ret;
6176
Harvey Harrison3110ff82008-05-02 13:42:44 -07006177 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006178 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006179 fl->fl_u.nfs4_fl.owner,
6180 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006181 if (data == NULL)
6182 return -ENOMEM;
6183 if (IS_SETLKW(cmd))
6184 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006185 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006186 msg.rpc_argp = &data->arg;
6187 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006188 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006189 if (recovery_type > NFS_LOCK_NEW) {
6190 if (recovery_type == NFS_LOCK_RECLAIM)
6191 data->arg.reclaim = NFS_LOCK_RECLAIM;
6192 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006193 } else
6194 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006195 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006196 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006197 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006198 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006199 if (ret == 0) {
6200 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006201 if (ret)
6202 nfs4_handle_setlk_error(data->server, data->lsp,
6203 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006204 } else
6205 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006206 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006207 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006208 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006209 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210}
6211
6212static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6213{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006214 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006215 struct nfs4_exception exception = {
6216 .inode = state->inode,
6217 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006218 int err;
6219
6220 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006221 /* Cache the lock if possible... */
6222 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6223 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006224 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006225 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006226 break;
6227 nfs4_handle_exception(server, err, &exception);
6228 } while (exception.retry);
6229 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230}
6231
6232static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6233{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006234 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006235 struct nfs4_exception exception = {
6236 .inode = state->inode,
6237 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006238 int err;
6239
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006240 err = nfs4_set_lock_state(state, request);
6241 if (err != 0)
6242 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006243 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006244 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6245 return 0;
6246 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006247 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006248 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6249 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006250 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006251 switch (err) {
6252 default:
6253 goto out;
6254 case -NFS4ERR_GRACE:
6255 case -NFS4ERR_DELAY:
6256 nfs4_handle_exception(server, err, &exception);
6257 err = 0;
6258 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006259 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006260out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006261 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262}
6263
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006264#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006265static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6266{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006267 struct nfs4_lock_state *lsp;
6268 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006269
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006270 status = nfs4_set_lock_state(state, request);
6271 if (status != 0)
6272 return status;
6273 lsp = request->fl_u.nfs4_fl.owner;
6274 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6275 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6276 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006277 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006278}
6279#endif
6280
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6282{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006283 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006284 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006285 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006286 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287
Trond Myklebust01c3b862006-06-29 16:38:39 -04006288 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006289 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006290 if (status < 0)
6291 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006292 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006293 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006294 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006295 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006296 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006297 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006298 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006299 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006300 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006301 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006302 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006303 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006304 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006305 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006306out:
6307 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 return status;
6309}
6310
6311static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6312{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006313 struct nfs4_exception exception = {
6314 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006315 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006316 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 int err;
6318
6319 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006320 err = _nfs4_proc_setlk(state, cmd, request);
6321 if (err == -NFS4ERR_DENIED)
6322 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006324 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 } while (exception.retry);
6326 return err;
6327}
6328
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006329#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6330#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6331
6332static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006333nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6334 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006335{
6336 int status = -ERESTARTSYS;
6337 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6338
6339 while(!signalled()) {
6340 status = nfs4_proc_setlk(state, cmd, request);
6341 if ((status != -EAGAIN) || IS_SETLK(cmd))
6342 break;
6343 freezable_schedule_timeout_interruptible(timeout);
6344 timeout *= 2;
6345 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6346 status = -ERESTARTSYS;
6347 }
6348 return status;
6349}
6350
Jeff Laytona1d617d82016-09-17 18:17:39 -04006351#ifdef CONFIG_NFS_V4_1
6352struct nfs4_lock_waiter {
6353 struct task_struct *task;
6354 struct inode *inode;
6355 struct nfs_lowner *owner;
6356 bool notified;
6357};
6358
6359static int
6360nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6361{
6362 int ret;
6363 struct cb_notify_lock_args *cbnl = key;
6364 struct nfs4_lock_waiter *waiter = wait->private;
6365 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6366 *wowner = waiter->owner;
6367
6368 /* Only wake if the callback was for the same owner */
6369 if (lowner->clientid != wowner->clientid ||
6370 lowner->id != wowner->id ||
6371 lowner->s_dev != wowner->s_dev)
6372 return 0;
6373
6374 /* Make sure it's for the right inode */
6375 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6376 return 0;
6377
6378 waiter->notified = true;
6379
6380 /* override "private" so we can use default_wake_function */
6381 wait->private = waiter->task;
6382 ret = autoremove_wake_function(wait, mode, flags, key);
6383 wait->private = waiter;
6384 return ret;
6385}
6386
6387static int
6388nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6389{
6390 int status = -ERESTARTSYS;
6391 unsigned long flags;
6392 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6393 struct nfs_server *server = NFS_SERVER(state->inode);
6394 struct nfs_client *clp = server->nfs_client;
6395 wait_queue_head_t *q = &clp->cl_lock_waitq;
6396 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6397 .id = lsp->ls_seqid.owner_id,
6398 .s_dev = server->s_dev };
6399 struct nfs4_lock_waiter waiter = { .task = current,
6400 .inode = state->inode,
6401 .owner = &owner,
6402 .notified = false };
6403 wait_queue_t wait;
6404
6405 /* Don't bother with waitqueue if we don't expect a callback */
6406 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6407 return nfs4_retry_setlk_simple(state, cmd, request);
6408
6409 init_wait(&wait);
6410 wait.private = &waiter;
6411 wait.func = nfs4_wake_lock_waiter;
6412 add_wait_queue(q, &wait);
6413
6414 while(!signalled()) {
6415 status = nfs4_proc_setlk(state, cmd, request);
6416 if ((status != -EAGAIN) || IS_SETLK(cmd))
6417 break;
6418
6419 status = -ERESTARTSYS;
6420 spin_lock_irqsave(&q->lock, flags);
6421 if (waiter.notified) {
6422 spin_unlock_irqrestore(&q->lock, flags);
6423 continue;
6424 }
6425 set_current_state(TASK_INTERRUPTIBLE);
6426 spin_unlock_irqrestore(&q->lock, flags);
6427
6428 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6429 }
6430
6431 finish_wait(q, &wait);
6432 return status;
6433}
6434#else /* !CONFIG_NFS_V4_1 */
6435static inline int
6436nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6437{
6438 return nfs4_retry_setlk_simple(state, cmd, request);
6439}
6440#endif
6441
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442static int
6443nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6444{
6445 struct nfs_open_context *ctx;
6446 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447 int status;
6448
6449 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006450 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 state = ctx->state;
6452
6453 if (request->fl_start < 0 || request->fl_end < 0)
6454 return -EINVAL;
6455
Trond Myklebustd9531262009-07-21 19:22:38 -04006456 if (IS_GETLK(cmd)) {
6457 if (state != NULL)
6458 return nfs4_proc_getlk(state, F_GETLK, request);
6459 return 0;
6460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461
6462 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6463 return -EINVAL;
6464
Trond Myklebustd9531262009-07-21 19:22:38 -04006465 if (request->fl_type == F_UNLCK) {
6466 if (state != NULL)
6467 return nfs4_proc_unlck(state, cmd, request);
6468 return 0;
6469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470
Trond Myklebustd9531262009-07-21 19:22:38 -04006471 if (state == NULL)
6472 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006473
6474 if ((request->fl_flags & FL_POSIX) &&
6475 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6476 return -ENOLCK;
6477
Trond Myklebust55725512012-04-18 12:48:35 -04006478 /*
6479 * Don't rely on the VFS having checked the file open mode,
6480 * since it won't do this for flock() locks.
6481 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006482 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006483 case F_RDLCK:
6484 if (!(filp->f_mode & FMODE_READ))
6485 return -EBADF;
6486 break;
6487 case F_WRLCK:
6488 if (!(filp->f_mode & FMODE_WRITE))
6489 return -EBADF;
6490 }
6491
Jeff Layton1ea67db2016-09-17 18:17:37 -04006492 status = nfs4_set_lock_state(state, request);
6493 if (status != 0)
6494 return status;
6495
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006496 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497}
6498
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006499int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006500{
6501 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006502 int err;
6503
6504 err = nfs4_set_lock_state(state, fl);
6505 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006506 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006507 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006508 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006509}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006510
Trond Myklebustcf470c32012-03-07 13:49:12 -05006511struct nfs_release_lockowner_data {
6512 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006513 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006514 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006515 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006516 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006517};
6518
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006519static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6520{
6521 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006522 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006523 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6524 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006525 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006526 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006527}
6528
6529static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6530{
6531 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006532 struct nfs_server *server = data->server;
6533
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006534 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006535
6536 switch (task->tk_status) {
6537 case 0:
6538 renew_lease(server, data->timestamp);
6539 break;
6540 case -NFS4ERR_STALE_CLIENTID:
6541 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006542 nfs4_schedule_lease_recovery(server->nfs_client);
6543 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006544 case -NFS4ERR_LEASE_MOVED:
6545 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006546 if (nfs4_async_handle_error(task, server,
6547 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006548 rpc_restart_call_prepare(task);
6549 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006550}
6551
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006552static void nfs4_release_lockowner_release(void *calldata)
6553{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006554 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006555 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006556 kfree(calldata);
6557}
6558
Trond Myklebust17280172012-03-11 13:11:00 -04006559static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006560 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6561 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006562 .rpc_release = nfs4_release_lockowner_release,
6563};
6564
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006565static void
6566nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006567{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006568 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006569 struct rpc_message msg = {
6570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6571 };
6572
6573 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006574 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006575
Trond Myklebustcf470c32012-03-07 13:49:12 -05006576 data = kmalloc(sizeof(*data), GFP_NOFS);
6577 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006578 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006579 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006580 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006581 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6582 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6583 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006584
Trond Myklebustcf470c32012-03-07 13:49:12 -05006585 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006586 msg.rpc_resp = &data->res;
6587 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006588 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006589}
6590
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006591#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6592
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006593static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006594 struct dentry *unused, struct inode *inode,
6595 const char *key, const void *buf,
6596 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006597{
Al Viro59301222016-05-27 10:19:30 -04006598 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006599}
6600
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006601static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006602 struct dentry *unused, struct inode *inode,
6603 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006604{
Al Virob2968212016-04-10 20:48:24 -04006605 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006606}
6607
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006608static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006609{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006610 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006611}
6612
David Quigleyc9bccef2013-05-22 12:50:45 -04006613#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006614
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006615static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006616 struct dentry *unused, struct inode *inode,
6617 const char *key, const void *buf,
6618 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006619{
6620 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006621 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006622
6623 return -EOPNOTSUPP;
6624}
6625
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006626static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006627 struct dentry *unused, struct inode *inode,
6628 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006629{
6630 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006631 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006632 return -EOPNOTSUPP;
6633}
6634
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006635static ssize_t
6636nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006637{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006638 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006639
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006640 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6641 len = security_inode_listsecurity(inode, list, list_len);
6642 if (list_len && len > list_len)
6643 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006644 }
6645 return len;
6646}
6647
6648static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6649 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006650 .get = nfs4_xattr_get_nfs4_label,
6651 .set = nfs4_xattr_set_nfs4_label,
6652};
David Quigleyc9bccef2013-05-22 12:50:45 -04006653
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006654#else
6655
6656static ssize_t
6657nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6658{
6659 return 0;
6660}
6661
6662#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006663
Andy Adamson533eb462011-06-13 18:25:56 -04006664/*
6665 * nfs_fhget will use either the mounted_on_fileid or the fileid
6666 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006667static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6668{
Andy Adamson533eb462011-06-13 18:25:56 -04006669 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6670 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6671 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006672 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006673 return;
6674
6675 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006676 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006677 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6678 fattr->nlink = 2;
6679}
6680
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006681static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6682 const struct qstr *name,
6683 struct nfs4_fs_locations *fs_locations,
6684 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006685{
6686 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006687 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006688 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006689 };
6690 struct nfs4_fs_locations_arg args = {
6691 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006692 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006693 .page = page,
6694 .bitmask = bitmask,
6695 };
Benny Halevy22958462009-04-01 09:22:02 -04006696 struct nfs4_fs_locations_res res = {
6697 .fs_locations = fs_locations,
6698 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006699 struct rpc_message msg = {
6700 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6701 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006702 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006703 };
6704 int status;
6705
Harvey Harrison3110ff82008-05-02 13:42:44 -07006706 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006707
6708 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6709 * is not supported */
6710 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6711 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6712 else
6713 bitmask[0] |= FATTR4_WORD0_FILEID;
6714
Trond Myklebustc228fd32007-01-13 02:28:11 -05006715 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006716 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006717 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006718 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006719 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006720 return status;
6721}
6722
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006723int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6724 const struct qstr *name,
6725 struct nfs4_fs_locations *fs_locations,
6726 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006727{
6728 struct nfs4_exception exception = { };
6729 int err;
6730 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006731 err = _nfs4_proc_fs_locations(client, dir, name,
6732 fs_locations, page);
6733 trace_nfs4_get_fs_locations(dir, name, err);
6734 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006735 &exception);
6736 } while (exception.retry);
6737 return err;
6738}
6739
Chuck Leverb03d7352013-10-17 14:12:50 -04006740/*
6741 * This operation also signals the server that this client is
6742 * performing migration recovery. The server can stop returning
6743 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6744 * appended to this compound to identify the client ID which is
6745 * performing recovery.
6746 */
6747static int _nfs40_proc_get_locations(struct inode *inode,
6748 struct nfs4_fs_locations *locations,
6749 struct page *page, struct rpc_cred *cred)
6750{
6751 struct nfs_server *server = NFS_SERVER(inode);
6752 struct rpc_clnt *clnt = server->client;
6753 u32 bitmask[2] = {
6754 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6755 };
6756 struct nfs4_fs_locations_arg args = {
6757 .clientid = server->nfs_client->cl_clientid,
6758 .fh = NFS_FH(inode),
6759 .page = page,
6760 .bitmask = bitmask,
6761 .migration = 1, /* skip LOOKUP */
6762 .renew = 1, /* append RENEW */
6763 };
6764 struct nfs4_fs_locations_res res = {
6765 .fs_locations = locations,
6766 .migration = 1,
6767 .renew = 1,
6768 };
6769 struct rpc_message msg = {
6770 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6771 .rpc_argp = &args,
6772 .rpc_resp = &res,
6773 .rpc_cred = cred,
6774 };
6775 unsigned long now = jiffies;
6776 int status;
6777
6778 nfs_fattr_init(&locations->fattr);
6779 locations->server = server;
6780 locations->nlocations = 0;
6781
6782 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6783 nfs4_set_sequence_privileged(&args.seq_args);
6784 status = nfs4_call_sync_sequence(clnt, server, &msg,
6785 &args.seq_args, &res.seq_res);
6786 if (status)
6787 return status;
6788
6789 renew_lease(server, now);
6790 return 0;
6791}
6792
6793#ifdef CONFIG_NFS_V4_1
6794
6795/*
6796 * This operation also signals the server that this client is
6797 * performing migration recovery. The server can stop asserting
6798 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6799 * performing this operation is identified in the SEQUENCE
6800 * operation in this compound.
6801 *
6802 * When the client supports GETATTR(fs_locations_info), it can
6803 * be plumbed in here.
6804 */
6805static int _nfs41_proc_get_locations(struct inode *inode,
6806 struct nfs4_fs_locations *locations,
6807 struct page *page, struct rpc_cred *cred)
6808{
6809 struct nfs_server *server = NFS_SERVER(inode);
6810 struct rpc_clnt *clnt = server->client;
6811 u32 bitmask[2] = {
6812 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6813 };
6814 struct nfs4_fs_locations_arg args = {
6815 .fh = NFS_FH(inode),
6816 .page = page,
6817 .bitmask = bitmask,
6818 .migration = 1, /* skip LOOKUP */
6819 };
6820 struct nfs4_fs_locations_res res = {
6821 .fs_locations = locations,
6822 .migration = 1,
6823 };
6824 struct rpc_message msg = {
6825 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6826 .rpc_argp = &args,
6827 .rpc_resp = &res,
6828 .rpc_cred = cred,
6829 };
6830 int status;
6831
6832 nfs_fattr_init(&locations->fattr);
6833 locations->server = server;
6834 locations->nlocations = 0;
6835
6836 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6837 nfs4_set_sequence_privileged(&args.seq_args);
6838 status = nfs4_call_sync_sequence(clnt, server, &msg,
6839 &args.seq_args, &res.seq_res);
6840 if (status == NFS4_OK &&
6841 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6842 status = -NFS4ERR_LEASE_MOVED;
6843 return status;
6844}
6845
6846#endif /* CONFIG_NFS_V4_1 */
6847
6848/**
6849 * nfs4_proc_get_locations - discover locations for a migrated FSID
6850 * @inode: inode on FSID that is migrating
6851 * @locations: result of query
6852 * @page: buffer
6853 * @cred: credential to use for this operation
6854 *
6855 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6856 * operation failed, or a negative errno if a local error occurred.
6857 *
6858 * On success, "locations" is filled in, but if the server has
6859 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6860 * asserted.
6861 *
6862 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6863 * from this client that require migration recovery.
6864 */
6865int nfs4_proc_get_locations(struct inode *inode,
6866 struct nfs4_fs_locations *locations,
6867 struct page *page, struct rpc_cred *cred)
6868{
6869 struct nfs_server *server = NFS_SERVER(inode);
6870 struct nfs_client *clp = server->nfs_client;
6871 const struct nfs4_mig_recovery_ops *ops =
6872 clp->cl_mvops->mig_recovery_ops;
6873 struct nfs4_exception exception = { };
6874 int status;
6875
6876 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6877 (unsigned long long)server->fsid.major,
6878 (unsigned long long)server->fsid.minor,
6879 clp->cl_hostname);
6880 nfs_display_fhandle(NFS_FH(inode), __func__);
6881
6882 do {
6883 status = ops->get_locations(inode, locations, page, cred);
6884 if (status != -NFS4ERR_DELAY)
6885 break;
6886 nfs4_handle_exception(server, status, &exception);
6887 } while (exception.retry);
6888 return status;
6889}
6890
Chuck Lever44c99932013-10-17 14:13:30 -04006891/*
6892 * This operation also signals the server that this client is
6893 * performing "lease moved" recovery. The server can stop
6894 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6895 * is appended to this compound to identify the client ID which is
6896 * performing recovery.
6897 */
6898static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6899{
6900 struct nfs_server *server = NFS_SERVER(inode);
6901 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6902 struct rpc_clnt *clnt = server->client;
6903 struct nfs4_fsid_present_arg args = {
6904 .fh = NFS_FH(inode),
6905 .clientid = clp->cl_clientid,
6906 .renew = 1, /* append RENEW */
6907 };
6908 struct nfs4_fsid_present_res res = {
6909 .renew = 1,
6910 };
6911 struct rpc_message msg = {
6912 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6913 .rpc_argp = &args,
6914 .rpc_resp = &res,
6915 .rpc_cred = cred,
6916 };
6917 unsigned long now = jiffies;
6918 int status;
6919
6920 res.fh = nfs_alloc_fhandle();
6921 if (res.fh == NULL)
6922 return -ENOMEM;
6923
6924 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6925 nfs4_set_sequence_privileged(&args.seq_args);
6926 status = nfs4_call_sync_sequence(clnt, server, &msg,
6927 &args.seq_args, &res.seq_res);
6928 nfs_free_fhandle(res.fh);
6929 if (status)
6930 return status;
6931
6932 do_renew_lease(clp, now);
6933 return 0;
6934}
6935
6936#ifdef CONFIG_NFS_V4_1
6937
6938/*
6939 * This operation also signals the server that this client is
6940 * performing "lease moved" recovery. The server can stop asserting
6941 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6942 * this operation is identified in the SEQUENCE operation in this
6943 * compound.
6944 */
6945static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6946{
6947 struct nfs_server *server = NFS_SERVER(inode);
6948 struct rpc_clnt *clnt = server->client;
6949 struct nfs4_fsid_present_arg args = {
6950 .fh = NFS_FH(inode),
6951 };
6952 struct nfs4_fsid_present_res res = {
6953 };
6954 struct rpc_message msg = {
6955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6956 .rpc_argp = &args,
6957 .rpc_resp = &res,
6958 .rpc_cred = cred,
6959 };
6960 int status;
6961
6962 res.fh = nfs_alloc_fhandle();
6963 if (res.fh == NULL)
6964 return -ENOMEM;
6965
6966 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6967 nfs4_set_sequence_privileged(&args.seq_args);
6968 status = nfs4_call_sync_sequence(clnt, server, &msg,
6969 &args.seq_args, &res.seq_res);
6970 nfs_free_fhandle(res.fh);
6971 if (status == NFS4_OK &&
6972 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6973 status = -NFS4ERR_LEASE_MOVED;
6974 return status;
6975}
6976
6977#endif /* CONFIG_NFS_V4_1 */
6978
6979/**
6980 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6981 * @inode: inode on FSID to check
6982 * @cred: credential to use for this operation
6983 *
6984 * Server indicates whether the FSID is present, moved, or not
6985 * recognized. This operation is necessary to clear a LEASE_MOVED
6986 * condition for this client ID.
6987 *
6988 * Returns NFS4_OK if the FSID is present on this server,
6989 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6990 * NFS4ERR code if some error occurred on the server, or a
6991 * negative errno if a local failure occurred.
6992 */
6993int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6994{
6995 struct nfs_server *server = NFS_SERVER(inode);
6996 struct nfs_client *clp = server->nfs_client;
6997 const struct nfs4_mig_recovery_ops *ops =
6998 clp->cl_mvops->mig_recovery_ops;
6999 struct nfs4_exception exception = { };
7000 int status;
7001
7002 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7003 (unsigned long long)server->fsid.major,
7004 (unsigned long long)server->fsid.minor,
7005 clp->cl_hostname);
7006 nfs_display_fhandle(NFS_FH(inode), __func__);
7007
7008 do {
7009 status = ops->fsid_present(inode, cred);
7010 if (status != -NFS4ERR_DELAY)
7011 break;
7012 nfs4_handle_exception(server, status, &exception);
7013 } while (exception.retry);
7014 return status;
7015}
7016
Andy Adamson5ec16a82013-08-08 10:57:55 -04007017/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007018 * If 'use_integrity' is true and the state managment nfs_client
7019 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7020 * and the machine credential as per RFC3530bis and RFC5661 Security
7021 * Considerations sections. Otherwise, just use the user cred with the
7022 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007023 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007024static 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 +00007025{
7026 int status;
7027 struct nfs4_secinfo_arg args = {
7028 .dir_fh = NFS_FH(dir),
7029 .name = name,
7030 };
7031 struct nfs4_secinfo_res res = {
7032 .flavors = flavors,
7033 };
7034 struct rpc_message msg = {
7035 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7036 .rpc_argp = &args,
7037 .rpc_resp = &res,
7038 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007039 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007040 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007041
7042 if (use_integrity) {
7043 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007044 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7045 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007046 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007047
7048 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007049
7050 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7051 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7052
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007053 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7054 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007055 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007056
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007057 if (cred)
7058 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007059
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007060 return status;
7061}
7062
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007063int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7064 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007065{
7066 struct nfs4_exception exception = { };
7067 int err;
7068 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007069 err = -NFS4ERR_WRONGSEC;
7070
7071 /* try to use integrity protection with machine cred */
7072 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7073 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7074
7075 /*
7076 * if unable to use integrity protection, or SECINFO with
7077 * integrity protection returns NFS4ERR_WRONGSEC (which is
7078 * disallowed by spec, but exists in deployed servers) use
7079 * the current filesystem's rpc_client and the user cred.
7080 */
7081 if (err == -NFS4ERR_WRONGSEC)
7082 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7083
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007084 trace_nfs4_secinfo(dir, name, err);
7085 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007086 &exception);
7087 } while (exception.retry);
7088 return err;
7089}
7090
Andy Adamson557134a2009-04-01 09:21:53 -04007091#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007092/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007093 * Check the exchange flags returned by the server for invalid flags, having
7094 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7095 * DS flags set.
7096 */
7097static int nfs4_check_cl_exchange_flags(u32 flags)
7098{
7099 if (flags & ~EXCHGID4_FLAG_MASK_R)
7100 goto out_inval;
7101 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7102 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7103 goto out_inval;
7104 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7105 goto out_inval;
7106 return NFS_OK;
7107out_inval:
7108 return -NFS4ERR_INVAL;
7109}
7110
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007111static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007112nfs41_same_server_scope(struct nfs41_server_scope *a,
7113 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007114{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007115 if (a->server_scope_sz != b->server_scope_sz)
7116 return false;
7117 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007118}
7119
Andy Adamson02a95de2016-02-05 16:08:37 -05007120static void
7121nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7122{
7123}
7124
7125static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7126 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7127};
7128
Andy Adamson357f54d2010-12-14 10:11:57 -05007129/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007130 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007131 *
7132 * The 4.1 client currently uses the same TCP connection for the
7133 * fore and backchannel.
7134 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007135static
7136int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7137 struct rpc_xprt *xprt,
7138 struct nfs_client *clp,
7139 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007140{
7141 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007142 struct nfs41_bind_conn_to_session_args args = {
7143 .client = clp,
7144 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7145 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007146 struct nfs41_bind_conn_to_session_res res;
7147 struct rpc_message msg = {
7148 .rpc_proc =
7149 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007150 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007151 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007152 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007153 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007154 struct rpc_task_setup task_setup_data = {
7155 .rpc_client = clnt,
7156 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007157 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007158 .rpc_message = &msg,
7159 .flags = RPC_TASK_TIMEOUT,
7160 };
7161 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007162
7163 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007164
Trond Myklebust71a097c2015-02-18 09:27:18 -08007165 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7166 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7167 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007168
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007169 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7170 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7171 args.dir = NFS4_CDFC4_FORE;
7172
7173 task = rpc_run_task(&task_setup_data);
7174 if (!IS_ERR(task)) {
7175 status = task->tk_status;
7176 rpc_put_task(task);
7177 } else
7178 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007179 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007180 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007181 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007182 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7183 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7184 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007185 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007186 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007187 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007188 dprintk("NFS: %s: Unexpected direction from server\n",
7189 __func__);
7190 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007191 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007192 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007193 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007194 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7195 __func__);
7196 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007197 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007198 }
7199 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007200out:
7201 dprintk("<-- %s status= %d\n", __func__, status);
7202 return status;
7203}
7204
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007205struct rpc_bind_conn_calldata {
7206 struct nfs_client *clp;
7207 struct rpc_cred *cred;
7208};
7209
7210static int
7211nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7212 struct rpc_xprt *xprt,
7213 void *calldata)
7214{
7215 struct rpc_bind_conn_calldata *p = calldata;
7216
7217 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7218}
7219
7220int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7221{
7222 struct rpc_bind_conn_calldata data = {
7223 .clp = clp,
7224 .cred = cred,
7225 };
7226 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7227 nfs4_proc_bind_conn_to_session_callback, &data);
7228}
7229
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007230/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007231 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7232 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007233 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007234static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7235 .how = SP4_MACH_CRED,
7236 .enforce.u.words = {
7237 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7238 1 << (OP_EXCHANGE_ID - 32) |
7239 1 << (OP_CREATE_SESSION - 32) |
7240 1 << (OP_DESTROY_SESSION - 32) |
7241 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007242 },
7243 .allow.u.words = {
7244 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007245 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007246 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007247 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007248 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007249 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007250 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007251 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007252 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007253 1 << (OP_FREE_STATEID - 32) |
7254 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007255 }
7256};
7257
7258/*
7259 * Select the state protection mode for client `clp' given the server results
7260 * from exchange_id in `sp'.
7261 *
7262 * Returns 0 on success, negative errno otherwise.
7263 */
7264static int nfs4_sp4_select_mode(struct nfs_client *clp,
7265 struct nfs41_state_protection *sp)
7266{
7267 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7268 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7269 1 << (OP_EXCHANGE_ID - 32) |
7270 1 << (OP_CREATE_SESSION - 32) |
7271 1 << (OP_DESTROY_SESSION - 32) |
7272 1 << (OP_DESTROY_CLIENTID - 32)
7273 };
7274 unsigned int i;
7275
7276 if (sp->how == SP4_MACH_CRED) {
7277 /* Print state protect result */
7278 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7279 for (i = 0; i <= LAST_NFS4_OP; i++) {
7280 if (test_bit(i, sp->enforce.u.longs))
7281 dfprintk(MOUNT, " enforce op %d\n", i);
7282 if (test_bit(i, sp->allow.u.longs))
7283 dfprintk(MOUNT, " allow op %d\n", i);
7284 }
7285
7286 /* make sure nothing is on enforce list that isn't supported */
7287 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7288 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7289 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7290 return -EINVAL;
7291 }
7292 }
7293
7294 /*
7295 * Minimal mode - state operations are allowed to use machine
7296 * credential. Note this already happens by default, so the
7297 * client doesn't have to do anything more than the negotiation.
7298 *
7299 * NOTE: we don't care if EXCHANGE_ID is in the list -
7300 * we're already using the machine cred for exchange_id
7301 * and will never use a different cred.
7302 */
7303 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7304 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7305 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7306 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7307 dfprintk(MOUNT, "sp4_mach_cred:\n");
7308 dfprintk(MOUNT, " minimal mode enabled\n");
7309 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7310 } else {
7311 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7312 return -EINVAL;
7313 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007314
7315 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007316 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7317 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007318 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7319 dfprintk(MOUNT, " cleanup mode enabled\n");
7320 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7321 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007322
Andrew Elble99ade3c2015-12-02 09:39:51 -05007323 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7324 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7325 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7326 &clp->cl_sp4_flags);
7327 }
7328
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007329 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7330 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7331 dfprintk(MOUNT, " secinfo mode enabled\n");
7332 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7333 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007334
7335 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7336 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7337 dfprintk(MOUNT, " stateid mode enabled\n");
7338 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7339 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007340
7341 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7342 dfprintk(MOUNT, " write mode enabled\n");
7343 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7344 }
7345
7346 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7347 dfprintk(MOUNT, " commit mode enabled\n");
7348 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7349 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007350 }
7351
7352 return 0;
7353}
7354
Andy Adamson8d89bd72016-09-09 09:22:18 -04007355struct nfs41_exchange_id_data {
7356 struct nfs41_exchange_id_res res;
7357 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007358 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007359 int rpc_status;
7360};
7361
7362static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007363{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007364 struct nfs41_exchange_id_data *cdata =
7365 (struct nfs41_exchange_id_data *)data;
7366 struct nfs_client *clp = cdata->args.client;
7367 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007368
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007369 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007370
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007371 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007372 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007373
7374 if (cdata->xprt && status == 0) {
7375 status = nfs4_detect_session_trunking(clp, &cdata->res,
7376 cdata->xprt);
7377 goto out;
7378 }
7379
Andy Adamson8d89bd72016-09-09 09:22:18 -04007380 if (status == 0)
7381 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007382
Chuck Lever177313f2012-05-21 22:44:58 -04007383 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007384 clp->cl_clientid = cdata->res.clientid;
7385 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007386 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007387 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007388 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007389 &clp->cl_session->session_state);
7390 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007391 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007392
Chuck Leveracdeb692012-05-21 22:46:16 -04007393 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007394 clp->cl_serverowner = cdata->res.server_owner;
7395 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007396
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007397 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007398 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007399 clp->cl_implid = cdata->res.impl_id;
7400 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007401
Chuck Lever177313f2012-05-21 22:44:58 -04007402 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007403 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007404 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007405 dprintk("%s: server_scope mismatch detected\n",
7406 __func__);
7407 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007408 kfree(clp->cl_serverscope);
7409 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007410 }
7411
Chuck Lever177313f2012-05-21 22:44:58 -04007412 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007413 clp->cl_serverscope = cdata->res.server_scope;
7414 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007415 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007416 /* Save the EXCHANGE_ID verifier session trunk tests */
7417 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7418 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007419 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007420out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007421 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007422 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007423}
7424
7425static void nfs4_exchange_id_release(void *data)
7426{
7427 struct nfs41_exchange_id_data *cdata =
7428 (struct nfs41_exchange_id_data *)data;
7429
7430 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007431 if (cdata->xprt) {
7432 xprt_put(cdata->xprt);
7433 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7434 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007435 kfree(cdata->res.impl_id);
7436 kfree(cdata->res.server_scope);
7437 kfree(cdata->res.server_owner);
7438 kfree(cdata);
7439}
7440
7441static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7442 .rpc_call_done = nfs4_exchange_id_done,
7443 .rpc_release = nfs4_exchange_id_release,
7444};
7445
Benny Halevy99fe60d2009-04-01 09:22:29 -04007446/*
7447 * _nfs4_proc_exchange_id()
7448 *
7449 * Wrapper for EXCHANGE_ID operation.
7450 */
7451static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007452 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007453{
7454 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007455 struct rpc_message msg = {
7456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007457 .rpc_cred = cred,
7458 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007459 struct rpc_task_setup task_setup_data = {
7460 .rpc_client = clp->cl_rpcclient,
7461 .callback_ops = &nfs4_exchange_id_call_ops,
7462 .rpc_message = &msg,
7463 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7464 };
7465 struct nfs41_exchange_id_data *calldata;
7466 struct rpc_task *task;
7467 int status = -EIO;
7468
7469 if (!atomic_inc_not_zero(&clp->cl_count))
7470 goto out;
7471
7472 status = -ENOMEM;
7473 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7474 if (!calldata)
7475 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007476
Andy Adamsonad0849a2016-09-09 09:22:28 -04007477 if (!xprt)
7478 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007479
7480 status = nfs4_init_uniform_client_string(clp);
7481 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007482 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007483
7484 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7485 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7486 clp->cl_owner_id);
7487
Andy Adamson8d89bd72016-09-09 09:22:18 -04007488 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7489 GFP_NOFS);
7490 status = -ENOMEM;
7491 if (unlikely(calldata->res.server_owner == NULL))
7492 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007493
Andy Adamson8d89bd72016-09-09 09:22:18 -04007494 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007495 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007496 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007497 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007498
Andy Adamson8d89bd72016-09-09 09:22:18 -04007499 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7500 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007501 goto out_server_scope;
7502
7503 switch (sp4_how) {
7504 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007505 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007506 break;
7507
7508 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007509 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007510 break;
7511
7512 default:
7513 /* unsupported! */
7514 WARN_ON_ONCE(1);
7515 status = -EINVAL;
7516 goto out_impl_id;
7517 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007518 if (xprt) {
7519 calldata->xprt = xprt;
7520 task_setup_data.rpc_xprt = xprt;
7521 task_setup_data.flags =
7522 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7523 calldata->args.verifier = &clp->cl_confirm;
7524 } else {
7525 calldata->args.verifier = &verifier;
7526 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007527 calldata->args.client = clp;
7528#ifdef CONFIG_NFS_V4_1_MIGRATION
7529 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7530 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7531 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7532#else
7533 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7534 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7535#endif
7536 msg.rpc_argp = &calldata->args;
7537 msg.rpc_resp = &calldata->res;
7538 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007539
Andy Adamson8d89bd72016-09-09 09:22:18 -04007540 task = rpc_run_task(&task_setup_data);
7541 if (IS_ERR(task)) {
7542 status = PTR_ERR(task);
7543 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007544 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007545
Andy Adamsonad0849a2016-09-09 09:22:28 -04007546 if (!xprt) {
7547 status = rpc_wait_for_completion_task(task);
7548 if (!status)
7549 status = calldata->rpc_status;
7550 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007551 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007552
Andy Adamson8d89bd72016-09-09 09:22:18 -04007553 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007554out:
Chuck Lever177313f2012-05-21 22:44:58 -04007555 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007556 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007557 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007558 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007559 clp->cl_implid->date.seconds,
7560 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007561 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007562 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007563
7564out_impl_id:
7565 kfree(calldata->res.impl_id);
7566out_server_scope:
7567 kfree(calldata->res.server_scope);
7568out_server_owner:
7569 kfree(calldata->res.server_owner);
7570out_calldata:
7571 kfree(calldata);
7572 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007573}
7574
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007575/*
7576 * nfs4_proc_exchange_id()
7577 *
7578 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7579 *
7580 * Since the clientid has expired, all compounds using sessions
7581 * associated with the stale clientid will be returning
7582 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7583 * be in some phase of session reset.
7584 *
7585 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7586 */
7587int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7588{
7589 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7590 int status;
7591
7592 /* try SP4_MACH_CRED if krb5i/p */
7593 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7594 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007595 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007596 if (!status)
7597 return 0;
7598 }
7599
7600 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007601 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007602}
7603
Andy Adamson04fa2c62016-09-09 09:22:29 -04007604/**
7605 * nfs4_test_session_trunk
7606 *
7607 * This is an add_xprt_test() test function called from
7608 * rpc_clnt_setup_test_and_add_xprt.
7609 *
7610 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7611 * and is dereferrenced in nfs4_exchange_id_release
7612 *
7613 * Upon success, add the new transport to the rpc_clnt
7614 *
7615 * @clnt: struct rpc_clnt to get new transport
7616 * @xprt: the rpc_xprt to test
7617 * @data: call data for _nfs4_proc_exchange_id.
7618 */
7619int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7620 void *data)
7621{
7622 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7623 u32 sp4_how;
7624
7625 dprintk("--> %s try %s\n", __func__,
7626 xprt->address_strings[RPC_DISPLAY_ADDR]);
7627
7628 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7629
7630 /* Test connection for session trunking. Async exchange_id call */
7631 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7632}
7633EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7634
Trond Myklebust66245532012-05-25 17:18:09 -04007635static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7636 struct rpc_cred *cred)
7637{
7638 struct rpc_message msg = {
7639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7640 .rpc_argp = clp,
7641 .rpc_cred = cred,
7642 };
7643 int status;
7644
7645 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007646 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007647 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007648 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007649 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7650 return status;
7651}
7652
7653static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7654 struct rpc_cred *cred)
7655{
7656 unsigned int loop;
7657 int ret;
7658
7659 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7660 ret = _nfs4_proc_destroy_clientid(clp, cred);
7661 switch (ret) {
7662 case -NFS4ERR_DELAY:
7663 case -NFS4ERR_CLIENTID_BUSY:
7664 ssleep(1);
7665 break;
7666 default:
7667 return ret;
7668 }
7669 }
7670 return 0;
7671}
7672
7673int nfs4_destroy_clientid(struct nfs_client *clp)
7674{
7675 struct rpc_cred *cred;
7676 int ret = 0;
7677
7678 if (clp->cl_mvops->minor_version < 1)
7679 goto out;
7680 if (clp->cl_exchange_flags == 0)
7681 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007682 if (clp->cl_preserve_clid)
7683 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007684 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007685 ret = nfs4_proc_destroy_clientid(clp, cred);
7686 if (cred)
7687 put_rpccred(cred);
7688 switch (ret) {
7689 case 0:
7690 case -NFS4ERR_STALE_CLIENTID:
7691 clp->cl_exchange_flags = 0;
7692 }
7693out:
7694 return ret;
7695}
7696
Andy Adamson2050f0c2009-04-01 09:22:30 -04007697struct nfs4_get_lease_time_data {
7698 struct nfs4_get_lease_time_args *args;
7699 struct nfs4_get_lease_time_res *res;
7700 struct nfs_client *clp;
7701};
7702
7703static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7704 void *calldata)
7705{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007706 struct nfs4_get_lease_time_data *data =
7707 (struct nfs4_get_lease_time_data *)calldata;
7708
7709 dprintk("--> %s\n", __func__);
7710 /* just setup sequence, do not trigger session recovery
7711 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007712 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007713 &data->args->la_seq_args,
7714 &data->res->lr_seq_res,
7715 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007716 dprintk("<-- %s\n", __func__);
7717}
7718
7719/*
7720 * Called from nfs4_state_manager thread for session setup, so don't recover
7721 * from sequence operation or clientid errors.
7722 */
7723static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7724{
7725 struct nfs4_get_lease_time_data *data =
7726 (struct nfs4_get_lease_time_data *)calldata;
7727
7728 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007729 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7730 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007731 switch (task->tk_status) {
7732 case -NFS4ERR_DELAY:
7733 case -NFS4ERR_GRACE:
7734 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7735 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7736 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007737 /* fall through */
7738 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007739 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007740 return;
7741 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007742 dprintk("<-- %s\n", __func__);
7743}
7744
Trond Myklebust17280172012-03-11 13:11:00 -04007745static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007746 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7747 .rpc_call_done = nfs4_get_lease_time_done,
7748};
7749
7750int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7751{
7752 struct rpc_task *task;
7753 struct nfs4_get_lease_time_args args;
7754 struct nfs4_get_lease_time_res res = {
7755 .lr_fsinfo = fsinfo,
7756 };
7757 struct nfs4_get_lease_time_data data = {
7758 .args = &args,
7759 .res = &res,
7760 .clp = clp,
7761 };
7762 struct rpc_message msg = {
7763 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7764 .rpc_argp = &args,
7765 .rpc_resp = &res,
7766 };
7767 struct rpc_task_setup task_setup = {
7768 .rpc_client = clp->cl_rpcclient,
7769 .rpc_message = &msg,
7770 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007771 .callback_data = &data,
7772 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007773 };
7774 int status;
7775
Chuck Levera9c92d62013-08-09 12:48:18 -04007776 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007777 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007778 dprintk("--> %s\n", __func__);
7779 task = rpc_run_task(&task_setup);
7780
7781 if (IS_ERR(task))
7782 status = PTR_ERR(task);
7783 else {
7784 status = task->tk_status;
7785 rpc_put_task(task);
7786 }
7787 dprintk("<-- %s return %d\n", __func__, status);
7788
7789 return status;
7790}
7791
Andy Adamsonfc931582009-04-01 09:22:31 -04007792/*
7793 * Initialize the values to be used by the client in CREATE_SESSION
7794 * If nfs4_init_session set the fore channel request and response sizes,
7795 * use them.
7796 *
7797 * Set the back channel max_resp_sz_cached to zero to force the client to
7798 * always set csa_cachethis to FALSE because the current implementation
7799 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7800 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007801static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7802 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007803{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007804 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007805 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007806
Andy Adamson18aad3d2013-06-26 12:21:49 -04007807 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7808 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7809
Andy Adamsonfc931582009-04-01 09:22:31 -04007810 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007811 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7812 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007813 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007814 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007815
7816 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007817 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007818 __func__,
7819 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007820 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007821
7822 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007823 args->bc_attrs.max_rqst_sz = max_bc_payload;
7824 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007825 args->bc_attrs.max_resp_sz_cached = 0;
7826 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007827 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007828
7829 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7830 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7831 __func__,
7832 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7833 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7834 args->bc_attrs.max_reqs);
7835}
7836
Trond Myklebust79969dd2015-02-18 11:30:18 -08007837static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7838 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007839{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007840 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007841 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007842
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007843 if (rcvd->max_resp_sz > sent->max_resp_sz)
7844 return -EINVAL;
7845 /*
7846 * Our requested max_ops is the minimum we need; we're not
7847 * prepared to break up compounds into smaller pieces than that.
7848 * So, no point even trying to continue if the server won't
7849 * cooperate:
7850 */
7851 if (rcvd->max_ops < sent->max_ops)
7852 return -EINVAL;
7853 if (rcvd->max_reqs == 0)
7854 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007855 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7856 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007857 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007858}
7859
Trond Myklebust79969dd2015-02-18 11:30:18 -08007860static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7861 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007862{
7863 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007864 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007865
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007866 if (!(res->flags & SESSION4_BACK_CHAN))
7867 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007868 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7869 return -EINVAL;
7870 if (rcvd->max_resp_sz < sent->max_resp_sz)
7871 return -EINVAL;
7872 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7873 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007874 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007875 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007876 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007877 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007878out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007879 return 0;
7880}
Andy Adamson8d353012009-04-01 09:22:32 -04007881
Andy Adamson8d353012009-04-01 09:22:32 -04007882static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007883 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007884{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007885 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007886
Trond Myklebust79969dd2015-02-18 11:30:18 -08007887 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007888 if (ret)
7889 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007890 return nfs4_verify_back_channel_attrs(args, res);
7891}
7892
7893static void nfs4_update_session(struct nfs4_session *session,
7894 struct nfs41_create_session_res *res)
7895{
7896 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007897 /* Mark client id and session as being confirmed */
7898 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7899 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007900 session->flags = res->flags;
7901 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007902 if (res->flags & SESSION4_BACK_CHAN)
7903 memcpy(&session->bc_attrs, &res->bc_attrs,
7904 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007905}
7906
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007907static int _nfs4_proc_create_session(struct nfs_client *clp,
7908 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007909{
7910 struct nfs4_session *session = clp->cl_session;
7911 struct nfs41_create_session_args args = {
7912 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007913 .clientid = clp->cl_clientid,
7914 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007915 .cb_program = NFS4_CALLBACK,
7916 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007917 struct nfs41_create_session_res res;
7918
Andy Adamsonfc931582009-04-01 09:22:31 -04007919 struct rpc_message msg = {
7920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7921 .rpc_argp = &args,
7922 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007923 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007924 };
7925 int status;
7926
Chuck Lever6b26cc82016-05-02 14:40:40 -04007927 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007928 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007929
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007930 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007931 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007932
Trond Myklebustb519d402016-09-11 14:50:01 -04007933 switch (status) {
7934 case -NFS4ERR_STALE_CLIENTID:
7935 case -NFS4ERR_DELAY:
7936 case -ETIMEDOUT:
7937 case -EACCES:
7938 case -EAGAIN:
7939 goto out;
7940 };
7941
7942 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007943 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007944 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007945 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007946 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007947 if (status)
7948 goto out;
7949 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007950 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007951out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007952 return status;
7953}
7954
7955/*
7956 * Issues a CREATE_SESSION operation to the server.
7957 * It is the responsibility of the caller to verify the session is
7958 * expired before calling this routine.
7959 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007960int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007961{
7962 int status;
7963 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007964 struct nfs4_session *session = clp->cl_session;
7965
7966 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7967
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007968 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007969 if (status)
7970 goto out;
7971
Andy Adamsonaacd5532011-11-09 13:58:21 -05007972 /* Init or reset the session slot tables */
7973 status = nfs4_setup_session_slot_tables(session);
7974 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007975 if (status)
7976 goto out;
7977
7978 ptr = (unsigned *)&session->sess_id.data[0];
7979 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7980 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007981out:
7982 dprintk("<-- %s\n", __func__);
7983 return status;
7984}
7985
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007986/*
7987 * Issue the over-the-wire RPC DESTROY_SESSION.
7988 * The caller must serialize access to this routine.
7989 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007990int nfs4_proc_destroy_session(struct nfs4_session *session,
7991 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007992{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007993 struct rpc_message msg = {
7994 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7995 .rpc_argp = session,
7996 .rpc_cred = cred,
7997 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007998 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007999
8000 dprintk("--> nfs4_proc_destroy_session\n");
8001
8002 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008003 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8004 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008005
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008006 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008007 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008008
8009 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008010 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008011 "Session has been destroyed regardless...\n", status);
8012
8013 dprintk("<-- nfs4_proc_destroy_session\n");
8014 return status;
8015}
8016
Trond Myklebust7b38c362012-05-23 13:23:31 -04008017/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008018 * Renew the cl_session lease.
8019 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008020struct nfs4_sequence_data {
8021 struct nfs_client *clp;
8022 struct nfs4_sequence_args args;
8023 struct nfs4_sequence_res res;
8024};
8025
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008026static void nfs41_sequence_release(void *data)
8027{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008028 struct nfs4_sequence_data *calldata = data;
8029 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008030
Alexandros Batsakis71358402010-02-05 03:45:05 -08008031 if (atomic_read(&clp->cl_count) > 1)
8032 nfs4_schedule_state_renewal(clp);
8033 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008034 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008035}
8036
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008037static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8038{
8039 switch(task->tk_status) {
8040 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008041 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8042 return -EAGAIN;
8043 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008044 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008045 }
8046 return 0;
8047}
8048
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008049static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008050{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008051 struct nfs4_sequence_data *calldata = data;
8052 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008053
Trond Myklebust14516c32010-07-31 14:29:06 -04008054 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8055 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008056
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008057 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008058 if (task->tk_status < 0) {
8059 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008060 if (atomic_read(&clp->cl_count) == 1)
8061 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008062
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008063 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8064 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008065 return;
8066 }
8067 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008068 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008069out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008070 dprintk("<-- %s\n", __func__);
8071}
8072
8073static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8074{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008075 struct nfs4_sequence_data *calldata = data;
8076 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008077 struct nfs4_sequence_args *args;
8078 struct nfs4_sequence_res *res;
8079
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008080 args = task->tk_msg.rpc_argp;
8081 res = task->tk_msg.rpc_resp;
8082
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008083 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008084}
8085
8086static const struct rpc_call_ops nfs41_sequence_ops = {
8087 .rpc_call_done = nfs41_sequence_call_done,
8088 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008089 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008090};
8091
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008092static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8093 struct rpc_cred *cred,
8094 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008095{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008096 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008097 struct rpc_message msg = {
8098 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8099 .rpc_cred = cred,
8100 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008101 struct rpc_task_setup task_setup_data = {
8102 .rpc_client = clp->cl_rpcclient,
8103 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008104 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008105 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008106 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008107
Alexandros Batsakis71358402010-02-05 03:45:05 -08008108 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008109 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008110 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008111 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008112 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008113 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008114 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008115 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008116 if (is_privileged)
8117 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008118 msg.rpc_argp = &calldata->args;
8119 msg.rpc_resp = &calldata->res;
8120 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008121 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008122
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008123 return rpc_run_task(&task_setup_data);
8124}
8125
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008126static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008127{
8128 struct rpc_task *task;
8129 int ret = 0;
8130
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008131 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008132 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008133 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008134 if (IS_ERR(task))
8135 ret = PTR_ERR(task);
8136 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008137 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008138 dprintk("<-- %s status=%d\n", __func__, ret);
8139 return ret;
8140}
8141
8142static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8143{
8144 struct rpc_task *task;
8145 int ret;
8146
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008147 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008148 if (IS_ERR(task)) {
8149 ret = PTR_ERR(task);
8150 goto out;
8151 }
8152 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008153 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008154 ret = task->tk_status;
8155 rpc_put_task(task);
8156out:
8157 dprintk("<-- %s status=%d\n", __func__, ret);
8158 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008159}
8160
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008161struct nfs4_reclaim_complete_data {
8162 struct nfs_client *clp;
8163 struct nfs41_reclaim_complete_args arg;
8164 struct nfs41_reclaim_complete_res res;
8165};
8166
8167static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8168{
8169 struct nfs4_reclaim_complete_data *calldata = data;
8170
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008171 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008172 &calldata->arg.seq_args,
8173 &calldata->res.seq_res,
8174 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008175}
8176
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008177static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8178{
8179 switch(task->tk_status) {
8180 case 0:
8181 case -NFS4ERR_COMPLETE_ALREADY:
8182 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8183 break;
8184 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008185 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008186 /* fall through */
8187 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008188 return -EAGAIN;
8189 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008190 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008191 }
8192 return 0;
8193}
8194
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008195static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8196{
8197 struct nfs4_reclaim_complete_data *calldata = data;
8198 struct nfs_client *clp = calldata->clp;
8199 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8200
8201 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008202 if (!nfs41_sequence_done(task, res))
8203 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008204
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008205 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008206 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8207 rpc_restart_call_prepare(task);
8208 return;
8209 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008210 dprintk("<-- %s\n", __func__);
8211}
8212
8213static void nfs4_free_reclaim_complete_data(void *data)
8214{
8215 struct nfs4_reclaim_complete_data *calldata = data;
8216
8217 kfree(calldata);
8218}
8219
8220static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8221 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8222 .rpc_call_done = nfs4_reclaim_complete_done,
8223 .rpc_release = nfs4_free_reclaim_complete_data,
8224};
8225
8226/*
8227 * Issue a global reclaim complete.
8228 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008229static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8230 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008231{
8232 struct nfs4_reclaim_complete_data *calldata;
8233 struct rpc_task *task;
8234 struct rpc_message msg = {
8235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008236 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008237 };
8238 struct rpc_task_setup task_setup_data = {
8239 .rpc_client = clp->cl_rpcclient,
8240 .rpc_message = &msg,
8241 .callback_ops = &nfs4_reclaim_complete_call_ops,
8242 .flags = RPC_TASK_ASYNC,
8243 };
8244 int status = -ENOMEM;
8245
8246 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008247 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008248 if (calldata == NULL)
8249 goto out;
8250 calldata->clp = clp;
8251 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008252
Chuck Levera9c92d62013-08-09 12:48:18 -04008253 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008254 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008255 msg.rpc_argp = &calldata->arg;
8256 msg.rpc_resp = &calldata->res;
8257 task_setup_data.callback_data = calldata;
8258 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008259 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008260 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008261 goto out;
8262 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008263 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008264 if (status == 0)
8265 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008266 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008267 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008268out:
8269 dprintk("<-- %s status=%d\n", __func__, status);
8270 return status;
8271}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008272
8273static void
8274nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8275{
8276 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008277 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008278
8279 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008280 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008281 &lgp->res.seq_res, task);
8282 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008283}
8284
8285static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8286{
8287 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008288
8289 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008290 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008291 dprintk("<-- %s\n", __func__);
8292}
8293
8294static int
8295nfs4_layoutget_handle_exception(struct rpc_task *task,
8296 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8297{
Trond Myklebustee314c22012-10-01 17:25:48 -07008298 struct inode *inode = lgp->args.inode;
8299 struct nfs_server *server = NFS_SERVER(inode);
8300 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008301 int nfs4err = task->tk_status;
8302 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008303 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008304
Boaz Harroshed7e5422014-01-22 20:34:54 +02008305 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008306
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008307 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008308 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008309 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008310
8311 /*
8312 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8313 * on the file. set tk_status to -ENODATA to tell upper layer to
8314 * retry go inband.
8315 */
8316 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008317 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008318 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008319 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008320 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8321 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8322 */
8323 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008324 status = -EOVERFLOW;
8325 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008326 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008327 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008328 * (or clients) writing to the same RAID stripe except when
8329 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008330 *
8331 * Treat it like we would RECALLCONFLICT -- we retry for a little
8332 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008333 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008334 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008335 if (lgp->args.minlength == 0) {
8336 status = -EOVERFLOW;
8337 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008338 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008339 status = -EBUSY;
8340 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008341 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008342 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008343 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008344 case -NFS4ERR_DELEG_REVOKED:
8345 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008346 case -NFS4ERR_EXPIRED:
8347 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008348 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008349 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008350 lo = NFS_I(inode)->layout;
8351 /* If the open stateid was bad, then recover it. */
8352 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8353 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008354 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008355 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008356 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008357 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008358 break;
8359 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008360
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008361 /*
8362 * Mark the bad layout state as invalid, then retry
8363 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008364 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008365 spin_unlock(&inode->i_lock);
8366 pnfs_free_lseg_list(&head);
8367 status = -EAGAIN;
8368 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008369 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008370
Trond Myklebust8ac09252017-01-23 22:44:12 -05008371 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008372 err = nfs4_handle_exception(server, nfs4err, exception);
8373 if (!status) {
8374 if (exception->retry)
8375 status = -EAGAIN;
8376 else
8377 status = err;
8378 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008379out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008380 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008381 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008382}
8383
Idan Kedar85541162012-08-02 11:47:10 +03008384static size_t max_response_pages(struct nfs_server *server)
8385{
8386 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8387 return nfs_page_array_len(0, max_resp_sz);
8388}
8389
8390static void nfs4_free_pages(struct page **pages, size_t size)
8391{
8392 int i;
8393
8394 if (!pages)
8395 return;
8396
8397 for (i = 0; i < size; i++) {
8398 if (!pages[i])
8399 break;
8400 __free_page(pages[i]);
8401 }
8402 kfree(pages);
8403}
8404
8405static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8406{
8407 struct page **pages;
8408 int i;
8409
8410 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8411 if (!pages) {
8412 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8413 return NULL;
8414 }
8415
8416 for (i = 0; i < size; i++) {
8417 pages[i] = alloc_page(gfp_flags);
8418 if (!pages[i]) {
8419 dprintk("%s: failed to allocate page\n", __func__);
8420 nfs4_free_pages(pages, size);
8421 return NULL;
8422 }
8423 }
8424
8425 return pages;
8426}
8427
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008428static void nfs4_layoutget_release(void *calldata)
8429{
8430 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008431 struct inode *inode = lgp->args.inode;
8432 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008433 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008434
8435 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008436 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008437 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008438 put_nfs_open_context(lgp->args.ctx);
8439 kfree(calldata);
8440 dprintk("<-- %s\n", __func__);
8441}
8442
8443static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8444 .rpc_call_prepare = nfs4_layoutget_prepare,
8445 .rpc_call_done = nfs4_layoutget_done,
8446 .rpc_release = nfs4_layoutget_release,
8447};
8448
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008449struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008450nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008451{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008452 struct inode *inode = lgp->args.inode;
8453 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008454 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008455 struct rpc_task *task;
8456 struct rpc_message msg = {
8457 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8458 .rpc_argp = &lgp->args,
8459 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008460 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008461 };
8462 struct rpc_task_setup task_setup_data = {
8463 .rpc_client = server->client,
8464 .rpc_message = &msg,
8465 .callback_ops = &nfs4_layoutget_call_ops,
8466 .callback_data = lgp,
8467 .flags = RPC_TASK_ASYNC,
8468 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008469 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008470 struct nfs4_exception exception = {
8471 .inode = inode,
8472 .timeout = *timeout,
8473 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008474 int status = 0;
8475
8476 dprintk("--> %s\n", __func__);
8477
Peng Tao4bd5a982014-11-17 11:05:17 +08008478 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8479 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8480
Idan Kedar85541162012-08-02 11:47:10 +03008481 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8482 if (!lgp->args.layout.pages) {
8483 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008484 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008485 }
8486 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8487
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008488 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008489 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008490 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008491
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008492 task = rpc_run_task(&task_setup_data);
8493 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008494 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008495 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008496 if (status == 0) {
8497 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8498 *timeout = exception.timeout;
8499 }
8500
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008501 trace_nfs4_layoutget(lgp->args.ctx,
8502 &lgp->args.range,
8503 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008504 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008505 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008506
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008507 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8508 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008509 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008510 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008511 rpc_put_task(task);
8512 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008513 if (status)
8514 return ERR_PTR(status);
8515 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008516}
8517
Benny Halevycbe82602011-05-22 19:52:37 +03008518static void
8519nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8520{
8521 struct nfs4_layoutreturn *lrp = calldata;
8522
8523 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008524 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008525 &lrp->args.seq_args,
8526 &lrp->res.seq_res,
8527 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008528}
8529
8530static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8531{
8532 struct nfs4_layoutreturn *lrp = calldata;
8533 struct nfs_server *server;
8534
8535 dprintk("--> %s\n", __func__);
8536
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008537 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008538 return;
8539
8540 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008541 switch (task->tk_status) {
8542 default:
8543 task->tk_status = 0;
8544 case 0:
8545 break;
8546 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008547 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008548 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008549 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008550 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008551 return;
8552 }
Benny Halevycbe82602011-05-22 19:52:37 +03008553 dprintk("<-- %s\n", __func__);
8554}
8555
8556static void nfs4_layoutreturn_release(void *calldata)
8557{
8558 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008559 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008560
8561 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008562 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008563 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008564 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008565 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8566 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008567 pnfs_put_layout_hdr(lrp->args.layout);
8568 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008569 kfree(calldata);
8570 dprintk("<-- %s\n", __func__);
8571}
8572
8573static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8574 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8575 .rpc_call_done = nfs4_layoutreturn_done,
8576 .rpc_release = nfs4_layoutreturn_release,
8577};
8578
Peng Tao6c166052014-11-17 09:30:40 +08008579int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008580{
8581 struct rpc_task *task;
8582 struct rpc_message msg = {
8583 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8584 .rpc_argp = &lrp->args,
8585 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008586 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008587 };
8588 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008589 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008590 .rpc_message = &msg,
8591 .callback_ops = &nfs4_layoutreturn_call_ops,
8592 .callback_data = lrp,
8593 };
Peng Tao6c166052014-11-17 09:30:40 +08008594 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008595
Andrew Elble99ade3c2015-12-02 09:39:51 -05008596 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8597 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8598 &task_setup_data.rpc_client, &msg);
8599
Benny Halevycbe82602011-05-22 19:52:37 +03008600 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008601 if (!sync) {
8602 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8603 if (!lrp->inode) {
8604 nfs4_layoutreturn_release(lrp);
8605 return -EAGAIN;
8606 }
8607 task_setup_data.flags |= RPC_TASK_ASYNC;
8608 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008609 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008610 task = rpc_run_task(&task_setup_data);
8611 if (IS_ERR(task))
8612 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008613 if (sync)
8614 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008615 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008616 dprintk("<-- %s status=%d\n", __func__, status);
8617 rpc_put_task(task);
8618 return status;
8619}
8620
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008621static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008622_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8623 struct pnfs_device *pdev,
8624 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008625{
8626 struct nfs4_getdeviceinfo_args args = {
8627 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008628 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8629 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008630 };
8631 struct nfs4_getdeviceinfo_res res = {
8632 .pdev = pdev,
8633 };
8634 struct rpc_message msg = {
8635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8636 .rpc_argp = &args,
8637 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008638 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008639 };
8640 int status;
8641
8642 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008643 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008644 if (res.notification & ~args.notify_types)
8645 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008646 if (res.notification != args.notify_types)
8647 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008648
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008649 dprintk("<-- %s status=%d\n", __func__, status);
8650
8651 return status;
8652}
8653
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008654int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8655 struct pnfs_device *pdev,
8656 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008657{
8658 struct nfs4_exception exception = { };
8659 int err;
8660
8661 do {
8662 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008663 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008664 &exception);
8665 } while (exception.retry);
8666 return err;
8667}
8668EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8669
Andy Adamson863a3c62011-03-23 13:27:54 +00008670static void nfs4_layoutcommit_prepare(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
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008675 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008676 &data->args.seq_args,
8677 &data->res.seq_res,
8678 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008679}
8680
8681static void
8682nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8683{
8684 struct nfs4_layoutcommit_data *data = calldata;
8685 struct nfs_server *server = NFS_SERVER(data->args.inode);
8686
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008687 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008688 return;
8689
8690 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008691 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8692 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8693 case -NFS4ERR_BADLAYOUT: /* no layout */
8694 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008695 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008696 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008697 break;
8698 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008699 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008700 rpc_restart_call_prepare(task);
8701 return;
8702 }
8703 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008704}
8705
8706static void nfs4_layoutcommit_release(void *calldata)
8707{
8708 struct nfs4_layoutcommit_data *data = calldata;
8709
Andy Adamsondb29c082011-07-30 20:52:38 -04008710 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008711 nfs_post_op_update_inode_force_wcc(data->args.inode,
8712 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008713 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008714 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008715 kfree(data);
8716}
8717
8718static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8719 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8720 .rpc_call_done = nfs4_layoutcommit_done,
8721 .rpc_release = nfs4_layoutcommit_release,
8722};
8723
8724int
Andy Adamsonef311532011-03-12 02:58:10 -05008725nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008726{
8727 struct rpc_message msg = {
8728 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8729 .rpc_argp = &data->args,
8730 .rpc_resp = &data->res,
8731 .rpc_cred = data->cred,
8732 };
8733 struct rpc_task_setup task_setup_data = {
8734 .task = &data->task,
8735 .rpc_client = NFS_CLIENT(data->args.inode),
8736 .rpc_message = &msg,
8737 .callback_ops = &nfs4_layoutcommit_ops,
8738 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008739 };
8740 struct rpc_task *task;
8741 int status = 0;
8742
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008743 dprintk("NFS: initiating layoutcommit call. sync %d "
8744 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008745 data->args.lastbytewritten,
8746 data->args.inode->i_ino);
8747
Trond Myklebust472e2592015-02-05 16:50:30 -05008748 if (!sync) {
8749 data->inode = nfs_igrab_and_active(data->args.inode);
8750 if (data->inode == NULL) {
8751 nfs4_layoutcommit_release(data);
8752 return -EAGAIN;
8753 }
8754 task_setup_data.flags = RPC_TASK_ASYNC;
8755 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008756 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008757 task = rpc_run_task(&task_setup_data);
8758 if (IS_ERR(task))
8759 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008760 if (sync)
8761 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008762 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008763 dprintk("%s: status %d\n", __func__, status);
8764 rpc_put_task(task);
8765 return status;
8766}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008767
Andy Adamson97431202013-08-08 10:57:56 -04008768/**
8769 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8770 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8771 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008772static int
8773_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008774 struct nfs_fsinfo *info,
8775 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008776{
8777 struct nfs41_secinfo_no_name_args args = {
8778 .style = SECINFO_STYLE_CURRENT_FH,
8779 };
8780 struct nfs4_secinfo_res res = {
8781 .flavors = flavors,
8782 };
8783 struct rpc_message msg = {
8784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8785 .rpc_argp = &args,
8786 .rpc_resp = &res,
8787 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008788 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008789 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008790 int status;
8791
8792 if (use_integrity) {
8793 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008794 cred = nfs4_get_clid_cred(server->nfs_client);
8795 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008796 }
8797
8798 dprintk("--> %s\n", __func__);
8799 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8800 &res.seq_res, 0);
8801 dprintk("<-- %s status=%d\n", __func__, status);
8802
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008803 if (cred)
8804 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008805
8806 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008807}
8808
8809static int
8810nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8811 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8812{
8813 struct nfs4_exception exception = { };
8814 int err;
8815 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008816 /* first try using integrity protection */
8817 err = -NFS4ERR_WRONGSEC;
8818
8819 /* try to use integrity protection with machine cred */
8820 if (_nfs4_is_integrity_protected(server->nfs_client))
8821 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8822 flavors, true);
8823
8824 /*
8825 * if unable to use integrity protection, or SECINFO with
8826 * integrity protection returns NFS4ERR_WRONGSEC (which is
8827 * disallowed by spec, but exists in deployed servers) use
8828 * the current filesystem's rpc_client and the user cred.
8829 */
8830 if (err == -NFS4ERR_WRONGSEC)
8831 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8832 flavors, false);
8833
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008834 switch (err) {
8835 case 0:
8836 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008837 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008838 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008839 default:
8840 err = nfs4_handle_exception(server, err, &exception);
8841 }
8842 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008843out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008844 return err;
8845}
8846
8847static int
8848nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8849 struct nfs_fsinfo *info)
8850{
8851 int err;
8852 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008853 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008854 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008855 struct nfs4_secinfo4 *secinfo;
8856 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008857
8858 page = alloc_page(GFP_KERNEL);
8859 if (!page) {
8860 err = -ENOMEM;
8861 goto out;
8862 }
8863
8864 flavors = page_address(page);
8865 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8866
8867 /*
8868 * Fall back on "guess and check" method if
8869 * the server doesn't support SECINFO_NO_NAME
8870 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008871 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008872 err = nfs4_find_root_sec(server, fhandle, info);
8873 goto out_freepage;
8874 }
8875 if (err)
8876 goto out_freepage;
8877
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008878 for (i = 0; i < flavors->num_flavors; i++) {
8879 secinfo = &flavors->flavors[i];
8880
8881 switch (secinfo->flavor) {
8882 case RPC_AUTH_NULL:
8883 case RPC_AUTH_UNIX:
8884 case RPC_AUTH_GSS:
8885 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8886 &secinfo->flavor_info);
8887 break;
8888 default:
8889 flavor = RPC_AUTH_MAXFLAVOR;
8890 break;
8891 }
8892
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008893 if (!nfs_auth_info_match(&server->auth_info, flavor))
8894 flavor = RPC_AUTH_MAXFLAVOR;
8895
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008896 if (flavor != RPC_AUTH_MAXFLAVOR) {
8897 err = nfs4_lookup_root_sec(server, fhandle,
8898 info, flavor);
8899 if (!err)
8900 break;
8901 }
8902 }
8903
8904 if (flavor == RPC_AUTH_MAXFLAVOR)
8905 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008906
8907out_freepage:
8908 put_page(page);
8909 if (err == -EACCES)
8910 return -EPERM;
8911out:
8912 return err;
8913}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008914
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008915static int _nfs41_test_stateid(struct nfs_server *server,
8916 nfs4_stateid *stateid,
8917 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008918{
8919 int status;
8920 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008921 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008922 };
8923 struct nfs41_test_stateid_res res;
8924 struct rpc_message msg = {
8925 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8926 .rpc_argp = &args,
8927 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008928 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008929 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008930 struct rpc_clnt *rpc_client = server->client;
8931
8932 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8933 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008934
Chuck Lever38527b12012-07-11 16:30:23 -04008935 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008936 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008937 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008938 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008939 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008940 if (status != NFS_OK) {
8941 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008942 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008943 }
8944 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008945 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008946}
8947
Trond Myklebust43912bb2016-09-22 13:38:56 -04008948static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8949 int err, struct nfs4_exception *exception)
8950{
8951 exception->retry = 0;
8952 switch(err) {
8953 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008954 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008955 nfs4_handle_exception(server, err, exception);
8956 break;
8957 case -NFS4ERR_BADSESSION:
8958 case -NFS4ERR_BADSLOT:
8959 case -NFS4ERR_BAD_HIGH_SLOT:
8960 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8961 case -NFS4ERR_DEADSESSION:
8962 nfs4_do_handle_exception(server, err, exception);
8963 }
8964}
8965
Chuck Lever38527b12012-07-11 16:30:23 -04008966/**
8967 * nfs41_test_stateid - perform a TEST_STATEID operation
8968 *
8969 * @server: server / transport on which to perform the operation
8970 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008971 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008972 *
8973 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8974 * Otherwise a negative NFS4ERR value is returned if the operation
8975 * failed or the state ID is not currently valid.
8976 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008977static int nfs41_test_stateid(struct nfs_server *server,
8978 nfs4_stateid *stateid,
8979 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008980{
8981 struct nfs4_exception exception = { };
8982 int err;
8983 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008984 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04008985 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008986 } while (exception.retry);
8987 return err;
8988}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008989
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008990struct nfs_free_stateid_data {
8991 struct nfs_server *server;
8992 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008993 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008994};
8995
8996static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8997{
8998 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008999 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009000 &data->args.seq_args,
9001 &data->res.seq_res,
9002 task);
9003}
9004
9005static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9006{
9007 struct nfs_free_stateid_data *data = calldata;
9008
9009 nfs41_sequence_done(task, &data->res.seq_res);
9010
9011 switch (task->tk_status) {
9012 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009013 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009014 rpc_restart_call_prepare(task);
9015 }
9016}
9017
9018static void nfs41_free_stateid_release(void *calldata)
9019{
9020 kfree(calldata);
9021}
9022
Trond Myklebust17f26b12013-08-21 15:48:42 -04009023static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009024 .rpc_call_prepare = nfs41_free_stateid_prepare,
9025 .rpc_call_done = nfs41_free_stateid_done,
9026 .rpc_release = nfs41_free_stateid_release,
9027};
9028
9029static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009030 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009031 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009032 bool privileged)
9033{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009034 struct rpc_message msg = {
9035 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009036 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009037 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009038 struct rpc_task_setup task_setup = {
9039 .rpc_client = server->client,
9040 .rpc_message = &msg,
9041 .callback_ops = &nfs41_free_stateid_ops,
9042 .flags = RPC_TASK_ASYNC,
9043 };
9044 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009045
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009046 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9047 &task_setup.rpc_client, &msg);
9048
Chuck Lever38527b12012-07-11 16:30:23 -04009049 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009050 data = kmalloc(sizeof(*data), GFP_NOFS);
9051 if (!data)
9052 return ERR_PTR(-ENOMEM);
9053 data->server = server;
9054 nfs4_stateid_copy(&data->args.stateid, stateid);
9055
9056 task_setup.callback_data = data;
9057
9058 msg.rpc_argp = &data->args;
9059 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009060 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009061 if (privileged)
9062 nfs4_set_sequence_privileged(&data->args.seq_args);
9063
9064 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009065}
9066
Chuck Lever38527b12012-07-11 16:30:23 -04009067/**
9068 * nfs41_free_stateid - perform a FREE_STATEID operation
9069 *
9070 * @server: server / transport on which to perform the operation
9071 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009072 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009073 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009074 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009075 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009076 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009077static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009078 const nfs4_stateid *stateid,
9079 struct rpc_cred *cred,
9080 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009081{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009082 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009083
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009084 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009085 if (IS_ERR(task))
9086 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009087 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009088 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009089}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009090
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009091static void
9092nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009093{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009094 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009095
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009096 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009097 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009098}
9099
Trond Myklebust36281ca2012-03-04 18:13:56 -05009100static bool nfs41_match_stateid(const nfs4_stateid *s1,
9101 const nfs4_stateid *s2)
9102{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009103 if (s1->type != s2->type)
9104 return false;
9105
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009106 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009107 return false;
9108
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009109 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009110 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009111
Anna Schumaker045c5512017-01-11 16:59:48 -05009112 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009113}
9114
Andy Adamson557134a2009-04-01 09:21:53 -04009115#endif /* CONFIG_NFS_V4_1 */
9116
Trond Myklebust36281ca2012-03-04 18:13:56 -05009117static bool nfs4_match_stateid(const nfs4_stateid *s1,
9118 const nfs4_stateid *s2)
9119{
Trond Myklebustf597c532012-03-04 18:13:56 -05009120 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009121}
9122
9123
Trond Myklebust17280172012-03-11 13:11:00 -04009124static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009125 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009126 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009127 .recover_open = nfs4_open_reclaim,
9128 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009129 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009130 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009131};
9132
Andy Adamson591d71c2009-04-01 09:22:47 -04009133#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009134static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009135 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9136 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9137 .recover_open = nfs4_open_reclaim,
9138 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009139 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009140 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009141 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009142};
9143#endif /* CONFIG_NFS_V4_1 */
9144
Trond Myklebust17280172012-03-11 13:11:00 -04009145static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009146 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009147 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009148 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009149 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009150 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009151};
9152
Andy Adamson591d71c2009-04-01 09:22:47 -04009153#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009154static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009155 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9156 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009157 .recover_open = nfs41_open_expired,
9158 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009159 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009160};
9161#endif /* CONFIG_NFS_V4_1 */
9162
Trond Myklebust17280172012-03-11 13:11:00 -04009163static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009164 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009165 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009166 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009167};
9168
9169#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009170static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009171 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009172 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009173 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009174};
9175#endif
9176
Chuck Leverec011fe2013-10-17 14:12:39 -04009177static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009178 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009179 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009180};
9181
9182#if defined(CONFIG_NFS_V4_1)
9183static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009184 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009185 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009186};
9187#endif /* CONFIG_NFS_V4_1 */
9188
Trond Myklebust97dc1352010-06-16 09:52:26 -04009189static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9190 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009191 .init_caps = NFS_CAP_READDIRPLUS
9192 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009193 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009194 .init_client = nfs40_init_client,
9195 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009196 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009197 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009198 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009199 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009200 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009201 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009202 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9203 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9204 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009205 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009206};
9207
9208#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009209static struct nfs_seqid *
9210nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9211{
9212 return NULL;
9213}
9214
Trond Myklebust97dc1352010-06-16 09:52:26 -04009215static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9216 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009217 .init_caps = NFS_CAP_READDIRPLUS
9218 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009219 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009220 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009221 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009222 .init_client = nfs41_init_client,
9223 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009224 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009225 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009226 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009227 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009228 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009229 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009230 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009231 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9232 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9233 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009234 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009235};
9236#endif
9237
Steve Dickson42c2c422013-05-22 12:50:38 -04009238#if defined(CONFIG_NFS_V4_2)
9239static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9240 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009241 .init_caps = NFS_CAP_READDIRPLUS
9242 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009243 | NFS_CAP_POSIX_LOCK
9244 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009245 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009246 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009247 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009248 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009249 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009250 | NFS_CAP_LAYOUTSTATS
9251 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009252 .init_client = nfs41_init_client,
9253 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009254 .match_stateid = nfs41_match_stateid,
9255 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009256 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009257 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009258 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009259 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009260 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009261 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9262 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9263 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009264 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009265};
9266#endif
9267
Trond Myklebust97dc1352010-06-16 09:52:26 -04009268const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9269 [0] = &nfs_v4_0_minor_ops,
9270#if defined(CONFIG_NFS_V4_1)
9271 [1] = &nfs_v4_1_minor_ops,
9272#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009273#if defined(CONFIG_NFS_V4_2)
9274 [2] = &nfs_v4_2_minor_ops,
9275#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009276};
9277
Trond Myklebust13997822016-07-24 17:10:52 -04009278static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009279{
9280 ssize_t error, error2;
9281
9282 error = generic_listxattr(dentry, list, size);
9283 if (error < 0)
9284 return error;
9285 if (list) {
9286 list += error;
9287 size -= error;
9288 }
9289
9290 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9291 if (error2 < 0)
9292 return error2;
9293 return error + error2;
9294}
9295
Trond Myklebust17f26b12013-08-21 15:48:42 -04009296static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009297 .create = nfs_create,
9298 .lookup = nfs_lookup,
9299 .atomic_open = nfs_atomic_open,
9300 .link = nfs_link,
9301 .unlink = nfs_unlink,
9302 .symlink = nfs_symlink,
9303 .mkdir = nfs_mkdir,
9304 .rmdir = nfs_rmdir,
9305 .mknod = nfs_mknod,
9306 .rename = nfs_rename,
9307 .permission = nfs_permission,
9308 .getattr = nfs_getattr,
9309 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009310 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009311};
9312
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009313static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009314 .permission = nfs_permission,
9315 .getattr = nfs_getattr,
9316 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009317 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009318};
9319
David Howells509de812006-08-22 20:06:11 -04009320const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009321 .version = 4, /* protocol version */
9322 .dentry_ops = &nfs4_dentry_operations,
9323 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009324 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009325 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009326 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009327 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009328 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009329 .getattr = nfs4_proc_getattr,
9330 .setattr = nfs4_proc_setattr,
9331 .lookup = nfs4_proc_lookup,
9332 .access = nfs4_proc_access,
9333 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009334 .create = nfs4_proc_create,
9335 .remove = nfs4_proc_remove,
9336 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009337 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009338 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009339 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009340 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009341 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009342 .link = nfs4_proc_link,
9343 .symlink = nfs4_proc_symlink,
9344 .mkdir = nfs4_proc_mkdir,
9345 .rmdir = nfs4_proc_remove,
9346 .readdir = nfs4_proc_readdir,
9347 .mknod = nfs4_proc_mknod,
9348 .statfs = nfs4_proc_statfs,
9349 .fsinfo = nfs4_proc_fsinfo,
9350 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009351 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009352 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009353 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009354 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009355 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009356 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009357 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009358 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009359 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009360 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009361 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009362 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009363 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009364 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009365 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009366 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009367 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009368 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009369 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009370 .create_server = nfs4_create_server,
9371 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009372};
9373
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009374static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009375 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009376 .list = nfs4_xattr_list_nfs4_acl,
9377 .get = nfs4_xattr_get_nfs4_acl,
9378 .set = nfs4_xattr_set_nfs4_acl,
9379};
9380
9381const struct xattr_handler *nfs4_xattr_handlers[] = {
9382 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009383#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9384 &nfs4_xattr_nfs4_label_handler,
9385#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009386 NULL
9387};
9388
Linus Torvalds1da177e2005-04-16 15:20:36 -07009389/*
9390 * Local variables:
9391 * c-basic-offset: 8
9392 * End:
9393 */