blob: 031b867aaefa878201f3ce96bfe5ac13c3359dea [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 Myklebust2cf10cd2016-12-04 19:26:40 -0500771 case -NFS4ERR_DEADSESSION:
772 case -NFS4ERR_BADSESSION:
773 nfs4_schedule_session_recovery(session, res->sr_status);
774 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400775 default:
776 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400777 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400778 }
779out:
780 /* The session may be reset by one of the error handlers. */
781 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500782out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500783 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500784retry_nowait:
785 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400786 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500787 task->tk_status = 0;
788 ret = 0;
789 }
790 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400791out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400792 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400793 goto out;
794 rpc_delay(task, NFS4_POLL_RETRY_MAX);
795 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400796}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400797
798int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
799{
800 if (!nfs41_sequence_process(task, res))
801 return 0;
802 if (res->sr_slot != NULL)
803 nfs41_sequence_free_slot(res);
804 return 1;
805
806}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500807EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400808
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400809static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
810{
811 if (res->sr_slot == NULL)
812 return 1;
813 if (res->sr_slot->table->session != NULL)
814 return nfs41_sequence_process(task, res);
815 return nfs40_sequence_done(task, res);
816}
817
818static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
819{
820 if (res->sr_slot != NULL) {
821 if (res->sr_slot->table->session != NULL)
822 nfs41_sequence_free_slot(res);
823 else
824 nfs40_sequence_free_slot(res);
825 }
826}
827
Peng Tao2c4b1312014-06-11 05:24:16 +0800828int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400829{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500830 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400831 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400832 if (!res->sr_slot->table->session)
833 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400834 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400835}
Peng Tao2c4b1312014-06-11 05:24:16 +0800836EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400837
Andy Adamsonce5039c2009-04-01 09:22:13 -0400838static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
839{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400840 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400841
Trond Myklebust035168ab2010-06-16 09:52:26 -0400842 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
843
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500844 nfs4_setup_sequence(data->seq_server->nfs_client,
845 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400846}
847
Andy Adamson69ab40c2009-04-01 09:22:19 -0400848static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
849{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400850 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400851
Trond Myklebust14516c32010-07-31 14:29:06 -0400852 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400853}
854
Trond Myklebust17280172012-03-11 13:11:00 -0400855static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400856 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400857 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400858};
859
Chuck Lever3bd23842013-08-09 12:49:19 -0400860#else /* !CONFIG_NFS_V4_1 */
861
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400862static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
863{
864 return nfs40_sequence_done(task, res);
865}
866
867static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
868{
869 if (res->sr_slot != NULL)
870 nfs40_sequence_free_slot(res);
871}
872
Peng Tao2c4b1312014-06-11 05:24:16 +0800873int nfs4_sequence_done(struct rpc_task *task,
874 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400875{
Chuck Lever3bd23842013-08-09 12:49:19 -0400876 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400877}
Peng Tao2c4b1312014-06-11 05:24:16 +0800878EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400879
880#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400881
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500882int nfs4_setup_sequence(const struct nfs_client *client,
883 struct nfs4_sequence_args *args,
884 struct nfs4_sequence_res *res,
885 struct rpc_task *task)
886{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500887 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500888 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500889 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500890
Anna Schumaker9dd91072017-01-10 12:01:46 -0500891 /* slot already allocated? */
892 if (res->sr_slot != NULL)
893 goto out_start;
894
Anna Schumaker76ee0352017-01-10 16:49:31 -0500895 if (session) {
896 tbl = &session->fc_slot_table;
897 task->tk_timeout = 0;
898 }
899
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500900 spin_lock(&tbl->slot_tbl_lock);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500901 /* The state manager will wait until the slot table is empty */
902 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
903 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500904
Anna Schumaker3d358082017-01-11 10:54:04 -0500905 slot = nfs4_alloc_slot(tbl);
906 if (IS_ERR(slot)) {
907 /* Try again in 1/4 second */
908 if (slot == ERR_PTR(-ENOMEM))
909 task->tk_timeout = HZ >> 2;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500910 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -0500911 }
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500912 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500913
Anna Schumaker3d358082017-01-11 10:54:04 -0500914 slot->privileged = args->sa_privileged ? 1 : 0;
915 args->sa_slot = slot;
916
917 res->sr_slot = slot;
918 if (session) {
919 res->sr_timestamp = jiffies;
920 res->sr_status_flags = 0;
921 res->sr_status = 1;
Anna Schumaker3d358082017-01-11 10:54:04 -0500922 }
923
Anna Schumakerad05cc02017-01-11 13:37:06 -0500924 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd91072017-01-10 12:01:46 -0500925out_start:
926 rpc_call_start(task);
927 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500928
929out_sleep:
930 if (args->sa_privileged)
931 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
932 NULL, RPC_PRIORITY_PRIVILEGED);
933 else
934 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
935 spin_unlock(&tbl->slot_tbl_lock);
936 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500937}
938EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
939
Chuck Lever9915ea72013-08-09 12:48:27 -0400940static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
941{
942 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500943 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -0400944 data->seq_args, data->seq_res, task);
945}
946
947static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
948{
949 struct nfs4_call_sync_data *data = calldata;
950 nfs4_sequence_done(task, data->seq_res);
951}
952
953static const struct rpc_call_ops nfs40_call_sync_ops = {
954 .rpc_call_prepare = nfs40_call_sync_prepare,
955 .rpc_call_done = nfs40_call_sync_done,
956};
957
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400958static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400959 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500960 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100961 struct nfs4_sequence_args *args,
962 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400963{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100964 int ret;
965 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400966 struct nfs_client *clp = server->nfs_client;
967 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100968 .seq_server = server,
969 .seq_args = args,
970 .seq_res = res,
971 };
972 struct rpc_task_setup task_setup = {
973 .rpc_client = clnt,
974 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400975 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100976 .callback_data = &data
977 };
978
979 task = rpc_run_task(&task_setup);
980 if (IS_ERR(task))
981 ret = PTR_ERR(task);
982 else {
983 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400984 rpc_put_task(task);
985 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100986 return ret;
987}
988
Bryan Schumaker7c513052011-03-24 17:12:24 +0000989int nfs4_call_sync(struct rpc_clnt *clnt,
990 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000991 struct rpc_message *msg,
992 struct nfs4_sequence_args *args,
993 struct nfs4_sequence_res *res,
994 int cache_reply)
995{
Chuck Levera9c92d62013-08-09 12:48:18 -0400996 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400997 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000998}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001000static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1001 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 struct nfs_inode *nfsi = NFS_I(dir);
1004
1005 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001006 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001007 if (cinfo->atomic && cinfo->before == dir->i_version) {
1008 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1009 nfsi->attrtimeo_timestamp = jiffies;
1010 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 nfs_force_lookup_revalidate(dir);
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001012 if (cinfo->before != dir->i_version)
1013 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1014 NFS_INO_INVALID_ACL;
1015 }
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001016 dir->i_version = cinfo->after;
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001017 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001018 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001019 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 spin_unlock(&dir->i_lock);
1021}
1022
1023struct nfs4_opendata {
1024 struct kref kref;
1025 struct nfs_openargs o_arg;
1026 struct nfs_openres o_res;
1027 struct nfs_open_confirmargs c_arg;
1028 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001029 struct nfs4_string owner_name;
1030 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001031 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001033 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001035 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 struct nfs4_state_owner *owner;
1037 struct nfs4_state *state;
1038 struct iattr attrs;
1039 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001040 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001041 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001042 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001043 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 int cancelled;
1045};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001046
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001047static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1048 int err, struct nfs4_exception *exception)
1049{
1050 if (err != -EINVAL)
1051 return false;
1052 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1053 return false;
1054 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1055 exception->retry = 1;
1056 return true;
1057}
1058
Trond Myklebust6ae37332015-01-30 14:21:14 -05001059static u32
1060nfs4_map_atomic_open_share(struct nfs_server *server,
1061 fmode_t fmode, int openflags)
1062{
1063 u32 res = 0;
1064
1065 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1066 case FMODE_READ:
1067 res = NFS4_SHARE_ACCESS_READ;
1068 break;
1069 case FMODE_WRITE:
1070 res = NFS4_SHARE_ACCESS_WRITE;
1071 break;
1072 case FMODE_READ|FMODE_WRITE:
1073 res = NFS4_SHARE_ACCESS_BOTH;
1074 }
1075 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1076 goto out;
1077 /* Want no delegation if we're using O_DIRECT */
1078 if (openflags & O_DIRECT)
1079 res |= NFS4_SHARE_WANT_NO_DELEG;
1080out:
1081 return res;
1082}
1083
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001084static enum open_claim_type4
1085nfs4_map_atomic_open_claim(struct nfs_server *server,
1086 enum open_claim_type4 claim)
1087{
1088 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1089 return claim;
1090 switch (claim) {
1091 default:
1092 return claim;
1093 case NFS4_OPEN_CLAIM_FH:
1094 return NFS4_OPEN_CLAIM_NULL;
1095 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1096 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1097 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1098 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1099 }
1100}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001103{
1104 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001105 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001106 p->o_res.seqid = p->o_arg.seqid;
1107 p->c_res.seqid = p->c_arg.seqid;
1108 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001109 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001110 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001111 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001112}
1113
Al Viro82a2c1b2011-06-22 18:30:55 -04001114static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001115 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001116 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001117 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001118 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001119 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001120{
Al Viro82a2c1b2011-06-22 18:30:55 -04001121 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001122 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001123 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001124 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001125 struct nfs4_opendata *p;
1126
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001127 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001128 if (p == NULL)
1129 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001130
1131 p->f_label = nfs4_label_alloc(server, gfp_mask);
1132 if (IS_ERR(p->f_label))
1133 goto err_free_p;
1134
Kinglong Meea49c2692015-07-27 15:31:38 +08001135 p->a_label = nfs4_label_alloc(server, gfp_mask);
1136 if (IS_ERR(p->a_label))
1137 goto err_free_f;
1138
Trond Myklebust63f5f792015-01-23 19:19:25 -05001139 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1140 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001141 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001142 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001143 nfs_sb_active(dentry->d_sb);
1144 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001145 p->dir = parent;
1146 p->owner = sp;
1147 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001148 p->o_arg.open_flags = flags;
1149 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001150 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001151 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001152 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1153 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001154 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1155 * will return permission denied for all bits until close */
1156 if (!(flags & O_EXCL)) {
1157 /* ask server to check for all possible rights as results
1158 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001159 switch (p->o_arg.claim) {
1160 default:
1161 break;
1162 case NFS4_OPEN_CLAIM_NULL:
1163 case NFS4_OPEN_CLAIM_FH:
1164 p->o_arg.access = NFS4_ACCESS_READ |
1165 NFS4_ACCESS_MODIFY |
1166 NFS4_ACCESS_EXTEND |
1167 NFS4_ACCESS_EXECUTE;
1168 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001169 }
David Howells7539bba2006-08-22 20:06:09 -04001170 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001171 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1172 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001173 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001174 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001175 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001176 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001177 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001178 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001179 case NFS4_OPEN_CLAIM_NULL:
1180 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1181 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1182 p->o_arg.fh = NFS_FH(dir);
1183 break;
1184 case NFS4_OPEN_CLAIM_PREVIOUS:
1185 case NFS4_OPEN_CLAIM_FH:
1186 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1187 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001188 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001189 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001190 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001191 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001192
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001193 p->o_arg.u.attrs = &p->attrs;
1194 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001195
1196 verf[0] = jiffies;
1197 verf[1] = current->pid;
1198 memcpy(p->o_arg.u.verifier.data, verf,
1199 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001200 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001201 p->c_arg.fh = &p->o_res.fh;
1202 p->c_arg.stateid = &p->o_res.stateid;
1203 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001204 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001205 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001206 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001207
1208err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001209 nfs4_label_free(p->a_label);
1210err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001211 nfs4_label_free(p->f_label);
1212err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001213 kfree(p);
1214err:
1215 dput(parent);
1216 return NULL;
1217}
1218
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001219static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001220{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001221 struct nfs4_opendata *p = container_of(kref,
1222 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001223 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001224
1225 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001226 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001227 if (p->state != NULL)
1228 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001229 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001230
Kinglong Meea49c2692015-07-27 15:31:38 +08001231 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001232 nfs4_label_free(p->f_label);
1233
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001234 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001235 dput(p->dentry);
1236 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001237 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001238 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001239 kfree(p);
1240}
1241
1242static void nfs4_opendata_put(struct nfs4_opendata *p)
1243{
1244 if (p != NULL)
1245 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001246}
1247
Trond Myklebust24311f82015-09-20 10:50:17 -04001248static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1249 fmode_t fmode)
1250{
1251 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1252 case FMODE_READ|FMODE_WRITE:
1253 return state->n_rdwr != 0;
1254 case FMODE_WRITE:
1255 return state->n_wronly != 0;
1256 case FMODE_READ:
1257 return state->n_rdonly != 0;
1258 }
1259 WARN_ON_ONCE(1);
1260 return false;
1261}
1262
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001263static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001264{
1265 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001266
Trond Myklebust536e43d2012-01-17 22:04:26 -05001267 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001268 goto out;
1269 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001270 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001271 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1272 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001273 break;
1274 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001275 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1276 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001277 break;
1278 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001279 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1280 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001281 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001282out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001283 return ret;
1284}
1285
Trond Myklebust2a606182015-08-19 22:30:00 -05001286static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1287 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001288{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001289 if (delegation == NULL)
1290 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001291 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001292 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001293 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1294 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001295 switch (claim) {
1296 case NFS4_OPEN_CLAIM_NULL:
1297 case NFS4_OPEN_CLAIM_FH:
1298 break;
1299 case NFS4_OPEN_CLAIM_PREVIOUS:
1300 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1301 break;
1302 default:
1303 return 0;
1304 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001305 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001306 return 1;
1307}
1308
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001309static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001310{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001311 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001312 case FMODE_WRITE:
1313 state->n_wronly++;
1314 break;
1315 case FMODE_READ:
1316 state->n_rdonly++;
1317 break;
1318 case FMODE_READ|FMODE_WRITE:
1319 state->n_rdwr++;
1320 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001321 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001322}
1323
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001324#ifdef CONFIG_NFS_V4_1
1325static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1326{
1327 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1328 return true;
1329 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1330 return true;
1331 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1332 return true;
1333 return false;
1334}
1335#endif /* CONFIG_NFS_V4_1 */
1336
Trond Myklebust4f14c192014-02-12 19:15:06 -05001337static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001338{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001339 struct nfs_client *clp = state->owner->so_server->nfs_client;
1340 bool need_recover = false;
1341
1342 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1343 need_recover = true;
1344 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1345 need_recover = true;
1346 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1347 need_recover = true;
1348 if (need_recover)
1349 nfs4_state_mark_reclaim_nograce(clp, state);
1350}
1351
Trond Myklebuste999e802014-02-10 18:20:47 -05001352static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001353 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001354{
1355 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1356 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001357 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001358 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001359 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001360 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001361 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001362 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1363 return true;
1364 return false;
1365}
1366
Trond Myklebustf95549c2015-01-23 18:06:09 -05001367static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1368{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001369 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1370 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001371 if (state->n_wronly)
1372 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1373 if (state->n_rdonly)
1374 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1375 if (state->n_rdwr)
1376 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001377 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001378}
1379
Trond Myklebust226056c2014-02-11 10:41:07 -05001380static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1381 nfs4_stateid *stateid, fmode_t fmode)
1382{
1383 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1384 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1385 case FMODE_WRITE:
1386 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1387 break;
1388 case FMODE_READ:
1389 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1390 break;
1391 case 0:
1392 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1393 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1394 clear_bit(NFS_OPEN_STATE, &state->flags);
1395 }
1396 if (stateid == NULL)
1397 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001398 /* Handle OPEN+OPEN_DOWNGRADE races */
1399 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1400 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001401 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001402 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001403 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001404 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001405 nfs4_stateid_copy(&state->stateid, stateid);
1406 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001407}
1408
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001409static void nfs_clear_open_stateid(struct nfs4_state *state,
1410 nfs4_stateid *arg_stateid,
1411 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001412{
1413 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001414 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1415 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1416 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001417 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001418 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1419 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001420}
1421
Trond Myklebust1393d962016-09-22 13:39:13 -04001422static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1423 const nfs4_stateid *stateid, fmode_t fmode,
1424 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001425{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001426 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001427 case FMODE_READ:
1428 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1429 break;
1430 case FMODE_WRITE:
1431 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1432 break;
1433 case FMODE_READ|FMODE_WRITE:
1434 set_bit(NFS_O_RDWR_STATE, &state->flags);
1435 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001436 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001437 return;
1438 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1439 nfs4_stateid_copy(&state->stateid, stateid);
1440 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001441}
1442
Trond Myklebust1393d962016-09-22 13:39:13 -04001443static void __update_open_stateid(struct nfs4_state *state,
1444 const nfs4_stateid *open_stateid,
1445 const nfs4_stateid *deleg_stateid,
1446 fmode_t fmode,
1447 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001449 /*
1450 * Protect the call to nfs4_state_set_mode_locked and
1451 * serialise the stateid update
1452 */
Andrew Elble361cad32015-12-02 09:20:57 -05001453 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001454 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001455 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001456 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001457 set_bit(NFS_DELEGATED_STATE, &state->flags);
1458 }
1459 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001460 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001461 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001462 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001463 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Trond Myklebust1393d962016-09-22 13:39:13 -04001466static int update_open_stateid(struct nfs4_state *state,
1467 const nfs4_stateid *open_stateid,
1468 const nfs4_stateid *delegation,
1469 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001470{
Trond Myklebust1393d962016-09-22 13:39:13 -04001471 struct nfs_server *server = NFS_SERVER(state->inode);
1472 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001473 struct nfs_inode *nfsi = NFS_I(state->inode);
1474 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001475 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001476 int ret = 0;
1477
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001478 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001479
1480 rcu_read_lock();
1481 deleg_cur = rcu_dereference(nfsi->delegation);
1482 if (deleg_cur == NULL)
1483 goto no_delegation;
1484
1485 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001486 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001487 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001488 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001489 goto no_delegation_unlock;
1490
1491 if (delegation == NULL)
1492 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001493 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001494 goto no_delegation_unlock;
1495
Trond Myklebustb7391f42008-12-23 15:21:52 -05001496 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001497 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1498 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001499 ret = 1;
1500no_delegation_unlock:
1501 spin_unlock(&deleg_cur->lock);
1502no_delegation:
1503 rcu_read_unlock();
1504
1505 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001506 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001507 ret = 1;
1508 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001509 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001510 nfs4_schedule_state_manager(clp);
1511 if (freeme.type != 0)
1512 nfs4_test_and_free_stateid(server, &freeme,
1513 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001514
1515 return ret;
1516}
1517
Trond Myklebust39071e62015-01-24 15:07:56 -05001518static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1519 const nfs4_stateid *stateid)
1520{
1521 struct nfs4_state *state = lsp->ls_state;
1522 bool ret = false;
1523
1524 spin_lock(&state->state_lock);
1525 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1526 goto out_noupdate;
1527 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1528 goto out_noupdate;
1529 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1530 ret = true;
1531out_noupdate:
1532 spin_unlock(&state->state_lock);
1533 return ret;
1534}
Trond Myklebust34310432008-12-23 15:21:38 -05001535
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001536static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001537{
1538 struct nfs_delegation *delegation;
1539
1540 rcu_read_lock();
1541 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001542 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001543 rcu_read_unlock();
1544 return;
1545 }
1546 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001547 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001548}
1549
Trond Myklebust6ee41262007-07-08 14:11:36 -04001550static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001551{
1552 struct nfs4_state *state = opendata->state;
1553 struct nfs_inode *nfsi = NFS_I(state->inode);
1554 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001555 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001556 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001557 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001558 nfs4_stateid stateid;
1559 int ret = -EAGAIN;
1560
Trond Myklebustaac00a82007-07-05 19:02:21 -04001561 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001562 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001563 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001564 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001565 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001566 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001567 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001568 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001569 rcu_read_lock();
1570 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001571 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001572 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001573 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001574 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001575 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001576 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001577 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001578 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001579 if (!opendata->is_recover) {
1580 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1581 if (ret != 0)
1582 goto out;
1583 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001584 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001585
1586 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001587 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001588 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001589 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001590out:
1591 return ERR_PTR(ret);
1592out_return_state:
1593 atomic_inc(&state->count);
1594 return state;
1595}
1596
Andy Adamsone23008e2012-10-02 21:07:32 -04001597static void
1598nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1599{
1600 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1601 struct nfs_delegation *delegation;
1602 int delegation_flags = 0;
1603
1604 rcu_read_lock();
1605 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1606 if (delegation)
1607 delegation_flags = delegation->flags;
1608 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001609 switch (data->o_arg.claim) {
1610 default:
1611 break;
1612 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1613 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001614 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1615 "returning a delegation for "
1616 "OPEN(CLAIM_DELEGATE_CUR)\n",
1617 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001618 return;
1619 }
1620 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001621 nfs_inode_set_delegation(state->inode,
1622 data->owner->so_cred,
1623 &data->o_res);
1624 else
1625 nfs_inode_reclaim_delegation(state->inode,
1626 data->owner->so_cred,
1627 &data->o_res);
1628}
1629
1630/*
1631 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1632 * and update the nfs4_state.
1633 */
1634static struct nfs4_state *
1635_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1636{
1637 struct inode *inode = data->state->inode;
1638 struct nfs4_state *state = data->state;
1639 int ret;
1640
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001641 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001642 if (data->rpc_status)
1643 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001644 /* cached opens have already been processed */
1645 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001646 }
1647
Andy Adamsone23008e2012-10-02 21:07:32 -04001648 ret = nfs_refresh_inode(inode, &data->f_attr);
1649 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001650 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001651
1652 if (data->o_res.delegation_type != 0)
1653 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001654update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001655 update_open_stateid(state, &data->o_res.stateid, NULL,
1656 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001657 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001658
1659 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001660}
1661
1662static struct nfs4_state *
1663_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001664{
1665 struct inode *inode;
1666 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001667 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001668
Trond Myklebustaac00a82007-07-05 19:02:21 -04001669 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001670 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001671 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001672 goto out;
1673 }
1674
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001675 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001676 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001677 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001678 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001679 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001680 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001681 goto err;
1682 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001683 state = nfs4_get_open_state(inode, data->owner);
1684 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001685 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001686 if (data->o_res.delegation_type != 0)
1687 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001688 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001689 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001690 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001691out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001692 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001693 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001694err_put_inode:
1695 iput(inode);
1696err:
1697 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001698}
1699
Andy Adamsone23008e2012-10-02 21:07:32 -04001700static struct nfs4_state *
1701nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1702{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001703 struct nfs4_state *ret;
1704
Andy Adamsone23008e2012-10-02 21:07:32 -04001705 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001706 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1707 else
1708 ret = _nfs4_opendata_to_nfs4_state(data);
1709 nfs4_sequence_free_slot(&data->o_res.seq_res);
1710 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001711}
1712
Trond Myklebust864472e2006-01-03 09:55:15 +01001713static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1714{
1715 struct nfs_inode *nfsi = NFS_I(state->inode);
1716 struct nfs_open_context *ctx;
1717
1718 spin_lock(&state->inode->i_lock);
1719 list_for_each_entry(ctx, &nfsi->open_files, list) {
1720 if (ctx->state != state)
1721 continue;
1722 get_nfs_open_context(ctx);
1723 spin_unlock(&state->inode->i_lock);
1724 return ctx;
1725 }
1726 spin_unlock(&state->inode->i_lock);
1727 return ERR_PTR(-ENOENT);
1728}
1729
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001730static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1731 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001732{
1733 struct nfs4_opendata *opendata;
1734
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001735 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001736 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001737 if (opendata == NULL)
1738 return ERR_PTR(-ENOMEM);
1739 opendata->state = state;
1740 atomic_inc(&state->count);
1741 return opendata;
1742}
1743
Trond Myklebust24311f82015-09-20 10:50:17 -04001744static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1745 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001746{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001747 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001748 int ret;
1749
Trond Myklebust24311f82015-09-20 10:50:17 -04001750 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001751 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001752 opendata->o_arg.open_flags = 0;
1753 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001754 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1755 NFS_SB(opendata->dentry->d_sb),
1756 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001757 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1758 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1759 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001760 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001761 if (ret != 0)
1762 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001763 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001764 if (IS_ERR(newstate))
1765 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001766 if (newstate != opendata->state)
1767 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001768 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001769 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001770}
1771
1772static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1773{
Trond Myklebust864472e2006-01-03 09:55:15 +01001774 int ret;
1775
Trond Myklebust4f14c192014-02-12 19:15:06 -05001776 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1777 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1778 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1779 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001780 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001781 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001782 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001783 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001784 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1785 if (ret != 0)
1786 return ret;
1787 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1788 if (ret != 0)
1789 return ret;
1790 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1791 if (ret != 0)
1792 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001793 /*
1794 * We may have performed cached opens for all three recoveries.
1795 * Check if we need to update the current stateid.
1796 */
1797 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001798 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001799 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001800 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001801 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001802 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001803 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001804 return 0;
1805}
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807/*
1808 * OPEN_RECLAIM:
1809 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001811static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001813 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001814 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001815 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 int status;
1817
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001818 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1819 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001820 if (IS_ERR(opendata))
1821 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001822 rcu_read_lock();
1823 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001824 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001825 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001826 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001827 opendata->o_arg.u.delegation_type = delegation_type;
1828 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001829 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return status;
1831}
1832
Trond Myklebust539cd032007-06-05 11:46:42 -04001833static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
1835 struct nfs_server *server = NFS_SERVER(state->inode);
1836 struct nfs4_exception exception = { };
1837 int err;
1838 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001839 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001840 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001841 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1842 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001843 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001844 break;
1845 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 } while (exception.retry);
1847 return err;
1848}
1849
Trond Myklebust864472e2006-01-03 09:55:15 +01001850static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1851{
1852 struct nfs_open_context *ctx;
1853 int ret;
1854
1855 ctx = nfs4_state_find_open_context(state);
1856 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001857 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001858 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001859 put_nfs_open_context(ctx);
1860 return ret;
1861}
1862
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001863static 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 -07001864{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001865 switch (err) {
1866 default:
1867 printk(KERN_ERR "NFS: %s: unhandled error "
1868 "%d.\n", __func__, err);
1869 case 0:
1870 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001871 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001872 case -ESTALE:
1873 break;
1874 case -NFS4ERR_BADSESSION:
1875 case -NFS4ERR_BADSLOT:
1876 case -NFS4ERR_BAD_HIGH_SLOT:
1877 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1878 case -NFS4ERR_DEADSESSION:
1879 set_bit(NFS_DELEGATED_STATE, &state->flags);
1880 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1881 return -EAGAIN;
1882 case -NFS4ERR_STALE_CLIENTID:
1883 case -NFS4ERR_STALE_STATEID:
1884 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001885 /* Don't recall a delegation if it was lost */
1886 nfs4_schedule_lease_recovery(server->nfs_client);
1887 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001888 case -NFS4ERR_MOVED:
1889 nfs4_schedule_migration_recovery(server);
1890 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001891 case -NFS4ERR_LEASE_MOVED:
1892 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1893 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001894 case -NFS4ERR_DELEG_REVOKED:
1895 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001896 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001897 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001898 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001899 nfs_inode_find_state_and_recover(state->inode,
1900 stateid);
1901 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001902 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001903 case -NFS4ERR_DELAY:
1904 case -NFS4ERR_GRACE:
1905 set_bit(NFS_DELEGATED_STATE, &state->flags);
1906 ssleep(1);
1907 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001908 case -ENOMEM:
1909 case -NFS4ERR_DENIED:
1910 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1911 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return err;
1914}
1915
Trond Myklebust24311f82015-09-20 10:50:17 -04001916int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1917 struct nfs4_state *state, const nfs4_stateid *stateid,
1918 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001919{
1920 struct nfs_server *server = NFS_SERVER(state->inode);
1921 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001922 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001923
1924 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1925 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1926 if (IS_ERR(opendata))
1927 return PTR_ERR(opendata);
1928 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001929 write_seqlock(&state->seqlock);
1930 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1931 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001932 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1933 switch (type & (FMODE_READ|FMODE_WRITE)) {
1934 case FMODE_READ|FMODE_WRITE:
1935 case FMODE_WRITE:
1936 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1937 if (err)
1938 break;
1939 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1940 if (err)
1941 break;
1942 case FMODE_READ:
1943 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1944 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001945 nfs4_opendata_put(opendata);
1946 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1947}
1948
Chuck Leverbe05c862013-08-09 12:49:47 -04001949static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1950{
1951 struct nfs4_opendata *data = calldata;
1952
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001953 nfs4_setup_sequence(data->o_arg.server->nfs_client,
1954 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001955}
1956
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001957static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1958{
1959 struct nfs4_opendata *data = calldata;
1960
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001961 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001962
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001963 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001964 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001965 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001966 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001967 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001968 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001969 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001970}
1971
1972static void nfs4_open_confirm_release(void *calldata)
1973{
1974 struct nfs4_opendata *data = calldata;
1975 struct nfs4_state *state = NULL;
1976
1977 /* If this request hasn't been cancelled, do nothing */
1978 if (data->cancelled == 0)
1979 goto out_free;
1980 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001981 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001982 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001983 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001984 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001985 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001986out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001987 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001988}
1989
1990static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001991 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001992 .rpc_call_done = nfs4_open_confirm_done,
1993 .rpc_release = nfs4_open_confirm_release,
1994};
1995
1996/*
1997 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1998 */
1999static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2000{
David Howells2b0143b2015-03-17 22:25:59 +00002001 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002002 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002003 struct rpc_message msg = {
2004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2005 .rpc_argp = &data->c_arg,
2006 .rpc_resp = &data->c_res,
2007 .rpc_cred = data->owner->so_cred,
2008 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002009 struct rpc_task_setup task_setup_data = {
2010 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002011 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002012 .callback_ops = &nfs4_open_confirm_ops,
2013 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002014 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002015 .flags = RPC_TASK_ASYNC,
2016 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 int status;
2018
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002019 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002020 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002021 data->rpc_done = 0;
2022 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002023 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002024 if (data->is_recover)
2025 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002026 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002027 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002028 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002029 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002030 if (status != 0) {
2031 data->cancelled = 1;
2032 smp_wmb();
2033 } else
2034 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002035 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return status;
2037}
2038
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002039static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002041 struct nfs4_opendata *data = calldata;
2042 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002043 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002044 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002045
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002046 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002047 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002048 /*
2049 * Check if we still need to send an OPEN call, or if we can use
2050 * a delegation instead.
2051 */
2052 if (data->state != NULL) {
2053 struct nfs_delegation *delegation;
2054
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002055 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002056 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002057 rcu_read_lock();
2058 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002059 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002060 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002061 rcu_read_unlock();
2062 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002063 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002064 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002065 switch (claim) {
2066 default:
2067 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002068 case NFS4_OPEN_CLAIM_PREVIOUS:
2069 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2070 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002071 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002072 case NFS4_OPEN_CLAIM_FH:
2073 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002074 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2075 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002076 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002077 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002078 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002079 &data->o_res.seq_res,
2080 task) != 0)
2081 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002082
2083 /* Set the create mode (note dependency on the session type) */
2084 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2085 if (data->o_arg.open_flags & O_EXCL) {
2086 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2087 if (nfs4_has_persistent_session(clp))
2088 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2089 else if (clp->cl_mvops->minor_version > 0)
2090 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2091 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002092 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002093unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002094 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002095 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002096out_no_action:
2097 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002098out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002099 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002100}
2101
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002102static void nfs4_open_done(struct rpc_task *task, void *calldata)
2103{
2104 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002106 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002107
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002108 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002109 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002110
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002111 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002112 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2113 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002114 case S_IFREG:
2115 break;
2116 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002117 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002118 break;
2119 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002120 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002121 break;
2122 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002123 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002124 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002125 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002126 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002127 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2128 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002129 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002130 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002131}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002132
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002133static void nfs4_open_release(void *calldata)
2134{
2135 struct nfs4_opendata *data = calldata;
2136 struct nfs4_state *state = NULL;
2137
2138 /* If this request hasn't been cancelled, do nothing */
2139 if (data->cancelled == 0)
2140 goto out_free;
2141 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002142 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002143 goto out_free;
2144 /* In case we need an open_confirm, no cleanup! */
2145 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2146 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002147 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002148 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002149 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002150out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002151 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002152}
2153
2154static const struct rpc_call_ops nfs4_open_ops = {
2155 .rpc_call_prepare = nfs4_open_prepare,
2156 .rpc_call_done = nfs4_open_done,
2157 .rpc_release = nfs4_open_release,
2158};
2159
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002160static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002161{
David Howells2b0143b2015-03-17 22:25:59 +00002162 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002163 struct nfs_server *server = NFS_SERVER(dir);
2164 struct nfs_openargs *o_arg = &data->o_arg;
2165 struct nfs_openres *o_res = &data->o_res;
2166 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002167 struct rpc_message msg = {
2168 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2169 .rpc_argp = o_arg,
2170 .rpc_resp = o_res,
2171 .rpc_cred = data->owner->so_cred,
2172 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002173 struct rpc_task_setup task_setup_data = {
2174 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002175 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002176 .callback_ops = &nfs4_open_ops,
2177 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002178 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002179 .flags = RPC_TASK_ASYNC,
2180 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002181 int status;
2182
Chuck Levera9c92d62013-08-09 12:48:18 -04002183 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002184 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002185 data->rpc_done = 0;
2186 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002187 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002188 data->is_recover = 0;
2189 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002190 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002191 data->is_recover = 1;
2192 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002193 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002194 if (IS_ERR(task))
2195 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002196 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002197 if (status != 0) {
2198 data->cancelled = 1;
2199 smp_wmb();
2200 } else
2201 status = data->rpc_status;
2202 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002203
2204 return status;
2205}
2206
2207static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2208{
David Howells2b0143b2015-03-17 22:25:59 +00002209 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002210 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002211 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002212
2213 status = nfs4_run_open_task(data, 1);
2214 if (status != 0 || !data->rpc_done)
2215 return status;
2216
Trond Myklebust6926afd2012-01-07 13:22:46 -05002217 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2218
Anna Schumakerd7e98252017-01-11 16:13:29 -05002219 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002220 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002221
2222 return status;
2223}
2224
Trond Myklebustf3792d62014-07-10 08:54:32 -04002225/*
2226 * Additional permission checks in order to distinguish between an
2227 * open for read, and an open for execute. This works around the
2228 * fact that NFSv4 OPEN treats read and execute permissions as being
2229 * the same.
2230 * Note that in the non-execute case, we want to turn off permission
2231 * checking if we just created a new file (POSIX open() semantics).
2232 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002233static int nfs4_opendata_access(struct rpc_cred *cred,
2234 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002235 struct nfs4_state *state, fmode_t fmode,
2236 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002237{
2238 struct nfs_access_entry cache;
2239 u32 mask;
2240
2241 /* access call failed or for some reason the server doesn't
2242 * support any access modes -- defer access call until later */
2243 if (opendata->o_res.access_supported == 0)
2244 return 0;
2245
2246 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002247 /*
2248 * Use openflags to check for exec, because fmode won't
2249 * always have FMODE_EXEC set when file open for exec.
2250 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002251 if (openflags & __FMODE_EXEC) {
2252 /* ONLY check for exec rights */
2253 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002254 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002255 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002256
2257 cache.cred = cred;
2258 cache.jiffies = jiffies;
2259 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2260 nfs_access_add_cache(state->inode, &cache);
2261
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002262 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002263 return 0;
2264
2265 /* even though OPEN succeeded, access is denied. Close the file */
2266 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002267 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002268}
2269
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002270/*
2271 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2272 */
2273static int _nfs4_proc_open(struct nfs4_opendata *data)
2274{
David Howells2b0143b2015-03-17 22:25:59 +00002275 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002276 struct nfs_server *server = NFS_SERVER(dir);
2277 struct nfs_openargs *o_arg = &data->o_arg;
2278 struct nfs_openres *o_res = &data->o_res;
2279 int status;
2280
2281 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002282 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002283 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002284 if (status != 0) {
2285 if (status == -NFS4ERR_BADNAME &&
2286 !(o_arg->open_flags & O_CREAT))
2287 return -ENOENT;
2288 return status;
2289 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002290
Trond Myklebust6926afd2012-01-07 13:22:46 -05002291 nfs_fattr_map_and_free_names(server, &data->f_attr);
2292
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002293 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002294 if (o_arg->open_flags & O_EXCL)
2295 data->file_created = 1;
2296 else if (o_res->cinfo.before != o_res->cinfo.after)
2297 data->file_created = 1;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002298 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002299 update_changeattr(dir, &o_res->cinfo,
2300 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002301 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002302 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2303 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002305 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002307 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
2309 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002310 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002311 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
Andy Adamsond83217c2011-03-01 01:34:17 +00002314static int nfs4_recover_expired_lease(struct nfs_server *server)
2315{
2316 return nfs4_client_recover_expired_lease(server->nfs_client);
2317}
2318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319/*
2320 * OPEN_EXPIRED:
2321 * reclaim state on the server after a network partition.
2322 * Assumes caller holds the appropriate lock
2323 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002324static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002326 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002327 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002329 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002330 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002331 if (IS_ERR(opendata))
2332 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002333 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002334 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002335 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002336 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002337 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338}
2339
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002340static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002341{
Trond Myklebust539cd032007-06-05 11:46:42 -04002342 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002343 struct nfs4_exception exception = { };
2344 int err;
2345
2346 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002347 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002348 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002349 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2350 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002351 switch (err) {
2352 default:
2353 goto out;
2354 case -NFS4ERR_GRACE:
2355 case -NFS4ERR_DELAY:
2356 nfs4_handle_exception(server, err, &exception);
2357 err = 0;
2358 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002359 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002360out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002361 return err;
2362}
2363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2365{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002367 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
Trond Myklebust864472e2006-01-03 09:55:15 +01002369 ctx = nfs4_state_find_open_context(state);
2370 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002371 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002372 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002373 put_nfs_open_context(ctx);
2374 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375}
2376
Trond Myklebust41020b62016-09-22 13:38:58 -04002377static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2378 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002379{
Trond Myklebust41020b62016-09-22 13:38:58 -04002380 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002381 write_seqlock(&state->seqlock);
2382 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2383 write_sequnlock(&state->seqlock);
2384 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2385}
2386
2387static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2388{
2389 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002390 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002391}
2392
2393static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2394{
2395 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2396 nfs40_clear_delegation_stateid(state);
2397 return nfs4_open_expired(sp, state);
2398}
2399
Trond Myklebust45870d62016-09-22 13:38:59 -04002400static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2401 nfs4_stateid *stateid,
2402 struct rpc_cred *cred)
2403{
2404 return -NFS4ERR_BAD_STATEID;
2405}
2406
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002407#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002408static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2409 nfs4_stateid *stateid,
2410 struct rpc_cred *cred)
2411{
2412 int status;
2413
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002414 switch (stateid->type) {
2415 default:
2416 break;
2417 case NFS4_INVALID_STATEID_TYPE:
2418 case NFS4_SPECIAL_STATEID_TYPE:
2419 return -NFS4ERR_BAD_STATEID;
2420 case NFS4_REVOKED_STATEID_TYPE:
2421 goto out_free;
2422 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002423
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002424 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002425 switch (status) {
2426 case -NFS4ERR_EXPIRED:
2427 case -NFS4ERR_ADMIN_REVOKED:
2428 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002429 break;
2430 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002431 return status;
2432 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002433out_free:
2434 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002435 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002436 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002437}
2438
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002439static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002440{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002441 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002442 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002443 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002444 struct rpc_cred *cred;
2445 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002446
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002447 /* Get the delegation credential for use by test/free_stateid */
2448 rcu_read_lock();
2449 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002450 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002451 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002452 return;
2453 }
2454
2455 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002456 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2457 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002458 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002459 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002460 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002461
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002462 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2463 rcu_read_unlock();
2464 return;
2465 }
2466
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002467 cred = get_rpccred(delegation->cred);
2468 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002469 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002470 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002471 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002472 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002473
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002474 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002475}
2476
2477/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002478 * nfs41_check_expired_locks - possibly free a lock stateid
2479 *
2480 * @state: NFSv4 state for an inode
2481 *
2482 * Returns NFS_OK if recovery for this stateid is now finished.
2483 * Otherwise a negative NFS4ERR value is returned.
2484 */
2485static int nfs41_check_expired_locks(struct nfs4_state *state)
2486{
2487 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002488 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002489 struct nfs_server *server = NFS_SERVER(state->inode);
2490
2491 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2492 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002493
2494 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002495 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2496 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2497 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2498
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002499 atomic_inc(&lsp->ls_count);
2500 spin_unlock(&state->state_lock);
2501
2502 nfs4_put_lock_state(prev);
2503 prev = lsp;
2504
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002505 status = nfs41_test_and_free_expired_stateid(server,
2506 &lsp->ls_stateid,
2507 cred);
2508 trace_nfs4_test_lock_stateid(state, lsp, status);
2509 if (status == -NFS4ERR_EXPIRED ||
2510 status == -NFS4ERR_BAD_STATEID) {
2511 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002512 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002513 if (!recover_lost_locks)
2514 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2515 } else if (status != NFS_OK) {
2516 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002517 nfs4_put_lock_state(prev);
2518 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002519 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002520 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002521 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002522 }
2523 spin_unlock(&state->state_lock);
2524 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002525out:
2526 return ret;
2527}
2528
2529/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002530 * nfs41_check_open_stateid - possibly free an open stateid
2531 *
2532 * @state: NFSv4 state for an inode
2533 *
2534 * Returns NFS_OK if recovery for this stateid is now finished.
2535 * Otherwise a negative NFS4ERR value is returned.
2536 */
2537static int nfs41_check_open_stateid(struct nfs4_state *state)
2538{
2539 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002540 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002541 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002542 int status;
2543
Trond Myklebustb134fc42016-09-22 13:39:16 -04002544 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002545 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2546 if (nfs4_have_delegation(state->inode, state->state))
2547 return NFS_OK;
2548 return -NFS4ERR_OPENMODE;
2549 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002550 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002551 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002552 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002553 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002554 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002555 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2556 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2557 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002558 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002559 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002560 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002561 if (status != NFS_OK)
2562 return status;
2563 if (nfs_open_stateid_recover_openmode(state))
2564 return -NFS4ERR_OPENMODE;
2565 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002566}
2567
2568static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2569{
Chuck Levereb64cf92012-07-11 16:30:05 -04002570 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002571
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002572 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002573 status = nfs41_check_expired_locks(state);
2574 if (status != NFS_OK)
2575 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002576 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002577 if (status != NFS_OK)
2578 status = nfs4_open_expired(sp, state);
2579 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002580}
2581#endif
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002584 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2585 * fields corresponding to attributes that were used to store the verifier.
2586 * Make sure we clobber those fields in the later setattr call
2587 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002588static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2589 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002590{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002591 const u32 *attrset = opendata->o_res.attrset;
2592
2593 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002594 !(sattr->ia_valid & ATTR_ATIME_SET))
2595 sattr->ia_valid |= ATTR_ATIME;
2596
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002597 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002598 !(sattr->ia_valid & ATTR_MTIME_SET))
2599 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002600
2601 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002602 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2603 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002604 sattr->ia_valid &= ~ATTR_MODE;
2605
2606 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2607 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002608}
2609
Trond Myklebustc21443c2013-02-07 14:26:21 -05002610static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2611 fmode_t fmode,
2612 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002613 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002614{
2615 struct nfs4_state_owner *sp = opendata->owner;
2616 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002617 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002618 struct nfs4_state *state;
2619 unsigned int seq;
2620 int ret;
2621
2622 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2623
2624 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002625 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002626 goto out;
2627
2628 state = nfs4_opendata_to_nfs4_state(opendata);
2629 ret = PTR_ERR(state);
2630 if (IS_ERR(state))
2631 goto out;
2632 if (server->caps & NFS_CAP_POSIX_LOCK)
2633 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002634 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2635 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002636
Trond Myklebust275bb302013-05-29 13:11:28 -04002637 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002638 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002639 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002640 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002641 alias = d_exact_alias(dentry, state->inode);
2642 if (!alias)
2643 alias = d_splice_alias(igrab(state->inode), dentry);
2644 /* d_splice_alias() can't fail here - it's a non-directory */
2645 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002646 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002647 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002648 }
2649 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002650 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002651 }
2652
Trond Myklebustc21443c2013-02-07 14:26:21 -05002653 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2654 if (ret != 0)
2655 goto out;
2656
Trond Myklebust3efb9722013-05-29 13:17:04 -04002657 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002658 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002659 nfs_inode_attach_open_context(ctx);
2660 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2661 nfs4_schedule_stateid_recovery(server, state);
2662 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002663out:
2664 return ret;
2665}
2666
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002667/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002668 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 */
Andy Adamson82be4172012-05-23 05:02:35 -04002670static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002671 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002672 int flags,
2673 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002674 struct nfs4_label *label,
2675 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676{
2677 struct nfs4_state_owner *sp;
2678 struct nfs4_state *state = NULL;
2679 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002680 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002681 struct dentry *dentry = ctx->dentry;
2682 struct rpc_cred *cred = ctx->cred;
2683 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2684 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002685 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002686 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002687 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
2689 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002691 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2692 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2694 goto out_err;
2695 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002696 status = nfs4_recover_expired_lease(server);
2697 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002698 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002699 if (d_really_is_positive(dentry))
2700 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002701 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002702 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002703 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002704 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002705 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002706 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002707 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
David Quigley14c43f72013-05-22 12:50:43 -04002709 if (label) {
2710 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2711 if (IS_ERR(olabel)) {
2712 status = PTR_ERR(olabel);
2713 goto err_opendata_put;
2714 }
2715 }
2716
Trond Myklebuste911b812014-03-26 13:24:37 -07002717 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2718 if (!opendata->f_attr.mdsthreshold) {
2719 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2720 if (!opendata->f_attr.mdsthreshold)
2721 goto err_free_label;
2722 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002723 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002724 }
David Howells2b0143b2015-03-17 22:25:59 +00002725 if (d_really_is_positive(dentry))
2726 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002727
Trond Myklebust3efb9722013-05-29 13:17:04 -04002728 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002729 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002730 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002731 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002732
NeilBrownefcbc042015-07-30 13:00:56 +10002733 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002734 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002735 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002736 /*
2737 * send create attributes which was not set by open
2738 * with an extra setattr.
2739 */
2740 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2741 nfs_fattr_init(opendata->o_res.f_attr);
2742 status = nfs4_do_setattr(state->inode, cred,
2743 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002744 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002745 if (status == 0) {
2746 nfs_setattr_update_inode(state->inode, sattr,
2747 opendata->o_res.f_attr);
2748 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2749 }
David Quigley1775fd32013-05-22 12:50:42 -04002750 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002751 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002752 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002753 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002754
Trond Myklebuste911b812014-03-26 13:24:37 -07002755 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002756 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002757 opendata->f_attr.mdsthreshold = NULL;
2758 }
Andy Adamson82be4172012-05-23 05:02:35 -04002759
David Quigley14c43f72013-05-22 12:50:43 -04002760 nfs4_label_free(olabel);
2761
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002762 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002765err_free_label:
2766 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002767err_opendata_put:
2768 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002769err_put_state_owner:
2770 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return status;
2773}
2774
2775
Andy Adamson82be4172012-05-23 05:02:35 -04002776static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002777 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002778 int flags,
2779 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002780 struct nfs4_label *label,
2781 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002783 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 struct nfs4_exception exception = { };
2785 struct nfs4_state *res;
2786 int status;
2787
2788 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002789 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002790 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002791 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 if (status == 0)
2793 break;
2794 /* NOTE: BAD_SEQID means the server and client disagree about the
2795 * book-keeping w.r.t. state-changing operations
2796 * (OPEN/CLOSE/LOCK/LOCKU...)
2797 * It is actually a sign of a bug on the client or on the server.
2798 *
2799 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002800 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 * have unhashed the old state_owner for us, and that we can
2802 * therefore safely retry using a new one. We should still warn
2803 * the user though...
2804 */
2805 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002806 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002807 " returned a bad sequence-id error!\n",
2808 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 exception.retry = 1;
2810 continue;
2811 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002812 /*
2813 * BAD_STATEID on OPEN means that the server cancelled our
2814 * state before it received the OPEN_CONFIRM.
2815 * Recover by retrying the request as per the discussion
2816 * on Page 181 of RFC3530.
2817 */
2818 if (status == -NFS4ERR_BAD_STATEID) {
2819 exception.retry = 1;
2820 continue;
2821 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002822 if (status == -EAGAIN) {
2823 /* We must have found a delegation */
2824 exception.retry = 1;
2825 continue;
2826 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002827 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2828 continue;
2829 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 status, &exception));
2831 } while (exception.retry);
2832 return res;
2833}
2834
Trond Myklebust8487c472016-06-26 08:44:35 -04002835static int _nfs4_do_setattr(struct inode *inode,
2836 struct nfs_setattrargs *arg,
2837 struct nfs_setattrres *res,
2838 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002839 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002840{
2841 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002842 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04002843 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2844 .rpc_argp = arg,
2845 .rpc_resp = res,
2846 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002847 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002848 struct rpc_cred *delegation_cred = NULL;
2849 unsigned long timestamp = jiffies;
2850 fmode_t fmode;
2851 bool truncate;
2852 int status;
2853
2854 nfs_fattr_init(res->fattr);
2855
2856 /* Servers should only apply open mode checks for file size changes */
2857 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2858 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2859
2860 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2861 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002862 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002863 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002864 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002865 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002866 l_ctx = nfs_get_lock_context(ctx);
2867 if (IS_ERR(l_ctx))
2868 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002869 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2870 &arg->stateid, &delegation_cred);
2871 nfs_put_lock_context(l_ctx);
2872 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002873 return -EBADF;
2874 } else
2875 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2876 if (delegation_cred)
2877 msg.rpc_cred = delegation_cred;
2878
2879 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2880
2881 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002882 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002883 renew_lease(server, timestamp);
2884 trace_nfs4_setattr(inode, &arg->stateid, status);
2885 return status;
2886}
2887
2888static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2889 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002890 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002891 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002893 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002894 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002895 struct nfs_setattrargs arg = {
2896 .fh = NFS_FH(inode),
2897 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 .server = server,
2899 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002900 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002901 };
2902 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002904 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002906 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002907 struct nfs4_exception exception = {
2908 .state = state,
2909 .inode = inode,
2910 .stateid = &arg.stateid,
2911 };
2912 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
David Quigleyaa9c2662013-05-22 12:50:44 -04002914 arg.bitmask = nfs4_bitmask(server, ilabel);
2915 if (ilabel)
2916 arg.bitmask = nfs4_bitmask(server, olabel);
2917
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 do {
NeilBrown29b59f92016-10-13 15:26:47 +11002919 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04002920 switch (err) {
2921 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002922 if (!(sattr->ia_valid & ATTR_SIZE)) {
2923 pr_warn_once("NFSv4: server %s is incorrectly "
2924 "applying open mode checks to "
2925 "a SETATTR that is not "
2926 "changing file size.\n",
2927 server->nfs_client->cl_hostname);
2928 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002929 if (state && !(state->state & FMODE_WRITE)) {
2930 err = -EBADF;
2931 if (sattr->ia_valid & ATTR_OPEN)
2932 err = -EACCES;
2933 goto out;
2934 }
2935 }
2936 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002938out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 return err;
2940}
2941
Peng Tao500d7012015-09-22 11:35:22 +08002942static bool
2943nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2944{
2945 if (inode == NULL || !nfs_have_layout(inode))
2946 return false;
2947
2948 return pnfs_wait_on_layoutreturn(inode, task);
2949}
2950
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951struct nfs4_closedata {
2952 struct inode *inode;
2953 struct nfs4_state *state;
2954 struct nfs_closeargs arg;
2955 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05002956 struct {
2957 struct nfs4_layoutreturn_args arg;
2958 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04002959 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05002960 u32 roc_barrier;
2961 bool roc;
2962 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002963 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002964 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965};
2966
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002967static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002968{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002969 struct nfs4_closedata *calldata = data;
2970 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002971 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002972
Trond Myklebustcf805162016-11-15 14:56:07 -05002973 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05002974 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
2975 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07002976 nfs4_put_open_state(calldata->state);
2977 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002978 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002979 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002980 kfree(calldata);
2981}
2982
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002983static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002985 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002988 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Chuck Levera3ca5652012-03-01 17:00:40 -05002990 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002991 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2992 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002993 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05002994
2995 /* Handle Layoutreturn errors */
2996 if (calldata->arg.lr_args && task->tk_status != 0) {
2997 switch (calldata->res.lr_ret) {
2998 default:
2999 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3000 break;
3001 case 0:
3002 calldata->arg.lr_args = NULL;
3003 calldata->res.lr_res = NULL;
3004 break;
3005 case -NFS4ERR_ADMIN_REVOKED:
3006 case -NFS4ERR_DELEG_REVOKED:
3007 case -NFS4ERR_EXPIRED:
3008 case -NFS4ERR_BAD_STATEID:
3009 case -NFS4ERR_OLD_STATEID:
3010 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3011 case -NFS4ERR_WRONG_CRED:
3012 calldata->arg.lr_args = NULL;
3013 calldata->res.lr_res = NULL;
3014 calldata->res.lr_ret = 0;
3015 rpc_restart_call_prepare(task);
3016 return;
3017 }
3018 }
3019
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003020 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 * the state_owner. we keep this around to process errors
3022 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 switch (task->tk_status) {
3024 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003025 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003026 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003027 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003028 case -NFS4ERR_ACCESS:
3029 if (calldata->arg.bitmask != NULL) {
3030 calldata->arg.bitmask = NULL;
3031 calldata->res.fattr = NULL;
3032 task->tk_status = 0;
3033 rpc_restart_call_prepare(task);
3034 goto out_release;
3035
3036 }
3037 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003038 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003040 case -NFS4ERR_EXPIRED:
3041 nfs4_free_revoked_stateid(server,
3042 &calldata->arg.stateid,
3043 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003044 case -NFS4ERR_OLD_STATEID:
3045 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003046 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003047 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003048 rpc_restart_call_prepare(task);
3049 goto out_release;
3050 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003051 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003052 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003054 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003055 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003056 goto out_release;
3057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003059 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3060 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003061out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003062 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003063 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003064 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065}
3066
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003067static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003069 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003070 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003071 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003072 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003073 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003074
Chuck Levera3ca5652012-03-01 17:00:40 -05003075 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003076 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003077 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003078
Trond Myklebust88069f72009-12-08 08:33:16 -05003079 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003080 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003081 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3082 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3083 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003084 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003085 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003086 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003087 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003088 if (state->n_rdonly == 0)
3089 call_close |= is_rdonly;
3090 else if (is_rdonly)
3091 calldata->arg.fmode |= FMODE_READ;
3092 if (state->n_wronly == 0)
3093 call_close |= is_wronly;
3094 else if (is_wronly)
3095 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003096 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3097 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003098 } else if (is_rdwr)
3099 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3100
Trond Myklebust5cc78612016-11-14 11:19:56 -05003101 if (!nfs4_valid_open_stateid(state) ||
3102 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003103 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003104 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003105
3106 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003107 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003108 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003109 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003110
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003111 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003112 nfs_release_seqid(calldata->arg.seqid);
3113 goto out_wait;
3114 }
3115
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003116 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003117 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003118
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003119 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003120 /* Close-to-open cache consistency revalidation */
3121 if (!nfs4_have_delegation(inode, FMODE_READ))
3122 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3123 else
3124 calldata->arg.bitmask = NULL;
3125 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003126
Trond Myklebust6ae37332015-01-30 14:21:14 -05003127 calldata->arg.share_access =
3128 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3129 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003130
Trond Myklebustd8d84982016-12-19 12:14:44 -05003131 if (calldata->res.fattr == NULL)
3132 calldata->arg.bitmask = NULL;
3133 else if (calldata->arg.bitmask == NULL)
3134 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003135 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003136 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003137 &calldata->arg.seq_args,
3138 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003139 task) != 0)
3140 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003141 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003142 return;
3143out_no_action:
3144 task->tk_action = NULL;
3145out_wait:
3146 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147}
3148
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003149static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003150 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003151 .rpc_call_done = nfs4_close_done,
3152 .rpc_release = nfs4_free_closedata,
3153};
3154
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155/*
3156 * It is possible for data to be read/written from a mem-mapped file
3157 * after the sys_close call (which hits the vfs layer as a flush).
3158 * This means that we can't safely call nfsv4 close on a file until
3159 * the inode is cleared. This in turn means that we are not good
3160 * NFSv4 citizens - we do not indicate to the server to update the file's
3161 * share state even when we are done with one of the three share
3162 * stateid's in the inode.
3163 *
3164 * NOTE: Caller must be holding the sp->so_owner semaphore!
3165 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003166int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003168 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003169 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003171 struct nfs4_state_owner *sp = state->owner;
3172 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003173 struct rpc_message msg = {
3174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3175 .rpc_cred = state->owner->so_cred,
3176 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003177 struct rpc_task_setup task_setup_data = {
3178 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003179 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003180 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003181 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003182 .flags = RPC_TASK_ASYNC,
3183 };
Trond Myklebust95121352005-10-18 14:20:12 -07003184 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003186 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3187 &task_setup_data.rpc_client, &msg);
3188
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003189 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003191 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003192 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003193 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003195 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003197 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3198 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003199 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003200 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003201 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003202 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003203 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003204 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003205 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003206 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003207 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003208 calldata->lr.roc = pnfs_roc(state->inode,
3209 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3210 if (calldata->lr.roc) {
3211 calldata->arg.lr_args = &calldata->lr.arg;
3212 calldata->res.lr_res = &calldata->lr.res;
3213 }
Al Viro643168c2011-06-22 18:20:23 -04003214 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003215
Trond Myklebust1174dd12010-12-21 10:52:24 -05003216 msg.rpc_argp = &calldata->arg;
3217 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003218 task_setup_data.callback_data = calldata;
3219 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003220 if (IS_ERR(task))
3221 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003222 status = 0;
3223 if (wait)
3224 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003225 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003226 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003227out_free_calldata:
3228 kfree(calldata);
3229out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003230 nfs4_put_open_state(state);
3231 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003232 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233}
3234
Trond Myklebust2b484292010-09-17 10:56:51 -04003235static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003236nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3237 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003240 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3241
3242 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
Trond Myklebust565277f2007-10-15 18:17:53 -04003244 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003245 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003246
3247 nfs4_label_release_security(label);
3248
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003249 if (IS_ERR(state))
3250 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003251 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252}
3253
Trond Myklebust1185a552009-12-03 15:54:02 -05003254static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003255{
3256 if (ctx->state == NULL)
3257 return;
3258 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003259 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003260 else
Al Viro643168c2011-06-22 18:20:23 -04003261 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003262}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Trond Myklebustb944dba2013-11-04 15:20:20 -05003264#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3265#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003266#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003267
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3269{
Kinglong Mee8c612822015-08-26 21:12:58 +08003270 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003271 struct nfs4_server_caps_arg args = {
3272 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003273 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003274 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 struct nfs4_server_caps_res res = {};
3276 struct rpc_message msg = {
3277 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003278 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 .rpc_resp = &res,
3280 };
3281 int status;
3282
Kinglong Mee8c612822015-08-26 21:12:58 +08003283 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3284 FATTR4_WORD0_FH_EXPIRE_TYPE |
3285 FATTR4_WORD0_LINK_SUPPORT |
3286 FATTR4_WORD0_SYMLINK_SUPPORT |
3287 FATTR4_WORD0_ACLSUPPORT;
3288 if (minorversion)
3289 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3290
Bryan Schumaker7c513052011-03-24 17:12:24 +00003291 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003293 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003294 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003295 case 0:
3296 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3297 res.attr_bitmask[2] = 0;
3298 break;
3299 case 1:
3300 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3301 break;
3302 case 2:
3303 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003306 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3307 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3308 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3309 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003310 NFS_CAP_CTIME|NFS_CAP_MTIME|
3311 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003312 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3313 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 server->caps |= NFS_CAP_ACLS;
3315 if (res.has_links != 0)
3316 server->caps |= NFS_CAP_HARDLINKS;
3317 if (res.has_symlinks != 0)
3318 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003319 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3320 server->caps |= NFS_CAP_FILEID;
3321 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3322 server->caps |= NFS_CAP_MODE;
3323 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3324 server->caps |= NFS_CAP_NLINK;
3325 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3326 server->caps |= NFS_CAP_OWNER;
3327 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3328 server->caps |= NFS_CAP_OWNER_GROUP;
3329 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3330 server->caps |= NFS_CAP_ATIME;
3331 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3332 server->caps |= NFS_CAP_CTIME;
3333 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3334 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003335#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3336 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3337 server->caps |= NFS_CAP_SECURITY_LABEL;
3338#endif
3339 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3340 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003341 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003342
Trond Myklebusta65318b2009-03-11 14:10:28 -04003343 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3344 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3345 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003346 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003347 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3348 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003350 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003352
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 return status;
3354}
3355
Trond Myklebust55a97592006-06-09 09:34:19 -04003356int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357{
3358 struct nfs4_exception exception = { };
3359 int err;
3360 do {
3361 err = nfs4_handle_exception(server,
3362 _nfs4_server_capabilities(server, fhandle),
3363 &exception);
3364 } while (exception.retry);
3365 return err;
3366}
3367
3368static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3369 struct nfs_fsinfo *info)
3370{
David Quigleyaa9c2662013-05-22 12:50:44 -04003371 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003373 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 };
3375 struct nfs4_lookup_res res = {
3376 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003377 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 .fh = fhandle,
3379 };
3380 struct rpc_message msg = {
3381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3382 .rpc_argp = &args,
3383 .rpc_resp = &res,
3384 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003385
David Quigleyaa9c2662013-05-22 12:50:44 -04003386 bitmask[0] = nfs4_fattr_bitmap[0];
3387 bitmask[1] = nfs4_fattr_bitmap[1];
3388 /*
3389 * Process the label in the upcoming getfattr
3390 */
3391 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3392
Trond Myklebust0e574af2005-10-27 22:12:38 -04003393 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003394 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395}
3396
3397static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3398 struct nfs_fsinfo *info)
3399{
3400 struct nfs4_exception exception = { };
3401 int err;
3402 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003403 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003404 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003405 switch (err) {
3406 case 0:
3407 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003408 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003409 default:
3410 err = nfs4_handle_exception(server, err, &exception);
3411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003413out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 return err;
3415}
3416
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003417static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3418 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3419{
Trond Myklebustc2190662013-08-26 19:23:04 -04003420 struct rpc_auth_create_args auth_args = {
3421 .pseudoflavor = flavor,
3422 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003423 struct rpc_auth *auth;
3424 int ret;
3425
Trond Myklebustc2190662013-08-26 19:23:04 -04003426 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003427 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003428 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003429 goto out;
3430 }
3431 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003432out:
3433 return ret;
3434}
3435
Chuck Lever9a744ba2013-03-16 15:56:02 -04003436/*
3437 * Retry pseudoroot lookup with various security flavors. We do this when:
3438 *
3439 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3440 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3441 *
3442 * Returns zero on success, or a negative NFS4ERR value, or a
3443 * negative errno value.
3444 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003445static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003446 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003448 /* Per 3530bis 15.33.5 */
3449 static const rpc_authflavor_t flav_array[] = {
3450 RPC_AUTH_GSS_KRB5P,
3451 RPC_AUTH_GSS_KRB5I,
3452 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003453 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003454 RPC_AUTH_NULL,
3455 };
3456 int status = -EPERM;
3457 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003459 if (server->auth_info.flavor_len > 0) {
3460 /* try each flavor specified by user */
3461 for (i = 0; i < server->auth_info.flavor_len; i++) {
3462 status = nfs4_lookup_root_sec(server, fhandle, info,
3463 server->auth_info.flavors[i]);
3464 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3465 continue;
3466 break;
3467 }
3468 } else {
3469 /* no flavors specified by user, try default list */
3470 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3471 status = nfs4_lookup_root_sec(server, fhandle, info,
3472 flav_array[i]);
3473 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3474 continue;
3475 break;
3476 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003477 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003478
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003479 /*
3480 * -EACCESS could mean that the user doesn't have correct permissions
3481 * to access the mount. It could also mean that we tried to mount
3482 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3483 * existing mount programs don't handle -EACCES very well so it should
3484 * be mapped to -EPERM instead.
3485 */
3486 if (status == -EACCES)
3487 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003488 return status;
3489}
3490
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003491/**
3492 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3493 * @server: initialized nfs_server handle
3494 * @fhandle: we fill in the pseudo-fs root file handle
3495 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003496 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003497 *
3498 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003499 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003500int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003501 struct nfs_fsinfo *info,
3502 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003503{
Andre Przywarac7757072015-04-23 17:17:40 +01003504 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003505
Andre Przywarac7757072015-04-23 17:17:40 +01003506 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003507 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003508
3509 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003510 status = server->nfs_client->cl_mvops->find_root_sec(server,
3511 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003512
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 if (status == 0)
3514 status = nfs4_server_capabilities(server, fhandle);
3515 if (status == 0)
3516 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003517
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003518 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519}
3520
Bryan Schumakerbae36242012-05-10 15:07:31 -04003521static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3522 struct nfs_fsinfo *info)
3523{
3524 int error;
3525 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003526 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003527
3528 error = nfs4_server_capabilities(server, mntfh);
3529 if (error < 0) {
3530 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3531 return error;
3532 }
3533
David Quigley14c43f72013-05-22 12:50:43 -04003534 label = nfs4_label_alloc(server, GFP_KERNEL);
3535 if (IS_ERR(label))
3536 return PTR_ERR(label);
3537
David Quigley1775fd32013-05-22 12:50:42 -04003538 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003539 if (error < 0) {
3540 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003541 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003542 }
3543
3544 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3545 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3546 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3547
David Quigley14c43f72013-05-22 12:50:43 -04003548err_free_label:
3549 nfs4_label_free(label);
3550
Bryan Schumakerbae36242012-05-10 15:07:31 -04003551 return error;
3552}
3553
Manoj Naik6b97fd32006-06-09 09:34:29 -04003554/*
3555 * Get locations and (maybe) other attributes of a referral.
3556 * Note that we'll actually follow the referral later when
3557 * we detect fsid mismatch in inode revalidation
3558 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003559static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3560 const struct qstr *name, struct nfs_fattr *fattr,
3561 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003562{
3563 int status = -ENOMEM;
3564 struct page *page = NULL;
3565 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003566
3567 page = alloc_page(GFP_KERNEL);
3568 if (page == NULL)
3569 goto out;
3570 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3571 if (locations == NULL)
3572 goto out;
3573
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003574 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003575 if (status != 0)
3576 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003577
3578 /*
3579 * If the fsid didn't change, this is a migration event, not a
3580 * referral. Cause us to drop into the exception handler, which
3581 * will kick off migration recovery.
3582 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003583 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003584 dprintk("%s: server did not return a different fsid for"
3585 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003586 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003587 goto out;
3588 }
Andy Adamson533eb462011-06-13 18:25:56 -04003589 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3590 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003591
Andy Adamson533eb462011-06-13 18:25:56 -04003592 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003593 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003594 memset(fhandle, 0, sizeof(struct nfs_fh));
3595out:
3596 if (page)
3597 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003598 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003599 return status;
3600}
3601
David Quigley1775fd32013-05-22 12:50:42 -04003602static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3603 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604{
3605 struct nfs4_getattr_arg args = {
3606 .fh = fhandle,
3607 .bitmask = server->attr_bitmask,
3608 };
3609 struct nfs4_getattr_res res = {
3610 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003611 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 .server = server,
3613 };
3614 struct rpc_message msg = {
3615 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3616 .rpc_argp = &args,
3617 .rpc_resp = &res,
3618 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003619
3620 args.bitmask = nfs4_bitmask(server, label);
3621
Trond Myklebust0e574af2005-10-27 22:12:38 -04003622 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003623 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624}
3625
David Quigley1775fd32013-05-22 12:50:42 -04003626static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3627 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628{
3629 struct nfs4_exception exception = { };
3630 int err;
3631 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003632 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3633 trace_nfs4_getattr(server, fhandle, fattr, err);
3634 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 &exception);
3636 } while (exception.retry);
3637 return err;
3638}
3639
3640/*
3641 * The file is not closed if it is opened due to the a request to change
3642 * the size of the file. The open call will not be needed once the
3643 * VFS layer lookup-intents are implemented.
3644 *
3645 * Close is called when the inode is destroyed.
3646 * If we haven't opened the file for O_WRONLY, we
3647 * need to in the size_change case to obtain a stateid.
3648 *
3649 * Got race?
3650 * Because OPEN is always done by name in nfsv4, it is
3651 * possible that we opened a different file by the same
3652 * name. We can recognize this race condition, but we
3653 * can't do anything about it besides returning an error.
3654 *
3655 * This will be fixed with VFS changes (lookup-intent).
3656 */
3657static int
3658nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3659 struct iattr *sattr)
3660{
David Howells2b0143b2015-03-17 22:25:59 +00003661 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003662 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003663 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003664 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 int status;
3666
Peng Tao88ac8152014-09-12 11:04:10 +08003667 if (pnfs_ld_layoutret_on_setattr(inode) &&
3668 sattr->ia_valid & ATTR_SIZE &&
3669 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003670 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003671
Trond Myklebust0e574af2005-10-27 22:12:38 -04003672 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
Andy Adamson26699402012-05-30 16:12:24 -04003674 /* Deal with open(O_TRUNC) */
3675 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003676 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003677
3678 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003679 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003680 return 0;
3681
Trond Myklebustd5308382005-11-04 15:33:38 -05003682 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003683 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003684
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003685 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003686 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003687 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003688 }
3689
David Quigley14c43f72013-05-22 12:50:43 -04003690 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3691 if (IS_ERR(label))
3692 return PTR_ERR(label);
3693
NeilBrown29b59f92016-10-13 15:26:47 +11003694 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003695 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003696 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003697 nfs_setsecurity(inode, fattr, label);
3698 }
David Quigley14c43f72013-05-22 12:50:43 -04003699 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 return status;
3701}
3702
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003703static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3704 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003705 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003706{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003707 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003708 int status;
3709 struct nfs4_lookup_arg args = {
3710 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003711 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003712 .name = name,
3713 };
3714 struct nfs4_lookup_res res = {
3715 .server = server,
3716 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003717 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003718 .fh = fhandle,
3719 };
3720 struct rpc_message msg = {
3721 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3722 .rpc_argp = &args,
3723 .rpc_resp = &res,
3724 };
3725
David Quigleyaa9c2662013-05-22 12:50:44 -04003726 args.bitmask = nfs4_bitmask(server, label);
3727
David Howells2b3de442006-08-22 20:06:09 -04003728 nfs_fattr_init(fattr);
3729
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003731 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 dprintk("NFS reply lookup: %d\n", status);
3733 return status;
3734}
3735
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003736static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003737{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003738 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003739 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003740 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3741 fattr->nlink = 2;
3742}
3743
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003744static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003745 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003746 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003747{
3748 struct nfs4_exception exception = { };
3749 struct rpc_clnt *client = *clnt;
3750 int err;
3751 do {
David Quigley1775fd32013-05-22 12:50:42 -04003752 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003753 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003754 switch (err) {
3755 case -NFS4ERR_BADNAME:
3756 err = -ENOENT;
3757 goto out;
3758 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003759 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003760 if (err == -NFS4ERR_MOVED)
3761 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003762 goto out;
3763 case -NFS4ERR_WRONGSEC:
3764 err = -EPERM;
3765 if (client != *clnt)
3766 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003767 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003768 if (IS_ERR(client))
3769 return PTR_ERR(client);
3770
3771 exception.retry = 1;
3772 break;
3773 default:
3774 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3775 }
3776 } while (exception.retry);
3777
3778out:
3779 if (err == 0)
3780 *clnt = client;
3781 else if (client != *clnt)
3782 rpc_shutdown_client(client);
3783
3784 return err;
3785}
3786
Al Virobeffb8f2016-07-20 16:34:42 -04003787static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003788 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3789 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003791 int status;
3792 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003793
David Quigley1775fd32013-05-22 12:50:42 -04003794 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003795 if (client != NFS_CLIENT(dir)) {
3796 rpc_shutdown_client(client);
3797 nfs_fixup_secinfo_attributes(fattr);
3798 }
3799 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800}
3801
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003802struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003803nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003804 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3805{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003806 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003807 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003808
David Quigley1775fd32013-05-22 12:50:42 -04003809 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003810 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003811 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003812 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003813}
3814
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3816{
Trond Myklebust76b32992007-08-10 17:45:11 -04003817 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 struct nfs4_accessargs args = {
3819 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003820 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003822 struct nfs4_accessres res = {
3823 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003824 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 struct rpc_message msg = {
3826 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3827 .rpc_argp = &args,
3828 .rpc_resp = &res,
3829 .rpc_cred = entry->cred,
3830 };
3831 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003832 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833
3834 /*
3835 * Determine which access bits we want to ask for...
3836 */
3837 if (mode & MAY_READ)
3838 args.access |= NFS4_ACCESS_READ;
3839 if (S_ISDIR(inode->i_mode)) {
3840 if (mode & MAY_WRITE)
3841 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3842 if (mode & MAY_EXEC)
3843 args.access |= NFS4_ACCESS_LOOKUP;
3844 } else {
3845 if (mode & MAY_WRITE)
3846 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3847 if (mode & MAY_EXEC)
3848 args.access |= NFS4_ACCESS_EXECUTE;
3849 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003850
3851 res.fattr = nfs_alloc_fattr();
3852 if (res.fattr == NULL)
3853 return -ENOMEM;
3854
Bryan Schumaker7c513052011-03-24 17:12:24 +00003855 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003857 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003858 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003860 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 return status;
3862}
3863
3864static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3865{
3866 struct nfs4_exception exception = { };
3867 int err;
3868 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003869 err = _nfs4_proc_access(inode, entry);
3870 trace_nfs4_access(inode, err);
3871 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 &exception);
3873 } while (exception.retry);
3874 return err;
3875}
3876
3877/*
3878 * TODO: For the time being, we don't try to get any attributes
3879 * along with any of the zero-copy operations READ, READDIR,
3880 * READLINK, WRITE.
3881 *
3882 * In the case of the first three, we want to put the GETATTR
3883 * after the read-type operation -- this is because it is hard
3884 * to predict the length of a GETATTR response in v4, and thus
3885 * align the READ data correctly. This means that the GETATTR
3886 * may end up partially falling into the page cache, and we should
3887 * shift it into the 'tail' of the xdr_buf before processing.
3888 * To do this efficiently, we need to know the total length
3889 * of data received, which doesn't seem to be available outside
3890 * of the RPC layer.
3891 *
3892 * In the case of WRITE, we also want to put the GETATTR after
3893 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003894 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 *
3896 * Both of these changes to the XDR layer would in fact be quite
3897 * minor, but I decided to leave them for a subsequent patch.
3898 */
3899static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3900 unsigned int pgbase, unsigned int pglen)
3901{
3902 struct nfs4_readlink args = {
3903 .fh = NFS_FH(inode),
3904 .pgbase = pgbase,
3905 .pglen = pglen,
3906 .pages = &page,
3907 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003908 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 struct rpc_message msg = {
3910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3911 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003912 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 };
3914
Bryan Schumaker7c513052011-03-24 17:12:24 +00003915 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 -07003916}
3917
3918static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3919 unsigned int pgbase, unsigned int pglen)
3920{
3921 struct nfs4_exception exception = { };
3922 int err;
3923 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003924 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3925 trace_nfs4_readlink(inode, err);
3926 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 &exception);
3928 } while (exception.retry);
3929 return err;
3930}
3931
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003933 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935static int
3936nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003937 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003939 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04003940 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003941 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 int status = 0;
3944
NeilBrown532d4de2016-10-13 15:26:47 +11003945 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003946 if (IS_ERR(ctx))
3947 return PTR_ERR(ctx);
3948
David Quigleyaa9c2662013-05-22 12:50:44 -04003949 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3950
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003951 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
3952 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003953 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 if (IS_ERR(state)) {
3955 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003956 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003959 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003960 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 return status;
3962}
3963
Al Virobeffb8f2016-07-20 16:34:42 -04003964static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965{
Trond Myklebust16e42952005-10-27 22:12:44 -04003966 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003967 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003969 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003971 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003972 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003973 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3976 .rpc_argp = &args,
3977 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05003979 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04003980 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003981
Bryan Schumaker7c513052011-03-24 17:12:24 +00003982 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003983 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05003984 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 return status;
3986}
3987
Al Virobeffb8f2016-07-20 16:34:42 -04003988static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989{
3990 struct nfs4_exception exception = { };
3991 int err;
3992 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003993 err = _nfs4_proc_remove(dir, name);
3994 trace_nfs4_remove(dir, name, err);
3995 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 &exception);
3997 } while (exception.retry);
3998 return err;
3999}
4000
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004001static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004003 struct nfs_server *server = NFS_SERVER(dir);
4004 struct nfs_removeargs *args = msg->rpc_argp;
4005 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004007 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004009 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004010
4011 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012}
4013
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004014static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4015{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004016 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004017 &data->args.seq_args,
4018 &data->res.seq_res,
4019 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020}
4021
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004022static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004024 struct nfs_unlinkdata *data = task->tk_calldata;
4025 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004026
Trond Myklebust14516c32010-07-31 14:29:06 -04004027 if (!nfs4_sequence_done(task, &res->seq_res))
4028 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004029 if (nfs4_async_handle_error(task, res->server, NULL,
4030 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004031 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004032 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004033 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004034 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035}
4036
Jeff Laytond3d41522010-09-17 17:31:57 -04004037static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4038{
4039 struct nfs_server *server = NFS_SERVER(dir);
4040 struct nfs_renameargs *arg = msg->rpc_argp;
4041 struct nfs_renameres *res = msg->rpc_resp;
4042
4043 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004044 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004045 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004046}
4047
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004048static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4049{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004050 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004051 &data->args.seq_args,
4052 &data->res.seq_res,
4053 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004054}
4055
4056static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4057 struct inode *new_dir)
4058{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004059 struct nfs_renamedata *data = task->tk_calldata;
4060 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004061
4062 if (!nfs4_sequence_done(task, &res->seq_res))
4063 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004064 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004065 return 0;
4066
Trond Myklebustc733c492017-01-11 12:32:26 -05004067 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004068 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004069 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004070 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004071 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004072 return 1;
4073}
4074
Al Virobeffb8f2016-07-20 16:34:42 -04004075static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004077 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 struct nfs4_link_arg arg = {
4079 .fh = NFS_FH(inode),
4080 .dir_fh = NFS_FH(dir),
4081 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004082 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004084 struct nfs4_link_res res = {
4085 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004086 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004087 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 struct rpc_message msg = {
4089 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4090 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004091 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004093 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094
Trond Myklebust136f2622010-04-16 16:22:49 -04004095 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004096 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004097 goto out;
4098
David Quigley14c43f72013-05-22 12:50:43 -04004099 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4100 if (IS_ERR(res.label)) {
4101 status = PTR_ERR(res.label);
4102 goto out;
4103 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004104 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004105
Bryan Schumaker7c513052011-03-24 17:12:24 +00004106 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004107 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004108 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004109 status = nfs_post_op_update_inode(inode, res.fattr);
4110 if (!status)
4111 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004112 }
David Quigley14c43f72013-05-22 12:50:43 -04004113
4114
4115 nfs4_label_free(res.label);
4116
Trond Myklebust136f2622010-04-16 16:22:49 -04004117out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004118 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 return status;
4120}
4121
Al Virobeffb8f2016-07-20 16:34:42 -04004122static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123{
4124 struct nfs4_exception exception = { };
4125 int err;
4126 do {
4127 err = nfs4_handle_exception(NFS_SERVER(inode),
4128 _nfs4_proc_link(inode, dir, name),
4129 &exception);
4130 } while (exception.retry);
4131 return err;
4132}
4133
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004134struct nfs4_createdata {
4135 struct rpc_message msg;
4136 struct nfs4_create_arg arg;
4137 struct nfs4_create_res res;
4138 struct nfs_fh fh;
4139 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004140 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004141};
4142
4143static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004144 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004145{
4146 struct nfs4_createdata *data;
4147
4148 data = kzalloc(sizeof(*data), GFP_KERNEL);
4149 if (data != NULL) {
4150 struct nfs_server *server = NFS_SERVER(dir);
4151
David Quigley14c43f72013-05-22 12:50:43 -04004152 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4153 if (IS_ERR(data->label))
4154 goto out_free;
4155
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004156 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4157 data->msg.rpc_argp = &data->arg;
4158 data->msg.rpc_resp = &data->res;
4159 data->arg.dir_fh = NFS_FH(dir);
4160 data->arg.server = server;
4161 data->arg.name = name;
4162 data->arg.attrs = sattr;
4163 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004164 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004165 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004166 data->res.server = server;
4167 data->res.fh = &data->fh;
4168 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004169 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004170 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004171 }
4172 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004173out_free:
4174 kfree(data);
4175 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004176}
4177
4178static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4179{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004180 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004181 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004182 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004183 update_changeattr(dir, &data->res.dir_cinfo,
4184 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004185 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004186 }
4187 return status;
4188}
4189
4190static void nfs4_free_createdata(struct nfs4_createdata *data)
4191{
David Quigley14c43f72013-05-22 12:50:43 -04004192 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004193 kfree(data);
4194}
4195
Chuck Lever4f390c12006-08-22 20:06:22 -04004196static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004197 struct page *page, unsigned int len, struct iattr *sattr,
4198 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004200 struct nfs4_createdata *data;
4201 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
Chuck Lever94a6d752006-08-22 20:06:23 -04004203 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004204 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004205
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004206 status = -ENOMEM;
4207 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4208 if (data == NULL)
4209 goto out;
4210
4211 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4212 data->arg.u.symlink.pages = &page;
4213 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004214 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004216 status = nfs4_do_create(dir, dentry, data);
4217
4218 nfs4_free_createdata(data);
4219out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 return status;
4221}
4222
Chuck Lever4f390c12006-08-22 20:06:22 -04004223static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004224 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225{
4226 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004227 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004229
4230 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4231
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004233 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4234 trace_nfs4_symlink(dir, &dentry->d_name, err);
4235 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 &exception);
4237 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004238
4239 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 return err;
4241}
4242
4243static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004244 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004246 struct nfs4_createdata *data;
4247 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004249 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4250 if (data == NULL)
4251 goto out;
4252
David Quigley1775fd32013-05-22 12:50:42 -04004253 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004254 status = nfs4_do_create(dir, dentry, data);
4255
4256 nfs4_free_createdata(data);
4257out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 return status;
4259}
4260
4261static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4262 struct iattr *sattr)
4263{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004264 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004266 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004268
David Quigleyaa9c2662013-05-22 12:50:44 -04004269 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4270
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004271 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4272 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004274 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4275 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4276 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 &exception);
4278 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004279 nfs4_label_release_security(label);
4280
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 return err;
4282}
4283
4284static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004285 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286{
David Howells2b0143b2015-03-17 22:25:59 +00004287 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 struct nfs4_readdir_arg args = {
4289 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004290 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 .pgbase = 0,
4292 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004293 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004294 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 };
4296 struct nfs4_readdir_res res;
4297 struct rpc_message msg = {
4298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4299 .rpc_argp = &args,
4300 .rpc_resp = &res,
4301 .rpc_cred = cred,
4302 };
4303 int status;
4304
Al Viro6de14722013-09-16 10:53:17 -04004305 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4306 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004307 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004308 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004310 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 -05004311 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004312 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004313 status += args.pgbase;
4314 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004315
4316 nfs_invalidate_atime(dir);
4317
Harvey Harrison3110ff82008-05-02 13:42:44 -07004318 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 return status;
4320}
4321
4322static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004323 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324{
4325 struct nfs4_exception exception = { };
4326 int err;
4327 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004328 err = _nfs4_proc_readdir(dentry, cred, cookie,
4329 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004330 trace_nfs4_readdir(d_inode(dentry), err);
4331 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 &exception);
4333 } while (exception.retry);
4334 return err;
4335}
4336
4337static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004338 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004340 struct nfs4_createdata *data;
4341 int mode = sattr->ia_mode;
4342 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004344 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4345 if (data == NULL)
4346 goto out;
4347
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004349 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004351 data->arg.ftype = NF4BLK;
4352 data->arg.u.device.specdata1 = MAJOR(rdev);
4353 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 }
4355 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004356 data->arg.ftype = NF4CHR;
4357 data->arg.u.device.specdata1 = MAJOR(rdev);
4358 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004359 } else if (!S_ISSOCK(mode)) {
4360 status = -EINVAL;
4361 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 }
David Quigley1775fd32013-05-22 12:50:42 -04004363
David Quigleyaa9c2662013-05-22 12:50:44 -04004364 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004365 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004366out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004367 nfs4_free_createdata(data);
4368out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 return status;
4370}
4371
4372static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4373 struct iattr *sattr, dev_t rdev)
4374{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004375 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004377 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004379
David Quigleyaa9c2662013-05-22 12:50:44 -04004380 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4381
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004382 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4383 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004385 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4386 trace_nfs4_mknod(dir, &dentry->d_name, err);
4387 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 &exception);
4389 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004390
4391 nfs4_label_release_security(label);
4392
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 return err;
4394}
4395
4396static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4397 struct nfs_fsstat *fsstat)
4398{
4399 struct nfs4_statfs_arg args = {
4400 .fh = fhandle,
4401 .bitmask = server->attr_bitmask,
4402 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004403 struct nfs4_statfs_res res = {
4404 .fsstat = fsstat,
4405 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 struct rpc_message msg = {
4407 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4408 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004409 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 };
4411
Trond Myklebust0e574af2005-10-27 22:12:38 -04004412 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004413 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414}
4415
4416static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4417{
4418 struct nfs4_exception exception = { };
4419 int err;
4420 do {
4421 err = nfs4_handle_exception(server,
4422 _nfs4_proc_statfs(server, fhandle, fsstat),
4423 &exception);
4424 } while (exception.retry);
4425 return err;
4426}
4427
4428static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4429 struct nfs_fsinfo *fsinfo)
4430{
4431 struct nfs4_fsinfo_arg args = {
4432 .fh = fhandle,
4433 .bitmask = server->attr_bitmask,
4434 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004435 struct nfs4_fsinfo_res res = {
4436 .fsinfo = fsinfo,
4437 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 struct rpc_message msg = {
4439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4440 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004441 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 };
4443
Bryan Schumaker7c513052011-03-24 17:12:24 +00004444 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445}
4446
4447static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4448{
4449 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004450 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 int err;
4452
4453 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004454 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004455 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004456 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004457 nfs4_set_lease_period(server->nfs_client,
4458 fsinfo->lease_time * HZ,
4459 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004460 break;
4461 }
4462 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 } while (exception.retry);
4464 return err;
4465}
4466
4467static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4468{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004469 int error;
4470
Trond Myklebust0e574af2005-10-27 22:12:38 -04004471 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004472 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004473 if (error == 0) {
4474 /* block layout checks this! */
4475 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004476 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004477 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004478
4479 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480}
4481
4482static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4483 struct nfs_pathconf *pathconf)
4484{
4485 struct nfs4_pathconf_arg args = {
4486 .fh = fhandle,
4487 .bitmask = server->attr_bitmask,
4488 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004489 struct nfs4_pathconf_res res = {
4490 .pathconf = pathconf,
4491 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 struct rpc_message msg = {
4493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4494 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004495 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 };
4497
4498 /* None of the pathconf attributes are mandatory to implement */
4499 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4500 memset(pathconf, 0, sizeof(*pathconf));
4501 return 0;
4502 }
4503
Trond Myklebust0e574af2005-10-27 22:12:38 -04004504 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004505 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506}
4507
4508static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4509 struct nfs_pathconf *pathconf)
4510{
4511 struct nfs4_exception exception = { };
4512 int err;
4513
4514 do {
4515 err = nfs4_handle_exception(server,
4516 _nfs4_proc_pathconf(server, fhandle, pathconf),
4517 &exception);
4518 } while (exception.retry);
4519 return err;
4520}
4521
Trond Myklebust5521abf2013-03-16 20:54:34 -04004522int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004523 const struct nfs_open_context *ctx,
4524 const struct nfs_lock_context *l_ctx,
4525 fmode_t fmode)
4526{
NeilBrown17393472016-10-13 15:26:47 +11004527 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004528}
4529EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4530
Trond Myklebust5521abf2013-03-16 20:54:34 -04004531static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4532 const struct nfs_open_context *ctx,
4533 const struct nfs_lock_context *l_ctx,
4534 fmode_t fmode)
4535{
4536 nfs4_stateid current_stateid;
4537
Trond Myklebuste1253be2014-03-05 08:44:23 -05004538 /* If the current stateid represents a lost lock, then exit */
4539 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4540 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004541 return nfs4_stateid_match(stateid, &current_stateid);
4542}
4543
4544static bool nfs4_error_stateid_expired(int err)
4545{
4546 switch (err) {
4547 case -NFS4ERR_DELEG_REVOKED:
4548 case -NFS4ERR_ADMIN_REVOKED:
4549 case -NFS4ERR_BAD_STATEID:
4550 case -NFS4ERR_STALE_STATEID:
4551 case -NFS4ERR_OLD_STATEID:
4552 case -NFS4ERR_OPENMODE:
4553 case -NFS4ERR_EXPIRED:
4554 return true;
4555 }
4556 return false;
4557}
4558
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004559static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004561 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004563 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004564 if (task->tk_status < 0) {
4565 struct nfs4_exception exception = {
4566 .inode = hdr->inode,
4567 .state = hdr->args.context->state,
4568 .stateid = &hdr->args.stateid,
4569 };
4570 task->tk_status = nfs4_async_handle_exception(task,
4571 server, task->tk_status, &exception);
4572 if (exception.retry) {
4573 rpc_restart_call_prepare(task);
4574 return -EAGAIN;
4575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004577
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004579 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004580 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581}
4582
Trond Myklebust5521abf2013-03-16 20:54:34 -04004583static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004584 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004585{
4586
4587 if (!nfs4_error_stateid_expired(task->tk_status) ||
4588 nfs4_stateid_is_current(&args->stateid,
4589 args->context,
4590 args->lock_context,
4591 FMODE_READ))
4592 return false;
4593 rpc_restart_call_prepare(task);
4594 return true;
4595}
4596
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004597static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004598{
4599
4600 dprintk("--> %s\n", __func__);
4601
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004602 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004603 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004604 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004605 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004606 if (task->tk_status > 0)
4607 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004608 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4609 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004610}
4611
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004612static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4613 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004615 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004616 if (!hdr->pgio_done_cb)
4617 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004618 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004619 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620}
4621
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004622static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4623 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004624{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004625 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004626 &hdr->args.seq_args,
4627 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004628 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004629 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004630 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4631 hdr->args.lock_context,
4632 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004633 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004634 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004635 return -EIO;
4636 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637}
4638
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004639static int nfs4_write_done_cb(struct rpc_task *task,
4640 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004642 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004643
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004644 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004645 if (task->tk_status < 0) {
4646 struct nfs4_exception exception = {
4647 .inode = hdr->inode,
4648 .state = hdr->args.context->state,
4649 .stateid = &hdr->args.stateid,
4650 };
4651 task->tk_status = nfs4_async_handle_exception(task,
4652 NFS_SERVER(inode), task->tk_status,
4653 &exception);
4654 if (exception.retry) {
4655 rpc_restart_call_prepare(task);
4656 return -EAGAIN;
4657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004659 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004660 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004661 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004662 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004663 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664}
4665
Trond Myklebust5521abf2013-03-16 20:54:34 -04004666static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004667 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004668{
4669
4670 if (!nfs4_error_stateid_expired(task->tk_status) ||
4671 nfs4_stateid_is_current(&args->stateid,
4672 args->context,
4673 args->lock_context,
4674 FMODE_WRITE))
4675 return false;
4676 rpc_restart_call_prepare(task);
4677 return true;
4678}
4679
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004680static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004681{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004682 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004683 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004684 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004685 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004686 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4687 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004688}
4689
Trond Myklebust5a37f852012-04-28 14:55:16 -04004690static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004691bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004692{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004693 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004694 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004695 return false;
4696 /* Otherwise, request attributes if and only if we don't hold
4697 * a delegation
4698 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004699 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004700}
Fred Isamana69aef12011-03-03 15:13:47 +00004701
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004702static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4703 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004705 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004706
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004707 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4708 hdr->args.bitmask = NULL;
4709 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004710 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004711 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004712
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004713 if (!hdr->pgio_done_cb)
4714 hdr->pgio_done_cb = nfs4_write_done_cb;
4715 hdr->res.server = server;
4716 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004718 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004719 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720}
4721
Fred Isaman0b7c0152012-04-20 14:47:39 -04004722static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4723{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004724 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004725 &data->args.seq_args,
4726 &data->res.seq_res,
4727 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728}
4729
Fred Isaman0b7c0152012-04-20 14:47:39 -04004730static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004733
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004734 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004735 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4736 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004737 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004738 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004740 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741}
4742
Fred Isaman0b7c0152012-04-20 14:47:39 -04004743static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004744{
4745 if (!nfs4_sequence_done(task, &data->res.seq_res))
4746 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004747 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004748}
4749
Fred Isaman0b7c0152012-04-20 14:47:39 -04004750static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004752 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004753
Fred Isaman0b7c0152012-04-20 14:47:39 -04004754 if (data->commit_done_cb == NULL)
4755 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004756 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004757 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004758 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759}
4760
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004761struct nfs4_renewdata {
4762 struct nfs_client *client;
4763 unsigned long timestamp;
4764};
4765
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766/*
4767 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4768 * standalone procedure for queueing an asynchronous RENEW.
4769 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004770static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004771{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004772 struct nfs4_renewdata *data = calldata;
4773 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004774
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004775 if (atomic_read(&clp->cl_count) > 1)
4776 nfs4_schedule_state_renewal(clp);
4777 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004778 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004779}
4780
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004781static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004783 struct nfs4_renewdata *data = calldata;
4784 struct nfs_client *clp = data->client;
4785 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004787 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004788 switch (task->tk_status) {
4789 case 0:
4790 break;
4791 case -NFS4ERR_LEASE_MOVED:
4792 nfs4_schedule_lease_moved_recovery(clp);
4793 break;
4794 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004795 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004796 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4797 return;
4798 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004799 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004800 return;
4801 }
4802 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004804 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805}
4806
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004807static const struct rpc_call_ops nfs4_renew_ops = {
4808 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004809 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004810};
4811
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004812static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813{
4814 struct rpc_message msg = {
4815 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4816 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004817 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004819 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004821 if (renew_flags == 0)
4822 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004823 if (!atomic_inc_not_zero(&clp->cl_count))
4824 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004825 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004826 if (data == NULL)
4827 return -ENOMEM;
4828 data->client = clp;
4829 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004830 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004831 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832}
4833
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004834static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835{
4836 struct rpc_message msg = {
4837 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4838 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004839 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 };
4841 unsigned long now = jiffies;
4842 int status;
4843
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004844 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 if (status < 0)
4846 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004847 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 return 0;
4849}
4850
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004851static inline int nfs4_server_supports_acls(struct nfs_server *server)
4852{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004853 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004854}
4855
Trond Myklebust21f498c2012-08-24 10:59:25 -04004856/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4857 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004858 * the stack.
4859 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004860#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004861
Neil Hormane9e3d722011-03-04 19:26:03 -05004862static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004863 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004864{
4865 struct page *newpage, **spages;
4866 int rc = 0;
4867 size_t len;
4868 spages = pages;
4869
4870 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004871 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004872 newpage = alloc_page(GFP_KERNEL);
4873
4874 if (newpage == NULL)
4875 goto unwind;
4876 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05004877 buf += len;
4878 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05004879 *pages++ = newpage;
4880 rc++;
4881 } while (buflen != 0);
4882
4883 return rc;
4884
4885unwind:
4886 for(; rc > 0; rc--)
4887 __free_page(spages[rc-1]);
4888 return -ENOMEM;
4889}
4890
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004891struct nfs4_cached_acl {
4892 int cached;
4893 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004894 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004895};
4896
4897static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004898{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004899 struct nfs_inode *nfsi = NFS_I(inode);
4900
4901 spin_lock(&inode->i_lock);
4902 kfree(nfsi->nfs4_acl);
4903 nfsi->nfs4_acl = acl;
4904 spin_unlock(&inode->i_lock);
4905}
4906
4907static void nfs4_zap_acl_attr(struct inode *inode)
4908{
4909 nfs4_set_cached_acl(inode, NULL);
4910}
4911
4912static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4913{
4914 struct nfs_inode *nfsi = NFS_I(inode);
4915 struct nfs4_cached_acl *acl;
4916 int ret = -ENOENT;
4917
4918 spin_lock(&inode->i_lock);
4919 acl = nfsi->nfs4_acl;
4920 if (acl == NULL)
4921 goto out;
4922 if (buf == NULL) /* user is just asking for length */
4923 goto out_len;
4924 if (acl->cached == 0)
4925 goto out;
4926 ret = -ERANGE; /* see getxattr(2) man page */
4927 if (acl->len > buflen)
4928 goto out;
4929 memcpy(buf, acl->data, acl->len);
4930out_len:
4931 ret = acl->len;
4932out:
4933 spin_unlock(&inode->i_lock);
4934 return ret;
4935}
4936
Sachin Prabhu5794d212012-04-17 14:36:40 +01004937static 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 +00004938{
4939 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004940 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004941
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004942 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004943 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004944 if (acl == NULL)
4945 goto out;
4946 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004947 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004948 } else {
4949 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4950 if (acl == NULL)
4951 goto out;
4952 acl->cached = 0;
4953 }
4954 acl->len = acl_len;
4955out:
4956 nfs4_set_cached_acl(inode, acl);
4957}
4958
Andy Adamsonbf118a32011-12-07 11:55:27 -05004959/*
4960 * The getxattr API returns the required buffer length when called with a
4961 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4962 * the required buf. On a NULL buf, we send a page of data to the server
4963 * guessing that the ACL request can be serviced by a page. If so, we cache
4964 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4965 * the cache. If not so, we throw away the page, and cache the required
4966 * length. The next getxattr call will then produce another round trip to
4967 * the server, this time with the input buf of the required size.
4968 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004969static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004970{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004971 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004972 struct nfs_getaclargs args = {
4973 .fh = NFS_FH(inode),
4974 .acl_pages = pages,
4975 .acl_len = buflen,
4976 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004977 struct nfs_getaclres res = {
4978 .acl_len = buflen,
4979 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004980 struct rpc_message msg = {
4981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4982 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004983 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004984 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004985 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4986 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004987
Andy Adamsonbf118a32011-12-07 11:55:27 -05004988 /* As long as we're doing a round trip to the server anyway,
4989 * let's be prepared for a page of acl data. */
4990 if (npages == 0)
4991 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004992 if (npages > ARRAY_SIZE(pages))
4993 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004994
Andy Adamsonbf118a32011-12-07 11:55:27 -05004995 for (i = 0; i < npages; i++) {
4996 pages[i] = alloc_page(GFP_KERNEL);
4997 if (!pages[i])
4998 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004999 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005000
5001 /* for decoding across pages */
5002 res.acl_scratch = alloc_page(GFP_KERNEL);
5003 if (!res.acl_scratch)
5004 goto out_free;
5005
Andy Adamsonbf118a32011-12-07 11:55:27 -05005006 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005007
Peng Taode040be2012-01-10 22:42:47 +08005008 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005009 __func__, buf, buflen, npages, args.acl_len);
5010 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5011 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005012 if (ret)
5013 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005014
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005015 /* Handle the case where the passed-in buffer is too short */
5016 if (res.acl_flags & NFS4_ACL_TRUNC) {
5017 /* Did the user only issue a request for the acl length? */
5018 if (buf == NULL)
5019 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005020 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005021 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005022 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005023 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005024 if (buf) {
5025 if (res.acl_len > buflen) {
5026 ret = -ERANGE;
5027 goto out_free;
5028 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005029 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005030 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005031out_ok:
5032 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005033out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005034 for (i = 0; i < npages; i++)
5035 if (pages[i])
5036 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005037 if (res.acl_scratch)
5038 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005039 return ret;
5040}
5041
Trond Myklebust16b42892006-08-24 12:27:15 -04005042static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5043{
5044 struct nfs4_exception exception = { };
5045 ssize_t ret;
5046 do {
5047 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005048 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005049 if (ret >= 0)
5050 break;
5051 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5052 } while (exception.retry);
5053 return ret;
5054}
5055
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005056static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5057{
5058 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005059 int ret;
5060
5061 if (!nfs4_server_supports_acls(server))
5062 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005063 ret = nfs_revalidate_inode(server, inode);
5064 if (ret < 0)
5065 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005066 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5067 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005068 ret = nfs4_read_cached_acl(inode, buf, buflen);
5069 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005070 /* -ENOENT is returned if there is no ACL or if there is an ACL
5071 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005072 return ret;
5073 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005074}
5075
Trond Myklebust16b42892006-08-24 12:27:15 -04005076static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005077{
5078 struct nfs_server *server = NFS_SERVER(inode);
5079 struct page *pages[NFS4ACL_MAXPAGES];
5080 struct nfs_setaclargs arg = {
5081 .fh = NFS_FH(inode),
5082 .acl_pages = pages,
5083 .acl_len = buflen,
5084 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005085 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005086 struct rpc_message msg = {
5087 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5088 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005089 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005090 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005091 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005092 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005093
5094 if (!nfs4_server_supports_acls(server))
5095 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005096 if (npages > ARRAY_SIZE(pages))
5097 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005098 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005099 if (i < 0)
5100 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005101 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005102 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005103
5104 /*
5105 * Free each page after tx, so the only ref left is
5106 * held by the network stack
5107 */
5108 for (; i > 0; i--)
5109 put_page(pages[i-1]);
5110
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005111 /*
5112 * Acl update can result in inode attribute update.
5113 * so mark the attribute cache invalid.
5114 */
5115 spin_lock(&inode->i_lock);
5116 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5117 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005118 nfs_access_zap_cache(inode);
5119 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005120 return ret;
5121}
5122
Trond Myklebust16b42892006-08-24 12:27:15 -04005123static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5124{
5125 struct nfs4_exception exception = { };
5126 int err;
5127 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005128 err = __nfs4_proc_set_acl(inode, buf, buflen);
5129 trace_nfs4_set_acl(inode, err);
5130 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005131 &exception);
5132 } while (exception.retry);
5133 return err;
5134}
5135
David Quigleyaa9c2662013-05-22 12:50:44 -04005136#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5137static int _nfs4_get_security_label(struct inode *inode, void *buf,
5138 size_t buflen)
5139{
5140 struct nfs_server *server = NFS_SERVER(inode);
5141 struct nfs_fattr fattr;
5142 struct nfs4_label label = {0, 0, buflen, buf};
5143
5144 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005145 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005146 .fh = NFS_FH(inode),
5147 .bitmask = bitmask,
5148 };
5149 struct nfs4_getattr_res res = {
5150 .fattr = &fattr,
5151 .label = &label,
5152 .server = server,
5153 };
5154 struct rpc_message msg = {
5155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005156 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005157 .rpc_resp = &res,
5158 };
5159 int ret;
5160
5161 nfs_fattr_init(&fattr);
5162
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005163 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005164 if (ret)
5165 return ret;
5166 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5167 return -ENOENT;
5168 if (buflen < label.len)
5169 return -ERANGE;
5170 return 0;
5171}
5172
5173static int nfs4_get_security_label(struct inode *inode, void *buf,
5174 size_t buflen)
5175{
5176 struct nfs4_exception exception = { };
5177 int err;
5178
5179 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5180 return -EOPNOTSUPP;
5181
5182 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005183 err = _nfs4_get_security_label(inode, buf, buflen);
5184 trace_nfs4_get_security_label(inode, err);
5185 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005186 &exception);
5187 } while (exception.retry);
5188 return err;
5189}
5190
5191static int _nfs4_do_set_security_label(struct inode *inode,
5192 struct nfs4_label *ilabel,
5193 struct nfs_fattr *fattr,
5194 struct nfs4_label *olabel)
5195{
5196
5197 struct iattr sattr = {0};
5198 struct nfs_server *server = NFS_SERVER(inode);
5199 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005200 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005201 .fh = NFS_FH(inode),
5202 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005203 .server = server,
5204 .bitmask = bitmask,
5205 .label = ilabel,
5206 };
5207 struct nfs_setattrres res = {
5208 .fattr = fattr,
5209 .label = olabel,
5210 .server = server,
5211 };
5212 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005213 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5214 .rpc_argp = &arg,
5215 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005216 };
5217 int status;
5218
Jeff Layton12207f62013-11-01 10:49:32 -04005219 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005220
Jeff Layton12207f62013-11-01 10:49:32 -04005221 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005222 if (status)
5223 dprintk("%s failed: %d\n", __func__, status);
5224
5225 return status;
5226}
5227
5228static int nfs4_do_set_security_label(struct inode *inode,
5229 struct nfs4_label *ilabel,
5230 struct nfs_fattr *fattr,
5231 struct nfs4_label *olabel)
5232{
5233 struct nfs4_exception exception = { };
5234 int err;
5235
5236 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005237 err = _nfs4_do_set_security_label(inode, ilabel,
5238 fattr, olabel);
5239 trace_nfs4_set_security_label(inode, err);
5240 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005241 &exception);
5242 } while (exception.retry);
5243 return err;
5244}
5245
5246static int
Al Viro59301222016-05-27 10:19:30 -04005247nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005248{
5249 struct nfs4_label ilabel, *olabel = NULL;
5250 struct nfs_fattr fattr;
5251 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005252 int status;
5253
5254 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5255 return -EOPNOTSUPP;
5256
5257 nfs_fattr_init(&fattr);
5258
5259 ilabel.pi = 0;
5260 ilabel.lfs = 0;
5261 ilabel.label = (char *)buf;
5262 ilabel.len = buflen;
5263
5264 cred = rpc_lookup_cred();
5265 if (IS_ERR(cred))
5266 return PTR_ERR(cred);
5267
5268 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5269 if (IS_ERR(olabel)) {
5270 status = -PTR_ERR(olabel);
5271 goto out;
5272 }
5273
5274 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5275 if (status == 0)
5276 nfs_setsecurity(inode, &fattr, olabel);
5277
5278 nfs4_label_free(olabel);
5279out:
5280 put_rpccred(cred);
5281 return status;
5282}
5283#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5284
5285
Chuck Leverf0920752012-05-21 22:45:41 -04005286static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5287 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005288{
5289 __be32 verf[2];
5290
Chuck Lever2c820d92012-05-21 22:45:33 -04005291 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5292 /* An impossible timestamp guarantees this value
5293 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005294 verf[0] = cpu_to_be32(U32_MAX);
5295 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005296 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005297 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005298 u64 ns = ktime_to_ns(nn->boot_time);
5299
5300 verf[0] = cpu_to_be32(ns >> 32);
5301 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005302 }
Chuck Levercd937102012-03-02 17:14:31 -05005303 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5304}
5305
Jeff Laytona3192682015-06-09 19:43:59 -04005306static int
5307nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005308{
Jeff Laytona3192682015-06-09 19:43:59 -04005309 size_t len;
5310 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005311
Trond Myklebustceb3a162015-01-03 15:16:04 -05005312 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005313 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005314
Jeff Laytona3192682015-06-09 19:43:59 -04005315 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005316 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005317 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5318 1 +
5319 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5320 1;
5321 rcu_read_unlock();
5322
5323 if (len > NFS4_OPAQUE_LIMIT + 1)
5324 return -EINVAL;
5325
5326 /*
5327 * Since this string is allocated at mount time, and held until the
5328 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5329 * about a memory-reclaim deadlock.
5330 */
5331 str = kmalloc(len, GFP_KERNEL);
5332 if (!str)
5333 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005334
Chuck Levere984a552012-09-14 17:24:21 -04005335 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005336 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005337 clp->cl_ipaddr,
5338 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5339 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005340 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005341
Jeff Laytona3192682015-06-09 19:43:59 -04005342 clp->cl_owner_id = str;
5343 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005344}
5345
Jeff Layton873e3852015-06-09 19:44:00 -04005346static int
5347nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005348{
Jeff Layton873e3852015-06-09 19:44:00 -04005349 size_t len;
5350 char *str;
5351
5352 len = 10 + 10 + 1 + 10 + 1 +
5353 strlen(nfs4_client_id_uniquifier) + 1 +
5354 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5355
5356 if (len > NFS4_OPAQUE_LIMIT + 1)
5357 return -EINVAL;
5358
5359 /*
5360 * Since this string is allocated at mount time, and held until the
5361 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5362 * about a memory-reclaim deadlock.
5363 */
5364 str = kmalloc(len, GFP_KERNEL);
5365 if (!str)
5366 return -ENOMEM;
5367
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005368 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005369 clp->rpc_ops->version, clp->cl_minorversion,
5370 nfs4_client_id_uniquifier,
5371 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005372 clp->cl_owner_id = str;
5373 return 0;
5374}
5375
5376static int
5377nfs4_init_uniform_client_string(struct nfs_client *clp)
5378{
Jeff Layton873e3852015-06-09 19:44:00 -04005379 size_t len;
5380 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005381
5382 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005383 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005384
5385 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005386 return nfs4_init_uniquifier_client_string(clp);
5387
5388 len = 10 + 10 + 1 + 10 + 1 +
5389 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5390
5391 if (len > NFS4_OPAQUE_LIMIT + 1)
5392 return -EINVAL;
5393
5394 /*
5395 * Since this string is allocated at mount time, and held until the
5396 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5397 * about a memory-reclaim deadlock.
5398 */
5399 str = kmalloc(len, GFP_KERNEL);
5400 if (!str)
5401 return -ENOMEM;
5402
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005403 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005404 clp->rpc_ops->version, clp->cl_minorversion,
5405 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005406 clp->cl_owner_id = str;
5407 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005408}
5409
Chuck Lever706cb8d2014-03-12 12:51:47 -04005410/*
5411 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5412 * services. Advertise one based on the address family of the
5413 * clientaddr.
5414 */
5415static unsigned int
5416nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5417{
5418 if (strchr(clp->cl_ipaddr, ':') != NULL)
5419 return scnprintf(buf, len, "tcp6");
5420 else
5421 return scnprintf(buf, len, "tcp");
5422}
5423
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005424static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5425{
5426 struct nfs4_setclientid *sc = calldata;
5427
5428 if (task->tk_status == 0)
5429 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5430}
5431
5432static const struct rpc_call_ops nfs4_setclientid_ops = {
5433 .rpc_call_done = nfs4_setclientid_done,
5434};
5435
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005436/**
5437 * nfs4_proc_setclientid - Negotiate client ID
5438 * @clp: state data structure
5439 * @program: RPC program for NFSv4 callback service
5440 * @port: IP port number for NFS4 callback service
5441 * @cred: RPC credential to use for this call
5442 * @res: where to place the result
5443 *
5444 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5445 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005446int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5447 unsigned short port, struct rpc_cred *cred,
5448 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449{
5450 nfs4_verifier sc_verifier;
5451 struct nfs4_setclientid setclientid = {
5452 .sc_verifier = &sc_verifier,
5453 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005454 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 };
5456 struct rpc_message msg = {
5457 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5458 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005459 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005460 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005462 struct rpc_task *task;
5463 struct rpc_task_setup task_setup_data = {
5464 .rpc_client = clp->cl_rpcclient,
5465 .rpc_message = &msg,
5466 .callback_ops = &nfs4_setclientid_ops,
5467 .callback_data = &setclientid,
5468 .flags = RPC_TASK_TIMEOUT,
5469 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005470 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471
Chuck Leverde734832012-07-11 16:30:50 -04005472 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005473 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005474
5475 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5476 status = nfs4_init_uniform_client_string(clp);
5477 else
Jeff Laytona3192682015-06-09 19:43:59 -04005478 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005479
5480 if (status)
5481 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005482
Chuck Leverde734832012-07-11 16:30:50 -04005483 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005484 setclientid.sc_netid_len =
5485 nfs4_init_callback_netid(clp,
5486 setclientid.sc_netid,
5487 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005488 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005489 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 clp->cl_ipaddr, port >> 8, port & 255);
5491
Jeff Layton3a6bb732015-06-09 19:43:57 -04005492 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005493 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005494 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005495 task = rpc_run_task(&task_setup_data);
5496 if (IS_ERR(task)) {
5497 status = PTR_ERR(task);
5498 goto out;
5499 }
5500 status = task->tk_status;
5501 if (setclientid.sc_cred) {
5502 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5503 put_rpccred(setclientid.sc_cred);
5504 }
5505 rpc_put_task(task);
5506out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005507 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005508 dprintk("NFS reply setclientid: %d\n", status);
5509 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510}
5511
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005512/**
5513 * nfs4_proc_setclientid_confirm - Confirm client ID
5514 * @clp: state data structure
5515 * @res: result of a previous SETCLIENTID
5516 * @cred: RPC credential to use for this call
5517 *
5518 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5519 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005520int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005521 struct nfs4_setclientid_res *arg,
5522 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 struct rpc_message msg = {
5525 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005526 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005527 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 int status;
5530
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005531 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5532 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5533 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005534 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005535 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005536 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 return status;
5538}
5539
Trond Myklebustfe650402006-01-03 09:55:18 +01005540struct nfs4_delegreturndata {
5541 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005542 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005543 struct nfs_fh fh;
5544 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005545 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005546 struct {
5547 struct nfs4_layoutreturn_args arg;
5548 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005549 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005550 u32 roc_barrier;
5551 bool roc;
5552 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005553 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005554 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005555 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005556};
5557
Trond Myklebustfe650402006-01-03 09:55:18 +01005558static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5559{
5560 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005561
Trond Myklebust14516c32010-07-31 14:29:06 -04005562 if (!nfs4_sequence_done(task, &data->res.seq_res))
5563 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005564
Trond Myklebustca8acf82013-08-13 10:36:56 -04005565 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005566
5567 /* Handle Layoutreturn errors */
5568 if (data->args.lr_args && task->tk_status != 0) {
5569 switch(data->res.lr_ret) {
5570 default:
5571 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5572 break;
5573 case 0:
5574 data->args.lr_args = NULL;
5575 data->res.lr_res = NULL;
5576 break;
5577 case -NFS4ERR_ADMIN_REVOKED:
5578 case -NFS4ERR_DELEG_REVOKED:
5579 case -NFS4ERR_EXPIRED:
5580 case -NFS4ERR_BAD_STATEID:
5581 case -NFS4ERR_OLD_STATEID:
5582 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5583 case -NFS4ERR_WRONG_CRED:
5584 data->args.lr_args = NULL;
5585 data->res.lr_res = NULL;
5586 data->res.lr_ret = 0;
5587 rpc_restart_call_prepare(task);
5588 return;
5589 }
5590 }
5591
Ricardo Labiaga79708862009-12-07 09:23:21 -05005592 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005593 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005594 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005595 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005596 case -NFS4ERR_ADMIN_REVOKED:
5597 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005598 case -NFS4ERR_EXPIRED:
5599 nfs4_free_revoked_stateid(data->res.server,
5600 data->args.stateid,
5601 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005602 case -NFS4ERR_BAD_STATEID:
5603 case -NFS4ERR_OLD_STATEID:
5604 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005605 task->tk_status = 0;
5606 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005607 case -NFS4ERR_ACCESS:
5608 if (data->args.bitmask) {
5609 data->args.bitmask = NULL;
5610 data->res.fattr = NULL;
5611 task->tk_status = 0;
5612 rpc_restart_call_prepare(task);
5613 return;
5614 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005615 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005616 if (nfs4_async_handle_error(task, data->res.server,
5617 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005618 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005619 return;
5620 }
5621 }
5622 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005623}
5624
5625static void nfs4_delegreturn_release(void *calldata)
5626{
Peng Tao039b7562014-07-03 13:05:02 +08005627 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005628 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005629
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005630 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005631 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005632 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5633 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005634 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005635 nfs_iput_and_deactive(inode);
5636 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005637 kfree(calldata);
5638}
5639
Andy Adamson938e1012009-04-01 09:22:28 -04005640static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5641{
5642 struct nfs4_delegreturndata *d_data;
5643
5644 d_data = (struct nfs4_delegreturndata *)data;
5645
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005646 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005647 return;
5648
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005649 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005650 &d_data->args.seq_args,
5651 &d_data->res.seq_res,
5652 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005653}
Andy Adamson938e1012009-04-01 09:22:28 -04005654
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005655static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005656 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005657 .rpc_call_done = nfs4_delegreturn_done,
5658 .rpc_release = nfs4_delegreturn_release,
5659};
5660
Trond Myklebuste6f81072008-01-24 18:14:34 -05005661static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005662{
5663 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005664 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005665 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005666 struct rpc_message msg = {
5667 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5668 .rpc_cred = cred,
5669 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005670 struct rpc_task_setup task_setup_data = {
5671 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005672 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005673 .callback_ops = &nfs4_delegreturn_ops,
5674 .flags = RPC_TASK_ASYNC,
5675 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005676 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005677
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005678 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005679 if (data == NULL)
5680 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005681 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005682
5683 nfs4_state_protect(server->nfs_client,
5684 NFS_SP4_MACH_CRED_CLEANUP,
5685 &task_setup_data.rpc_client, &msg);
5686
Trond Myklebustfe650402006-01-03 09:55:18 +01005687 data->args.fhandle = &data->fh;
5688 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005689 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005690 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005691 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005692 data->res.fattr = &data->fattr;
5693 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005694 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005695 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005696 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005697 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005698 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005699 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005700 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005701 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005702 if (data->lr.roc) {
5703 data->args.lr_args = &data->lr.arg;
5704 data->res.lr_res = &data->lr.res;
5705 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005706 } else if (data->lr.roc) {
5707 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5708 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005709 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005710
Trond Myklebustc970aa82007-07-14 15:39:59 -04005711 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005712 msg.rpc_argp = &data->args;
5713 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005714 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005715 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005716 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005717 if (!issync)
5718 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005719 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005720 if (status != 0)
5721 goto out;
5722 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005723out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005724 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005725 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726}
5727
Trond Myklebuste6f81072008-01-24 18:14:34 -05005728int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729{
5730 struct nfs_server *server = NFS_SERVER(inode);
5731 struct nfs4_exception exception = { };
5732 int err;
5733 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005734 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005735 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 switch (err) {
5737 case -NFS4ERR_STALE_STATEID:
5738 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 case 0:
5740 return 0;
5741 }
5742 err = nfs4_handle_exception(server, err, &exception);
5743 } while (exception.retry);
5744 return err;
5745}
5746
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5748{
5749 struct inode *inode = state->inode;
5750 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005751 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005752 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005754 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005756 struct nfs_lockt_res res = {
5757 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 };
5759 struct rpc_message msg = {
5760 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005761 .rpc_argp = &arg,
5762 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 .rpc_cred = state->owner->so_cred,
5764 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 struct nfs4_lock_state *lsp;
5766 int status;
5767
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005768 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005769 status = nfs4_set_lock_state(state, request);
5770 if (status != 0)
5771 goto out;
5772 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005773 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005774 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005775 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005776 switch (status) {
5777 case 0:
5778 request->fl_type = F_UNLCK;
5779 break;
5780 case -NFS4ERR_DENIED:
5781 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005783 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005784 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005785out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 return status;
5787}
5788
5789static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5790{
5791 struct nfs4_exception exception = { };
5792 int err;
5793
5794 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005795 err = _nfs4_proc_getlk(state, cmd, request);
5796 trace_nfs4_get_lock(request, state, cmd, err);
5797 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 &exception);
5799 } while (exception.retry);
5800 return err;
5801}
5802
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005803struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005804 struct nfs_locku_args arg;
5805 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005806 struct nfs4_lock_state *lsp;
5807 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005808 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005809 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005810 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005811};
5812
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005813static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5814 struct nfs_open_context *ctx,
5815 struct nfs4_lock_state *lsp,
5816 struct nfs_seqid *seqid)
5817{
5818 struct nfs4_unlockdata *p;
5819 struct inode *inode = lsp->ls_state->inode;
5820
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005821 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005822 if (p == NULL)
5823 return NULL;
5824 p->arg.fh = NFS_FH(inode);
5825 p->arg.fl = &p->fl;
5826 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005827 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005828 p->lsp = lsp;
5829 atomic_inc(&lsp->ls_count);
5830 /* Ensure we don't close file until we're done freeing locks! */
5831 p->ctx = get_nfs_open_context(ctx);
5832 memcpy(&p->fl, fl, sizeof(p->fl));
5833 p->server = NFS_SERVER(inode);
5834 return p;
5835}
5836
Trond Myklebust06f814a2006-01-03 09:55:07 +01005837static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005838{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005839 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005840 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005841 nfs4_put_lock_state(calldata->lsp);
5842 put_nfs_open_context(calldata->ctx);
5843 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005844}
5845
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005846static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005847{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005848 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005849
Trond Myklebust14516c32010-07-31 14:29:06 -04005850 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5851 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005852 switch (task->tk_status) {
5853 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005854 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005855 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005856 if (nfs4_update_lock_stateid(calldata->lsp,
5857 &calldata->res.stateid))
5858 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005859 case -NFS4ERR_ADMIN_REVOKED:
5860 case -NFS4ERR_EXPIRED:
5861 nfs4_free_revoked_stateid(calldata->server,
5862 &calldata->arg.stateid,
5863 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005864 case -NFS4ERR_BAD_STATEID:
5865 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005866 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005867 if (!nfs4_stateid_match(&calldata->arg.stateid,
5868 &calldata->lsp->ls_stateid))
5869 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005870 break;
5871 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005872 if (nfs4_async_handle_error(task, calldata->server,
5873 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005874 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005875 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005876 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005877}
5878
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005879static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005880{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005881 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005883 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005884 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005885 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005886 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005887 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005888 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005889 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005890 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005891 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04005892 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005893 &calldata->res.seq_res,
5894 task) != 0)
5895 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005896 return;
5897out_no_action:
5898 task->tk_action = NULL;
5899out_wait:
5900 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901}
5902
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005903static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005904 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005905 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005906 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005907};
5908
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005909static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5910 struct nfs_open_context *ctx,
5911 struct nfs4_lock_state *lsp,
5912 struct nfs_seqid *seqid)
5913{
5914 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005915 struct rpc_message msg = {
5916 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5917 .rpc_cred = ctx->cred,
5918 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005919 struct rpc_task_setup task_setup_data = {
5920 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005921 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005922 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005923 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005924 .flags = RPC_TASK_ASYNC,
5925 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005926
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005927 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5928 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5929
Frank Filz137d6ac2007-07-09 15:32:29 -07005930 /* Ensure this is an unlock - when canceling a lock, the
5931 * canceled lock is passed in, and it won't be an unlock.
5932 */
5933 fl->fl_type = F_UNLCK;
5934
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005935 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5936 if (data == NULL) {
5937 nfs_free_seqid(seqid);
5938 return ERR_PTR(-ENOMEM);
5939 }
5940
Chuck Levera9c92d62013-08-09 12:48:18 -04005941 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005942 msg.rpc_argp = &data->arg;
5943 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005944 task_setup_data.callback_data = data;
5945 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005946}
5947
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5949{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005950 struct inode *inode = state->inode;
5951 struct nfs4_state_owner *sp = state->owner;
5952 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005953 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005954 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005955 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005956 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005957 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005958 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959
Trond Myklebust9b073572006-06-29 16:38:34 -04005960 status = nfs4_set_lock_state(state, request);
5961 /* Unlock _before_ we do the RPC call */
5962 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005963 /* Exclude nfs_delegation_claim_locks() */
5964 mutex_lock(&sp->so_delegreturn_mutex);
5965 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005966 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005967 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005968 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005969 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005970 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005971 }
5972 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005973 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005974 if (status != 0)
5975 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005976 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005977 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005978 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5979 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005980 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5981 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005982 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005983 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005984 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005985 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005986 status = PTR_ERR(task);
5987 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005988 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005989 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005990 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005991out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005992 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005993 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005994 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995}
5996
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005997struct nfs4_lockdata {
5998 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005999 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006000 struct nfs4_lock_state *lsp;
6001 struct nfs_open_context *ctx;
6002 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006003 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006004 int rpc_status;
6005 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006006 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006007};
6008
6009static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006010 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6011 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006012{
6013 struct nfs4_lockdata *p;
6014 struct inode *inode = lsp->ls_state->inode;
6015 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006016 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006017
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006018 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006019 if (p == NULL)
6020 return NULL;
6021
6022 p->arg.fh = NFS_FH(inode);
6023 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006024 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006025 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006026 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006027 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6028 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006029 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006030 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006031 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006032 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006033 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006034 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006035 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006036 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006037 atomic_inc(&lsp->ls_count);
6038 p->ctx = get_nfs_open_context(ctx);
6039 memcpy(&p->fl, fl, sizeof(p->fl));
6040 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006041out_free_seqid:
6042 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006043out_free:
6044 kfree(p);
6045 return NULL;
6046}
6047
6048static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6049{
6050 struct nfs4_lockdata *data = calldata;
6051 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052
Harvey Harrison3110ff82008-05-02 13:42:44 -07006053 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006054 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006055 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006056 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006057 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006058 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006059 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006060 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006061 nfs4_stateid_copy(&data->arg.open_stateid,
6062 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006063 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006064 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006065 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006066 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006067 nfs4_stateid_copy(&data->arg.lock_stateid,
6068 &data->lsp->ls_stateid);
6069 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006070 if (!nfs4_valid_open_stateid(state)) {
6071 data->rpc_status = -EBADF;
6072 task->tk_action = NULL;
6073 goto out_release_open_seqid;
6074 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006075 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006076 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006077 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006078 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006079 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006080 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006081out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006082 nfs_release_seqid(data->arg.open_seqid);
6083out_release_lock_seqid:
6084 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006085out_wait:
6086 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006087 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006088}
6089
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006090static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6091{
6092 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006093 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006094
Harvey Harrison3110ff82008-05-02 13:42:44 -07006095 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006096
Trond Myklebust14516c32010-07-31 14:29:06 -04006097 if (!nfs4_sequence_done(task, &data->res.seq_res))
6098 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006099
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006100 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006101 switch (task->tk_status) {
6102 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006103 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006104 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006105 if (data->arg.new_lock) {
6106 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006107 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006108 rpc_restart_call_prepare(task);
6109 break;
6110 }
6111 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006112 if (data->arg.new_lock_owner != 0) {
6113 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6114 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6115 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6116 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6117 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006118 break;
6119 case -NFS4ERR_BAD_STATEID:
6120 case -NFS4ERR_OLD_STATEID:
6121 case -NFS4ERR_STALE_STATEID:
6122 case -NFS4ERR_EXPIRED:
6123 if (data->arg.new_lock_owner != 0) {
6124 if (!nfs4_stateid_match(&data->arg.open_stateid,
6125 &lsp->ls_state->open_stateid))
6126 rpc_restart_call_prepare(task);
6127 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6128 &lsp->ls_stateid))
6129 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006130 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006131 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006132}
6133
6134static void nfs4_lock_release(void *calldata)
6135{
6136 struct nfs4_lockdata *data = calldata;
6137
Harvey Harrison3110ff82008-05-02 13:42:44 -07006138 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006139 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006140 if (data->cancelled != 0) {
6141 struct rpc_task *task;
6142 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6143 data->arg.lock_seqid);
6144 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006145 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006146 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006147 } else
6148 nfs_free_seqid(data->arg.lock_seqid);
6149 nfs4_put_lock_state(data->lsp);
6150 put_nfs_open_context(data->ctx);
6151 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006152 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006153}
6154
6155static const struct rpc_call_ops nfs4_lock_ops = {
6156 .rpc_call_prepare = nfs4_lock_prepare,
6157 .rpc_call_done = nfs4_lock_done,
6158 .rpc_release = nfs4_lock_release,
6159};
6160
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006161static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6162{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006163 switch (error) {
6164 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006165 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006166 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006167 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006168 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006169 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006170 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006171 break;
6172 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006173 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006174 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006175 };
6176}
6177
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006178static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006179{
6180 struct nfs4_lockdata *data;
6181 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006182 struct rpc_message msg = {
6183 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6184 .rpc_cred = state->owner->so_cred,
6185 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006186 struct rpc_task_setup task_setup_data = {
6187 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006188 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006189 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006190 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006191 .flags = RPC_TASK_ASYNC,
6192 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006193 int ret;
6194
Harvey Harrison3110ff82008-05-02 13:42:44 -07006195 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006196 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006197 fl->fl_u.nfs4_fl.owner,
6198 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006199 if (data == NULL)
6200 return -ENOMEM;
6201 if (IS_SETLKW(cmd))
6202 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006203 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006204 msg.rpc_argp = &data->arg;
6205 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006206 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006207 if (recovery_type > NFS_LOCK_NEW) {
6208 if (recovery_type == NFS_LOCK_RECLAIM)
6209 data->arg.reclaim = NFS_LOCK_RECLAIM;
6210 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006211 } else
6212 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006213 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006214 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006215 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006216 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006217 if (ret == 0) {
6218 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006219 if (ret)
6220 nfs4_handle_setlk_error(data->server, data->lsp,
6221 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006222 } else
6223 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006224 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006225 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006226 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006227 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228}
6229
6230static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6231{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006232 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006233 struct nfs4_exception exception = {
6234 .inode = state->inode,
6235 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006236 int err;
6237
6238 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006239 /* Cache the lock if possible... */
6240 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6241 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006242 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006243 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006244 break;
6245 nfs4_handle_exception(server, err, &exception);
6246 } while (exception.retry);
6247 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248}
6249
6250static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6251{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006252 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006253 struct nfs4_exception exception = {
6254 .inode = state->inode,
6255 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006256 int err;
6257
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006258 err = nfs4_set_lock_state(state, request);
6259 if (err != 0)
6260 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006261 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006262 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6263 return 0;
6264 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006265 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006266 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6267 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006268 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006269 switch (err) {
6270 default:
6271 goto out;
6272 case -NFS4ERR_GRACE:
6273 case -NFS4ERR_DELAY:
6274 nfs4_handle_exception(server, err, &exception);
6275 err = 0;
6276 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006277 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006278out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006279 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280}
6281
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006282#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006283static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6284{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006285 struct nfs4_lock_state *lsp;
6286 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006287
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006288 status = nfs4_set_lock_state(state, request);
6289 if (status != 0)
6290 return status;
6291 lsp = request->fl_u.nfs4_fl.owner;
6292 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6293 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6294 return 0;
6295 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006296 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006297}
6298#endif
6299
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6301{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006302 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006303 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006304 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006305 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306
Trond Myklebust01c3b862006-06-29 16:38:39 -04006307 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006308 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006309 if (status < 0)
6310 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006311 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006312 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006313 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006314 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006315 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006316 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006317 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006318 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006319 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006320 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006321 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006322 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006323 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006324 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006325out:
6326 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327 return status;
6328}
6329
6330static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6331{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006332 struct nfs4_exception exception = {
6333 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006334 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006335 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336 int err;
6337
6338 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006339 err = _nfs4_proc_setlk(state, cmd, request);
6340 if (err == -NFS4ERR_DENIED)
6341 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006343 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344 } while (exception.retry);
6345 return err;
6346}
6347
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006348#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6349#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6350
6351static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006352nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6353 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006354{
6355 int status = -ERESTARTSYS;
6356 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6357
6358 while(!signalled()) {
6359 status = nfs4_proc_setlk(state, cmd, request);
6360 if ((status != -EAGAIN) || IS_SETLK(cmd))
6361 break;
6362 freezable_schedule_timeout_interruptible(timeout);
6363 timeout *= 2;
6364 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6365 status = -ERESTARTSYS;
6366 }
6367 return status;
6368}
6369
Jeff Laytona1d617d82016-09-17 18:17:39 -04006370#ifdef CONFIG_NFS_V4_1
6371struct nfs4_lock_waiter {
6372 struct task_struct *task;
6373 struct inode *inode;
6374 struct nfs_lowner *owner;
6375 bool notified;
6376};
6377
6378static int
6379nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6380{
6381 int ret;
6382 struct cb_notify_lock_args *cbnl = key;
6383 struct nfs4_lock_waiter *waiter = wait->private;
6384 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6385 *wowner = waiter->owner;
6386
6387 /* Only wake if the callback was for the same owner */
6388 if (lowner->clientid != wowner->clientid ||
6389 lowner->id != wowner->id ||
6390 lowner->s_dev != wowner->s_dev)
6391 return 0;
6392
6393 /* Make sure it's for the right inode */
6394 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6395 return 0;
6396
6397 waiter->notified = true;
6398
6399 /* override "private" so we can use default_wake_function */
6400 wait->private = waiter->task;
6401 ret = autoremove_wake_function(wait, mode, flags, key);
6402 wait->private = waiter;
6403 return ret;
6404}
6405
6406static int
6407nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6408{
6409 int status = -ERESTARTSYS;
6410 unsigned long flags;
6411 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6412 struct nfs_server *server = NFS_SERVER(state->inode);
6413 struct nfs_client *clp = server->nfs_client;
6414 wait_queue_head_t *q = &clp->cl_lock_waitq;
6415 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6416 .id = lsp->ls_seqid.owner_id,
6417 .s_dev = server->s_dev };
6418 struct nfs4_lock_waiter waiter = { .task = current,
6419 .inode = state->inode,
6420 .owner = &owner,
6421 .notified = false };
6422 wait_queue_t wait;
6423
6424 /* Don't bother with waitqueue if we don't expect a callback */
6425 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6426 return nfs4_retry_setlk_simple(state, cmd, request);
6427
6428 init_wait(&wait);
6429 wait.private = &waiter;
6430 wait.func = nfs4_wake_lock_waiter;
6431 add_wait_queue(q, &wait);
6432
6433 while(!signalled()) {
6434 status = nfs4_proc_setlk(state, cmd, request);
6435 if ((status != -EAGAIN) || IS_SETLK(cmd))
6436 break;
6437
6438 status = -ERESTARTSYS;
6439 spin_lock_irqsave(&q->lock, flags);
6440 if (waiter.notified) {
6441 spin_unlock_irqrestore(&q->lock, flags);
6442 continue;
6443 }
6444 set_current_state(TASK_INTERRUPTIBLE);
6445 spin_unlock_irqrestore(&q->lock, flags);
6446
6447 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6448 }
6449
6450 finish_wait(q, &wait);
6451 return status;
6452}
6453#else /* !CONFIG_NFS_V4_1 */
6454static inline int
6455nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6456{
6457 return nfs4_retry_setlk_simple(state, cmd, request);
6458}
6459#endif
6460
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461static int
6462nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6463{
6464 struct nfs_open_context *ctx;
6465 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 int status;
6467
6468 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006469 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 state = ctx->state;
6471
6472 if (request->fl_start < 0 || request->fl_end < 0)
6473 return -EINVAL;
6474
Trond Myklebustd9531262009-07-21 19:22:38 -04006475 if (IS_GETLK(cmd)) {
6476 if (state != NULL)
6477 return nfs4_proc_getlk(state, F_GETLK, request);
6478 return 0;
6479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480
6481 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6482 return -EINVAL;
6483
Trond Myklebustd9531262009-07-21 19:22:38 -04006484 if (request->fl_type == F_UNLCK) {
6485 if (state != NULL)
6486 return nfs4_proc_unlck(state, cmd, request);
6487 return 0;
6488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489
Trond Myklebustd9531262009-07-21 19:22:38 -04006490 if (state == NULL)
6491 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006492
6493 if ((request->fl_flags & FL_POSIX) &&
6494 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6495 return -ENOLCK;
6496
Trond Myklebust55725512012-04-18 12:48:35 -04006497 /*
6498 * Don't rely on the VFS having checked the file open mode,
6499 * since it won't do this for flock() locks.
6500 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006501 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006502 case F_RDLCK:
6503 if (!(filp->f_mode & FMODE_READ))
6504 return -EBADF;
6505 break;
6506 case F_WRLCK:
6507 if (!(filp->f_mode & FMODE_WRITE))
6508 return -EBADF;
6509 }
6510
Jeff Layton1ea67db2016-09-17 18:17:37 -04006511 status = nfs4_set_lock_state(state, request);
6512 if (status != 0)
6513 return status;
6514
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006515 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516}
6517
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006518int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006519{
6520 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006521 int err;
6522
6523 err = nfs4_set_lock_state(state, fl);
6524 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006525 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006526 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006527 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006528}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006529
Trond Myklebustcf470c32012-03-07 13:49:12 -05006530struct nfs_release_lockowner_data {
6531 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006532 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006533 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006534 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006535 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006536};
6537
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006538static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6539{
6540 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006541 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006542 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6543 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006544 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006545 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006546}
6547
6548static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6549{
6550 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006551 struct nfs_server *server = data->server;
6552
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006553 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006554
6555 switch (task->tk_status) {
6556 case 0:
6557 renew_lease(server, data->timestamp);
6558 break;
6559 case -NFS4ERR_STALE_CLIENTID:
6560 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006561 nfs4_schedule_lease_recovery(server->nfs_client);
6562 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006563 case -NFS4ERR_LEASE_MOVED:
6564 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006565 if (nfs4_async_handle_error(task, server,
6566 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006567 rpc_restart_call_prepare(task);
6568 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006569}
6570
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006571static void nfs4_release_lockowner_release(void *calldata)
6572{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006573 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006574 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006575 kfree(calldata);
6576}
6577
Trond Myklebust17280172012-03-11 13:11:00 -04006578static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006579 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6580 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006581 .rpc_release = nfs4_release_lockowner_release,
6582};
6583
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006584static void
6585nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006586{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006587 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006588 struct rpc_message msg = {
6589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6590 };
6591
6592 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006593 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006594
Trond Myklebustcf470c32012-03-07 13:49:12 -05006595 data = kmalloc(sizeof(*data), GFP_NOFS);
6596 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006597 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006598 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006599 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006600 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6601 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6602 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006603
Trond Myklebustcf470c32012-03-07 13:49:12 -05006604 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006605 msg.rpc_resp = &data->res;
6606 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006607 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006608}
6609
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006610#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6611
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006612static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006613 struct dentry *unused, struct inode *inode,
6614 const char *key, const void *buf,
6615 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006616{
Al Viro59301222016-05-27 10:19:30 -04006617 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006618}
6619
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006620static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006621 struct dentry *unused, struct inode *inode,
6622 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006623{
Al Virob2968212016-04-10 20:48:24 -04006624 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006625}
6626
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006627static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006628{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006629 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006630}
6631
David Quigleyc9bccef2013-05-22 12:50:45 -04006632#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006633
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006634static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006635 struct dentry *unused, struct inode *inode,
6636 const char *key, const void *buf,
6637 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006638{
6639 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006640 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006641
6642 return -EOPNOTSUPP;
6643}
6644
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006645static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006646 struct dentry *unused, struct inode *inode,
6647 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006648{
6649 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006650 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006651 return -EOPNOTSUPP;
6652}
6653
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006654static ssize_t
6655nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006656{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006657 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006658
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006659 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6660 len = security_inode_listsecurity(inode, list, list_len);
6661 if (list_len && len > list_len)
6662 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006663 }
6664 return len;
6665}
6666
6667static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6668 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006669 .get = nfs4_xattr_get_nfs4_label,
6670 .set = nfs4_xattr_set_nfs4_label,
6671};
David Quigleyc9bccef2013-05-22 12:50:45 -04006672
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006673#else
6674
6675static ssize_t
6676nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6677{
6678 return 0;
6679}
6680
6681#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006682
Andy Adamson533eb462011-06-13 18:25:56 -04006683/*
6684 * nfs_fhget will use either the mounted_on_fileid or the fileid
6685 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006686static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6687{
Andy Adamson533eb462011-06-13 18:25:56 -04006688 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6689 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6690 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006691 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006692 return;
6693
6694 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006695 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006696 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6697 fattr->nlink = 2;
6698}
6699
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006700static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6701 const struct qstr *name,
6702 struct nfs4_fs_locations *fs_locations,
6703 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006704{
6705 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006706 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006707 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006708 };
6709 struct nfs4_fs_locations_arg args = {
6710 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006711 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006712 .page = page,
6713 .bitmask = bitmask,
6714 };
Benny Halevy22958462009-04-01 09:22:02 -04006715 struct nfs4_fs_locations_res res = {
6716 .fs_locations = fs_locations,
6717 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006718 struct rpc_message msg = {
6719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6720 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006721 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006722 };
6723 int status;
6724
Harvey Harrison3110ff82008-05-02 13:42:44 -07006725 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006726
6727 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6728 * is not supported */
6729 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6730 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6731 else
6732 bitmask[0] |= FATTR4_WORD0_FILEID;
6733
Trond Myklebustc228fd32007-01-13 02:28:11 -05006734 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006735 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006736 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006737 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006738 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006739 return status;
6740}
6741
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006742int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6743 const struct qstr *name,
6744 struct nfs4_fs_locations *fs_locations,
6745 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006746{
6747 struct nfs4_exception exception = { };
6748 int err;
6749 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006750 err = _nfs4_proc_fs_locations(client, dir, name,
6751 fs_locations, page);
6752 trace_nfs4_get_fs_locations(dir, name, err);
6753 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006754 &exception);
6755 } while (exception.retry);
6756 return err;
6757}
6758
Chuck Leverb03d7352013-10-17 14:12:50 -04006759/*
6760 * This operation also signals the server that this client is
6761 * performing migration recovery. The server can stop returning
6762 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6763 * appended to this compound to identify the client ID which is
6764 * performing recovery.
6765 */
6766static int _nfs40_proc_get_locations(struct inode *inode,
6767 struct nfs4_fs_locations *locations,
6768 struct page *page, struct rpc_cred *cred)
6769{
6770 struct nfs_server *server = NFS_SERVER(inode);
6771 struct rpc_clnt *clnt = server->client;
6772 u32 bitmask[2] = {
6773 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6774 };
6775 struct nfs4_fs_locations_arg args = {
6776 .clientid = server->nfs_client->cl_clientid,
6777 .fh = NFS_FH(inode),
6778 .page = page,
6779 .bitmask = bitmask,
6780 .migration = 1, /* skip LOOKUP */
6781 .renew = 1, /* append RENEW */
6782 };
6783 struct nfs4_fs_locations_res res = {
6784 .fs_locations = locations,
6785 .migration = 1,
6786 .renew = 1,
6787 };
6788 struct rpc_message msg = {
6789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6790 .rpc_argp = &args,
6791 .rpc_resp = &res,
6792 .rpc_cred = cred,
6793 };
6794 unsigned long now = jiffies;
6795 int status;
6796
6797 nfs_fattr_init(&locations->fattr);
6798 locations->server = server;
6799 locations->nlocations = 0;
6800
6801 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6802 nfs4_set_sequence_privileged(&args.seq_args);
6803 status = nfs4_call_sync_sequence(clnt, server, &msg,
6804 &args.seq_args, &res.seq_res);
6805 if (status)
6806 return status;
6807
6808 renew_lease(server, now);
6809 return 0;
6810}
6811
6812#ifdef CONFIG_NFS_V4_1
6813
6814/*
6815 * This operation also signals the server that this client is
6816 * performing migration recovery. The server can stop asserting
6817 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6818 * performing this operation is identified in the SEQUENCE
6819 * operation in this compound.
6820 *
6821 * When the client supports GETATTR(fs_locations_info), it can
6822 * be plumbed in here.
6823 */
6824static int _nfs41_proc_get_locations(struct inode *inode,
6825 struct nfs4_fs_locations *locations,
6826 struct page *page, struct rpc_cred *cred)
6827{
6828 struct nfs_server *server = NFS_SERVER(inode);
6829 struct rpc_clnt *clnt = server->client;
6830 u32 bitmask[2] = {
6831 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6832 };
6833 struct nfs4_fs_locations_arg args = {
6834 .fh = NFS_FH(inode),
6835 .page = page,
6836 .bitmask = bitmask,
6837 .migration = 1, /* skip LOOKUP */
6838 };
6839 struct nfs4_fs_locations_res res = {
6840 .fs_locations = locations,
6841 .migration = 1,
6842 };
6843 struct rpc_message msg = {
6844 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6845 .rpc_argp = &args,
6846 .rpc_resp = &res,
6847 .rpc_cred = cred,
6848 };
6849 int status;
6850
6851 nfs_fattr_init(&locations->fattr);
6852 locations->server = server;
6853 locations->nlocations = 0;
6854
6855 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6856 nfs4_set_sequence_privileged(&args.seq_args);
6857 status = nfs4_call_sync_sequence(clnt, server, &msg,
6858 &args.seq_args, &res.seq_res);
6859 if (status == NFS4_OK &&
6860 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6861 status = -NFS4ERR_LEASE_MOVED;
6862 return status;
6863}
6864
6865#endif /* CONFIG_NFS_V4_1 */
6866
6867/**
6868 * nfs4_proc_get_locations - discover locations for a migrated FSID
6869 * @inode: inode on FSID that is migrating
6870 * @locations: result of query
6871 * @page: buffer
6872 * @cred: credential to use for this operation
6873 *
6874 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6875 * operation failed, or a negative errno if a local error occurred.
6876 *
6877 * On success, "locations" is filled in, but if the server has
6878 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6879 * asserted.
6880 *
6881 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6882 * from this client that require migration recovery.
6883 */
6884int nfs4_proc_get_locations(struct inode *inode,
6885 struct nfs4_fs_locations *locations,
6886 struct page *page, struct rpc_cred *cred)
6887{
6888 struct nfs_server *server = NFS_SERVER(inode);
6889 struct nfs_client *clp = server->nfs_client;
6890 const struct nfs4_mig_recovery_ops *ops =
6891 clp->cl_mvops->mig_recovery_ops;
6892 struct nfs4_exception exception = { };
6893 int status;
6894
6895 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6896 (unsigned long long)server->fsid.major,
6897 (unsigned long long)server->fsid.minor,
6898 clp->cl_hostname);
6899 nfs_display_fhandle(NFS_FH(inode), __func__);
6900
6901 do {
6902 status = ops->get_locations(inode, locations, page, cred);
6903 if (status != -NFS4ERR_DELAY)
6904 break;
6905 nfs4_handle_exception(server, status, &exception);
6906 } while (exception.retry);
6907 return status;
6908}
6909
Chuck Lever44c99932013-10-17 14:13:30 -04006910/*
6911 * This operation also signals the server that this client is
6912 * performing "lease moved" recovery. The server can stop
6913 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6914 * is appended to this compound to identify the client ID which is
6915 * performing recovery.
6916 */
6917static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6918{
6919 struct nfs_server *server = NFS_SERVER(inode);
6920 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6921 struct rpc_clnt *clnt = server->client;
6922 struct nfs4_fsid_present_arg args = {
6923 .fh = NFS_FH(inode),
6924 .clientid = clp->cl_clientid,
6925 .renew = 1, /* append RENEW */
6926 };
6927 struct nfs4_fsid_present_res res = {
6928 .renew = 1,
6929 };
6930 struct rpc_message msg = {
6931 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6932 .rpc_argp = &args,
6933 .rpc_resp = &res,
6934 .rpc_cred = cred,
6935 };
6936 unsigned long now = jiffies;
6937 int status;
6938
6939 res.fh = nfs_alloc_fhandle();
6940 if (res.fh == NULL)
6941 return -ENOMEM;
6942
6943 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6944 nfs4_set_sequence_privileged(&args.seq_args);
6945 status = nfs4_call_sync_sequence(clnt, server, &msg,
6946 &args.seq_args, &res.seq_res);
6947 nfs_free_fhandle(res.fh);
6948 if (status)
6949 return status;
6950
6951 do_renew_lease(clp, now);
6952 return 0;
6953}
6954
6955#ifdef CONFIG_NFS_V4_1
6956
6957/*
6958 * This operation also signals the server that this client is
6959 * performing "lease moved" recovery. The server can stop asserting
6960 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6961 * this operation is identified in the SEQUENCE operation in this
6962 * compound.
6963 */
6964static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6965{
6966 struct nfs_server *server = NFS_SERVER(inode);
6967 struct rpc_clnt *clnt = server->client;
6968 struct nfs4_fsid_present_arg args = {
6969 .fh = NFS_FH(inode),
6970 };
6971 struct nfs4_fsid_present_res res = {
6972 };
6973 struct rpc_message msg = {
6974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6975 .rpc_argp = &args,
6976 .rpc_resp = &res,
6977 .rpc_cred = cred,
6978 };
6979 int status;
6980
6981 res.fh = nfs_alloc_fhandle();
6982 if (res.fh == NULL)
6983 return -ENOMEM;
6984
6985 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6986 nfs4_set_sequence_privileged(&args.seq_args);
6987 status = nfs4_call_sync_sequence(clnt, server, &msg,
6988 &args.seq_args, &res.seq_res);
6989 nfs_free_fhandle(res.fh);
6990 if (status == NFS4_OK &&
6991 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6992 status = -NFS4ERR_LEASE_MOVED;
6993 return status;
6994}
6995
6996#endif /* CONFIG_NFS_V4_1 */
6997
6998/**
6999 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7000 * @inode: inode on FSID to check
7001 * @cred: credential to use for this operation
7002 *
7003 * Server indicates whether the FSID is present, moved, or not
7004 * recognized. This operation is necessary to clear a LEASE_MOVED
7005 * condition for this client ID.
7006 *
7007 * Returns NFS4_OK if the FSID is present on this server,
7008 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7009 * NFS4ERR code if some error occurred on the server, or a
7010 * negative errno if a local failure occurred.
7011 */
7012int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7013{
7014 struct nfs_server *server = NFS_SERVER(inode);
7015 struct nfs_client *clp = server->nfs_client;
7016 const struct nfs4_mig_recovery_ops *ops =
7017 clp->cl_mvops->mig_recovery_ops;
7018 struct nfs4_exception exception = { };
7019 int status;
7020
7021 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7022 (unsigned long long)server->fsid.major,
7023 (unsigned long long)server->fsid.minor,
7024 clp->cl_hostname);
7025 nfs_display_fhandle(NFS_FH(inode), __func__);
7026
7027 do {
7028 status = ops->fsid_present(inode, cred);
7029 if (status != -NFS4ERR_DELAY)
7030 break;
7031 nfs4_handle_exception(server, status, &exception);
7032 } while (exception.retry);
7033 return status;
7034}
7035
Andy Adamson5ec16a82013-08-08 10:57:55 -04007036/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007037 * If 'use_integrity' is true and the state managment nfs_client
7038 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7039 * and the machine credential as per RFC3530bis and RFC5661 Security
7040 * Considerations sections. Otherwise, just use the user cred with the
7041 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007042 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007043static 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 +00007044{
7045 int status;
7046 struct nfs4_secinfo_arg args = {
7047 .dir_fh = NFS_FH(dir),
7048 .name = name,
7049 };
7050 struct nfs4_secinfo_res res = {
7051 .flavors = flavors,
7052 };
7053 struct rpc_message msg = {
7054 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7055 .rpc_argp = &args,
7056 .rpc_resp = &res,
7057 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007058 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007059 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007060
7061 if (use_integrity) {
7062 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007063 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7064 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007065 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007066
7067 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007068
7069 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7070 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7071
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007072 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7073 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007074 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007075
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007076 if (cred)
7077 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007078
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007079 return status;
7080}
7081
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007082int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7083 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007084{
7085 struct nfs4_exception exception = { };
7086 int err;
7087 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007088 err = -NFS4ERR_WRONGSEC;
7089
7090 /* try to use integrity protection with machine cred */
7091 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7092 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7093
7094 /*
7095 * if unable to use integrity protection, or SECINFO with
7096 * integrity protection returns NFS4ERR_WRONGSEC (which is
7097 * disallowed by spec, but exists in deployed servers) use
7098 * the current filesystem's rpc_client and the user cred.
7099 */
7100 if (err == -NFS4ERR_WRONGSEC)
7101 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7102
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007103 trace_nfs4_secinfo(dir, name, err);
7104 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007105 &exception);
7106 } while (exception.retry);
7107 return err;
7108}
7109
Andy Adamson557134a2009-04-01 09:21:53 -04007110#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007111/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007112 * Check the exchange flags returned by the server for invalid flags, having
7113 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7114 * DS flags set.
7115 */
7116static int nfs4_check_cl_exchange_flags(u32 flags)
7117{
7118 if (flags & ~EXCHGID4_FLAG_MASK_R)
7119 goto out_inval;
7120 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7121 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7122 goto out_inval;
7123 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7124 goto out_inval;
7125 return NFS_OK;
7126out_inval:
7127 return -NFS4ERR_INVAL;
7128}
7129
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007130static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007131nfs41_same_server_scope(struct nfs41_server_scope *a,
7132 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007133{
7134 if (a->server_scope_sz == b->server_scope_sz &&
7135 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7136 return true;
7137
7138 return false;
7139}
7140
Andy Adamson02a95de2016-02-05 16:08:37 -05007141static void
7142nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7143{
7144}
7145
7146static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7147 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7148};
7149
Andy Adamson357f54d2010-12-14 10:11:57 -05007150/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007151 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007152 *
7153 * The 4.1 client currently uses the same TCP connection for the
7154 * fore and backchannel.
7155 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007156static
7157int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7158 struct rpc_xprt *xprt,
7159 struct nfs_client *clp,
7160 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007161{
7162 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007163 struct nfs41_bind_conn_to_session_args args = {
7164 .client = clp,
7165 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7166 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007167 struct nfs41_bind_conn_to_session_res res;
7168 struct rpc_message msg = {
7169 .rpc_proc =
7170 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007171 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007172 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007173 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007174 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007175 struct rpc_task_setup task_setup_data = {
7176 .rpc_client = clnt,
7177 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007178 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007179 .rpc_message = &msg,
7180 .flags = RPC_TASK_TIMEOUT,
7181 };
7182 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007183
7184 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007185
Trond Myklebust71a097c2015-02-18 09:27:18 -08007186 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7187 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7188 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007189
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007190 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7191 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7192 args.dir = NFS4_CDFC4_FORE;
7193
7194 task = rpc_run_task(&task_setup_data);
7195 if (!IS_ERR(task)) {
7196 status = task->tk_status;
7197 rpc_put_task(task);
7198 } else
7199 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007200 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007201 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007202 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007203 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7204 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7205 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007206 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007207 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007208 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007209 dprintk("NFS: %s: Unexpected direction from server\n",
7210 __func__);
7211 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007212 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007213 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007214 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007215 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7216 __func__);
7217 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007218 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007219 }
7220 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007221out:
7222 dprintk("<-- %s status= %d\n", __func__, status);
7223 return status;
7224}
7225
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007226struct rpc_bind_conn_calldata {
7227 struct nfs_client *clp;
7228 struct rpc_cred *cred;
7229};
7230
7231static int
7232nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7233 struct rpc_xprt *xprt,
7234 void *calldata)
7235{
7236 struct rpc_bind_conn_calldata *p = calldata;
7237
7238 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7239}
7240
7241int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7242{
7243 struct rpc_bind_conn_calldata data = {
7244 .clp = clp,
7245 .cred = cred,
7246 };
7247 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7248 nfs4_proc_bind_conn_to_session_callback, &data);
7249}
7250
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007251/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007252 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7253 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007254 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007255static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7256 .how = SP4_MACH_CRED,
7257 .enforce.u.words = {
7258 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7259 1 << (OP_EXCHANGE_ID - 32) |
7260 1 << (OP_CREATE_SESSION - 32) |
7261 1 << (OP_DESTROY_SESSION - 32) |
7262 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007263 },
7264 .allow.u.words = {
7265 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007266 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007267 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007268 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007269 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007270 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007271 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007272 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007273 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007274 1 << (OP_FREE_STATEID - 32) |
7275 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007276 }
7277};
7278
7279/*
7280 * Select the state protection mode for client `clp' given the server results
7281 * from exchange_id in `sp'.
7282 *
7283 * Returns 0 on success, negative errno otherwise.
7284 */
7285static int nfs4_sp4_select_mode(struct nfs_client *clp,
7286 struct nfs41_state_protection *sp)
7287{
7288 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7289 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7290 1 << (OP_EXCHANGE_ID - 32) |
7291 1 << (OP_CREATE_SESSION - 32) |
7292 1 << (OP_DESTROY_SESSION - 32) |
7293 1 << (OP_DESTROY_CLIENTID - 32)
7294 };
7295 unsigned int i;
7296
7297 if (sp->how == SP4_MACH_CRED) {
7298 /* Print state protect result */
7299 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7300 for (i = 0; i <= LAST_NFS4_OP; i++) {
7301 if (test_bit(i, sp->enforce.u.longs))
7302 dfprintk(MOUNT, " enforce op %d\n", i);
7303 if (test_bit(i, sp->allow.u.longs))
7304 dfprintk(MOUNT, " allow op %d\n", i);
7305 }
7306
7307 /* make sure nothing is on enforce list that isn't supported */
7308 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7309 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7310 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7311 return -EINVAL;
7312 }
7313 }
7314
7315 /*
7316 * Minimal mode - state operations are allowed to use machine
7317 * credential. Note this already happens by default, so the
7318 * client doesn't have to do anything more than the negotiation.
7319 *
7320 * NOTE: we don't care if EXCHANGE_ID is in the list -
7321 * we're already using the machine cred for exchange_id
7322 * and will never use a different cred.
7323 */
7324 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7325 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7326 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7327 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7328 dfprintk(MOUNT, "sp4_mach_cred:\n");
7329 dfprintk(MOUNT, " minimal mode enabled\n");
7330 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7331 } else {
7332 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7333 return -EINVAL;
7334 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007335
7336 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007337 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7338 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007339 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7340 dfprintk(MOUNT, " cleanup mode enabled\n");
7341 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7342 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007343
Andrew Elble99ade3c2015-12-02 09:39:51 -05007344 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7345 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7346 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7347 &clp->cl_sp4_flags);
7348 }
7349
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007350 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7351 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7352 dfprintk(MOUNT, " secinfo mode enabled\n");
7353 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7354 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007355
7356 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7357 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7358 dfprintk(MOUNT, " stateid mode enabled\n");
7359 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7360 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007361
7362 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7363 dfprintk(MOUNT, " write mode enabled\n");
7364 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7365 }
7366
7367 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7368 dfprintk(MOUNT, " commit mode enabled\n");
7369 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7370 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007371 }
7372
7373 return 0;
7374}
7375
Andy Adamson8d89bd72016-09-09 09:22:18 -04007376struct nfs41_exchange_id_data {
7377 struct nfs41_exchange_id_res res;
7378 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007379 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007380 int rpc_status;
7381};
7382
7383static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007384{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007385 struct nfs41_exchange_id_data *cdata =
7386 (struct nfs41_exchange_id_data *)data;
7387 struct nfs_client *clp = cdata->args.client;
7388 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007389
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007390 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007391
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007392 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007393 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007394
7395 if (cdata->xprt && status == 0) {
7396 status = nfs4_detect_session_trunking(clp, &cdata->res,
7397 cdata->xprt);
7398 goto out;
7399 }
7400
Andy Adamson8d89bd72016-09-09 09:22:18 -04007401 if (status == 0)
7402 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007403
Chuck Lever177313f2012-05-21 22:44:58 -04007404 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007405 clp->cl_clientid = cdata->res.clientid;
7406 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007407 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007408 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007409 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007410 &clp->cl_session->session_state);
7411 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007412 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007413
Chuck Leveracdeb692012-05-21 22:46:16 -04007414 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007415 clp->cl_serverowner = cdata->res.server_owner;
7416 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007417
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007418 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007419 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007420 clp->cl_implid = cdata->res.impl_id;
7421 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007422
Chuck Lever177313f2012-05-21 22:44:58 -04007423 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007424 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007425 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007426 dprintk("%s: server_scope mismatch detected\n",
7427 __func__);
7428 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007429 kfree(clp->cl_serverscope);
7430 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007431 }
7432
Chuck Lever177313f2012-05-21 22:44:58 -04007433 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007434 clp->cl_serverscope = cdata->res.server_scope;
7435 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007436 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007437 /* Save the EXCHANGE_ID verifier session trunk tests */
7438 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7439 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007440 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007441out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007442 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007443 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007444}
7445
7446static void nfs4_exchange_id_release(void *data)
7447{
7448 struct nfs41_exchange_id_data *cdata =
7449 (struct nfs41_exchange_id_data *)data;
7450
7451 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007452 if (cdata->xprt) {
7453 xprt_put(cdata->xprt);
7454 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7455 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007456 kfree(cdata->res.impl_id);
7457 kfree(cdata->res.server_scope);
7458 kfree(cdata->res.server_owner);
7459 kfree(cdata);
7460}
7461
7462static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7463 .rpc_call_done = nfs4_exchange_id_done,
7464 .rpc_release = nfs4_exchange_id_release,
7465};
7466
Benny Halevy99fe60d2009-04-01 09:22:29 -04007467/*
7468 * _nfs4_proc_exchange_id()
7469 *
7470 * Wrapper for EXCHANGE_ID operation.
7471 */
7472static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007473 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007474{
7475 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007476 struct rpc_message msg = {
7477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007478 .rpc_cred = cred,
7479 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007480 struct rpc_task_setup task_setup_data = {
7481 .rpc_client = clp->cl_rpcclient,
7482 .callback_ops = &nfs4_exchange_id_call_ops,
7483 .rpc_message = &msg,
7484 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7485 };
7486 struct nfs41_exchange_id_data *calldata;
7487 struct rpc_task *task;
7488 int status = -EIO;
7489
7490 if (!atomic_inc_not_zero(&clp->cl_count))
7491 goto out;
7492
7493 status = -ENOMEM;
7494 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7495 if (!calldata)
7496 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007497
Andy Adamsonad0849a2016-09-09 09:22:28 -04007498 if (!xprt)
7499 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007500
7501 status = nfs4_init_uniform_client_string(clp);
7502 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007503 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007504
7505 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7506 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7507 clp->cl_owner_id);
7508
Andy Adamson8d89bd72016-09-09 09:22:18 -04007509 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7510 GFP_NOFS);
7511 status = -ENOMEM;
7512 if (unlikely(calldata->res.server_owner == NULL))
7513 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007514
Andy Adamson8d89bd72016-09-09 09:22:18 -04007515 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007516 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007517 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007518 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007519
Andy Adamson8d89bd72016-09-09 09:22:18 -04007520 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7521 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007522 goto out_server_scope;
7523
7524 switch (sp4_how) {
7525 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007526 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007527 break;
7528
7529 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007530 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007531 break;
7532
7533 default:
7534 /* unsupported! */
7535 WARN_ON_ONCE(1);
7536 status = -EINVAL;
7537 goto out_impl_id;
7538 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007539 if (xprt) {
7540 calldata->xprt = xprt;
7541 task_setup_data.rpc_xprt = xprt;
7542 task_setup_data.flags =
7543 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7544 calldata->args.verifier = &clp->cl_confirm;
7545 } else {
7546 calldata->args.verifier = &verifier;
7547 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007548 calldata->args.client = clp;
7549#ifdef CONFIG_NFS_V4_1_MIGRATION
7550 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7551 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7552 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7553#else
7554 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7555 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7556#endif
7557 msg.rpc_argp = &calldata->args;
7558 msg.rpc_resp = &calldata->res;
7559 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007560
Andy Adamson8d89bd72016-09-09 09:22:18 -04007561 task = rpc_run_task(&task_setup_data);
7562 if (IS_ERR(task)) {
7563 status = PTR_ERR(task);
7564 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007565 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007566
Andy Adamsonad0849a2016-09-09 09:22:28 -04007567 if (!xprt) {
7568 status = rpc_wait_for_completion_task(task);
7569 if (!status)
7570 status = calldata->rpc_status;
7571 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007572 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007573
Andy Adamson8d89bd72016-09-09 09:22:18 -04007574 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007575out:
Chuck Lever177313f2012-05-21 22:44:58 -04007576 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007577 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007578 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007579 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007580 clp->cl_implid->date.seconds,
7581 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007582 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007583 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007584
7585out_impl_id:
7586 kfree(calldata->res.impl_id);
7587out_server_scope:
7588 kfree(calldata->res.server_scope);
7589out_server_owner:
7590 kfree(calldata->res.server_owner);
7591out_calldata:
7592 kfree(calldata);
7593 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007594}
7595
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007596/*
7597 * nfs4_proc_exchange_id()
7598 *
7599 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7600 *
7601 * Since the clientid has expired, all compounds using sessions
7602 * associated with the stale clientid will be returning
7603 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7604 * be in some phase of session reset.
7605 *
7606 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7607 */
7608int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7609{
7610 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7611 int status;
7612
7613 /* try SP4_MACH_CRED if krb5i/p */
7614 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7615 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007616 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007617 if (!status)
7618 return 0;
7619 }
7620
7621 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007622 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007623}
7624
Andy Adamson04fa2c62016-09-09 09:22:29 -04007625/**
7626 * nfs4_test_session_trunk
7627 *
7628 * This is an add_xprt_test() test function called from
7629 * rpc_clnt_setup_test_and_add_xprt.
7630 *
7631 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7632 * and is dereferrenced in nfs4_exchange_id_release
7633 *
7634 * Upon success, add the new transport to the rpc_clnt
7635 *
7636 * @clnt: struct rpc_clnt to get new transport
7637 * @xprt: the rpc_xprt to test
7638 * @data: call data for _nfs4_proc_exchange_id.
7639 */
7640int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7641 void *data)
7642{
7643 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7644 u32 sp4_how;
7645
7646 dprintk("--> %s try %s\n", __func__,
7647 xprt->address_strings[RPC_DISPLAY_ADDR]);
7648
7649 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7650
7651 /* Test connection for session trunking. Async exchange_id call */
7652 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7653}
7654EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7655
Trond Myklebust66245532012-05-25 17:18:09 -04007656static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7657 struct rpc_cred *cred)
7658{
7659 struct rpc_message msg = {
7660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7661 .rpc_argp = clp,
7662 .rpc_cred = cred,
7663 };
7664 int status;
7665
7666 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007667 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007668 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007669 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007670 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7671 return status;
7672}
7673
7674static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7675 struct rpc_cred *cred)
7676{
7677 unsigned int loop;
7678 int ret;
7679
7680 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7681 ret = _nfs4_proc_destroy_clientid(clp, cred);
7682 switch (ret) {
7683 case -NFS4ERR_DELAY:
7684 case -NFS4ERR_CLIENTID_BUSY:
7685 ssleep(1);
7686 break;
7687 default:
7688 return ret;
7689 }
7690 }
7691 return 0;
7692}
7693
7694int nfs4_destroy_clientid(struct nfs_client *clp)
7695{
7696 struct rpc_cred *cred;
7697 int ret = 0;
7698
7699 if (clp->cl_mvops->minor_version < 1)
7700 goto out;
7701 if (clp->cl_exchange_flags == 0)
7702 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007703 if (clp->cl_preserve_clid)
7704 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007705 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007706 ret = nfs4_proc_destroy_clientid(clp, cred);
7707 if (cred)
7708 put_rpccred(cred);
7709 switch (ret) {
7710 case 0:
7711 case -NFS4ERR_STALE_CLIENTID:
7712 clp->cl_exchange_flags = 0;
7713 }
7714out:
7715 return ret;
7716}
7717
Andy Adamson2050f0c2009-04-01 09:22:30 -04007718struct nfs4_get_lease_time_data {
7719 struct nfs4_get_lease_time_args *args;
7720 struct nfs4_get_lease_time_res *res;
7721 struct nfs_client *clp;
7722};
7723
7724static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7725 void *calldata)
7726{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007727 struct nfs4_get_lease_time_data *data =
7728 (struct nfs4_get_lease_time_data *)calldata;
7729
7730 dprintk("--> %s\n", __func__);
7731 /* just setup sequence, do not trigger session recovery
7732 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007733 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007734 &data->args->la_seq_args,
7735 &data->res->lr_seq_res,
7736 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007737 dprintk("<-- %s\n", __func__);
7738}
7739
7740/*
7741 * Called from nfs4_state_manager thread for session setup, so don't recover
7742 * from sequence operation or clientid errors.
7743 */
7744static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7745{
7746 struct nfs4_get_lease_time_data *data =
7747 (struct nfs4_get_lease_time_data *)calldata;
7748
7749 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007750 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7751 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007752 switch (task->tk_status) {
7753 case -NFS4ERR_DELAY:
7754 case -NFS4ERR_GRACE:
7755 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7756 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7757 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007758 /* fall through */
7759 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007760 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007761 return;
7762 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007763 dprintk("<-- %s\n", __func__);
7764}
7765
Trond Myklebust17280172012-03-11 13:11:00 -04007766static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007767 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7768 .rpc_call_done = nfs4_get_lease_time_done,
7769};
7770
7771int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7772{
7773 struct rpc_task *task;
7774 struct nfs4_get_lease_time_args args;
7775 struct nfs4_get_lease_time_res res = {
7776 .lr_fsinfo = fsinfo,
7777 };
7778 struct nfs4_get_lease_time_data data = {
7779 .args = &args,
7780 .res = &res,
7781 .clp = clp,
7782 };
7783 struct rpc_message msg = {
7784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7785 .rpc_argp = &args,
7786 .rpc_resp = &res,
7787 };
7788 struct rpc_task_setup task_setup = {
7789 .rpc_client = clp->cl_rpcclient,
7790 .rpc_message = &msg,
7791 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007792 .callback_data = &data,
7793 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007794 };
7795 int status;
7796
Chuck Levera9c92d62013-08-09 12:48:18 -04007797 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007798 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007799 dprintk("--> %s\n", __func__);
7800 task = rpc_run_task(&task_setup);
7801
7802 if (IS_ERR(task))
7803 status = PTR_ERR(task);
7804 else {
7805 status = task->tk_status;
7806 rpc_put_task(task);
7807 }
7808 dprintk("<-- %s return %d\n", __func__, status);
7809
7810 return status;
7811}
7812
Andy Adamsonfc931582009-04-01 09:22:31 -04007813/*
7814 * Initialize the values to be used by the client in CREATE_SESSION
7815 * If nfs4_init_session set the fore channel request and response sizes,
7816 * use them.
7817 *
7818 * Set the back channel max_resp_sz_cached to zero to force the client to
7819 * always set csa_cachethis to FALSE because the current implementation
7820 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7821 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007822static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7823 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007824{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007825 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007826 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007827
Andy Adamson18aad3d2013-06-26 12:21:49 -04007828 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7829 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7830
Andy Adamsonfc931582009-04-01 09:22:31 -04007831 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007832 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7833 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007834 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007835 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007836
7837 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007838 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007839 __func__,
7840 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007841 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007842
7843 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007844 args->bc_attrs.max_rqst_sz = max_bc_payload;
7845 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007846 args->bc_attrs.max_resp_sz_cached = 0;
7847 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007848 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007849
7850 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7851 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7852 __func__,
7853 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7854 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7855 args->bc_attrs.max_reqs);
7856}
7857
Trond Myklebust79969dd2015-02-18 11:30:18 -08007858static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7859 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007860{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007861 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007862 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007863
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007864 if (rcvd->max_resp_sz > sent->max_resp_sz)
7865 return -EINVAL;
7866 /*
7867 * Our requested max_ops is the minimum we need; we're not
7868 * prepared to break up compounds into smaller pieces than that.
7869 * So, no point even trying to continue if the server won't
7870 * cooperate:
7871 */
7872 if (rcvd->max_ops < sent->max_ops)
7873 return -EINVAL;
7874 if (rcvd->max_reqs == 0)
7875 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007876 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7877 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007878 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007879}
7880
Trond Myklebust79969dd2015-02-18 11:30:18 -08007881static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7882 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007883{
7884 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007885 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007886
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007887 if (!(res->flags & SESSION4_BACK_CHAN))
7888 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007889 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7890 return -EINVAL;
7891 if (rcvd->max_resp_sz < sent->max_resp_sz)
7892 return -EINVAL;
7893 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7894 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007895 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007896 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007897 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007898 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007899out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007900 return 0;
7901}
Andy Adamson8d353012009-04-01 09:22:32 -04007902
Andy Adamson8d353012009-04-01 09:22:32 -04007903static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007904 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007905{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007906 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007907
Trond Myklebust79969dd2015-02-18 11:30:18 -08007908 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007909 if (ret)
7910 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007911 return nfs4_verify_back_channel_attrs(args, res);
7912}
7913
7914static void nfs4_update_session(struct nfs4_session *session,
7915 struct nfs41_create_session_res *res)
7916{
7917 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007918 /* Mark client id and session as being confirmed */
7919 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7920 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007921 session->flags = res->flags;
7922 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007923 if (res->flags & SESSION4_BACK_CHAN)
7924 memcpy(&session->bc_attrs, &res->bc_attrs,
7925 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007926}
7927
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007928static int _nfs4_proc_create_session(struct nfs_client *clp,
7929 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007930{
7931 struct nfs4_session *session = clp->cl_session;
7932 struct nfs41_create_session_args args = {
7933 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007934 .clientid = clp->cl_clientid,
7935 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007936 .cb_program = NFS4_CALLBACK,
7937 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007938 struct nfs41_create_session_res res;
7939
Andy Adamsonfc931582009-04-01 09:22:31 -04007940 struct rpc_message msg = {
7941 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7942 .rpc_argp = &args,
7943 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007944 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007945 };
7946 int status;
7947
Chuck Lever6b26cc82016-05-02 14:40:40 -04007948 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007949 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007950
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007951 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007952 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007953
Trond Myklebustb519d402016-09-11 14:50:01 -04007954 switch (status) {
7955 case -NFS4ERR_STALE_CLIENTID:
7956 case -NFS4ERR_DELAY:
7957 case -ETIMEDOUT:
7958 case -EACCES:
7959 case -EAGAIN:
7960 goto out;
7961 };
7962
7963 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007964 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007965 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007966 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007967 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007968 if (status)
7969 goto out;
7970 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007971 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007972out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007973 return status;
7974}
7975
7976/*
7977 * Issues a CREATE_SESSION operation to the server.
7978 * It is the responsibility of the caller to verify the session is
7979 * expired before calling this routine.
7980 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007981int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007982{
7983 int status;
7984 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007985 struct nfs4_session *session = clp->cl_session;
7986
7987 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7988
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007989 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007990 if (status)
7991 goto out;
7992
Andy Adamsonaacd5532011-11-09 13:58:21 -05007993 /* Init or reset the session slot tables */
7994 status = nfs4_setup_session_slot_tables(session);
7995 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007996 if (status)
7997 goto out;
7998
7999 ptr = (unsigned *)&session->sess_id.data[0];
8000 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8001 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008002out:
8003 dprintk("<-- %s\n", __func__);
8004 return status;
8005}
8006
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008007/*
8008 * Issue the over-the-wire RPC DESTROY_SESSION.
8009 * The caller must serialize access to this routine.
8010 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008011int nfs4_proc_destroy_session(struct nfs4_session *session,
8012 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008013{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008014 struct rpc_message msg = {
8015 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8016 .rpc_argp = session,
8017 .rpc_cred = cred,
8018 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008019 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008020
8021 dprintk("--> nfs4_proc_destroy_session\n");
8022
8023 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008024 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8025 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008026
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008027 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008028 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008029
8030 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008031 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008032 "Session has been destroyed regardless...\n", status);
8033
8034 dprintk("<-- nfs4_proc_destroy_session\n");
8035 return status;
8036}
8037
Trond Myklebust7b38c362012-05-23 13:23:31 -04008038/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008039 * Renew the cl_session lease.
8040 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008041struct nfs4_sequence_data {
8042 struct nfs_client *clp;
8043 struct nfs4_sequence_args args;
8044 struct nfs4_sequence_res res;
8045};
8046
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008047static void nfs41_sequence_release(void *data)
8048{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008049 struct nfs4_sequence_data *calldata = data;
8050 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008051
Alexandros Batsakis71358402010-02-05 03:45:05 -08008052 if (atomic_read(&clp->cl_count) > 1)
8053 nfs4_schedule_state_renewal(clp);
8054 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008055 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008056}
8057
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008058static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8059{
8060 switch(task->tk_status) {
8061 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008062 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8063 return -EAGAIN;
8064 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008065 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008066 }
8067 return 0;
8068}
8069
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008070static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008071{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008072 struct nfs4_sequence_data *calldata = data;
8073 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008074
Trond Myklebust14516c32010-07-31 14:29:06 -04008075 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8076 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008077
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008078 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008079 if (task->tk_status < 0) {
8080 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008081 if (atomic_read(&clp->cl_count) == 1)
8082 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008083
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008084 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8085 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008086 return;
8087 }
8088 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008089 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008090out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008091 dprintk("<-- %s\n", __func__);
8092}
8093
8094static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8095{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008096 struct nfs4_sequence_data *calldata = data;
8097 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008098 struct nfs4_sequence_args *args;
8099 struct nfs4_sequence_res *res;
8100
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008101 args = task->tk_msg.rpc_argp;
8102 res = task->tk_msg.rpc_resp;
8103
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008104 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008105}
8106
8107static const struct rpc_call_ops nfs41_sequence_ops = {
8108 .rpc_call_done = nfs41_sequence_call_done,
8109 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008110 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008111};
8112
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008113static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8114 struct rpc_cred *cred,
8115 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008116{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008117 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008118 struct rpc_message msg = {
8119 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8120 .rpc_cred = cred,
8121 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008122 struct rpc_task_setup task_setup_data = {
8123 .rpc_client = clp->cl_rpcclient,
8124 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008125 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008126 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008127 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008128
Alexandros Batsakis71358402010-02-05 03:45:05 -08008129 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008130 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008131 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008132 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008133 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008134 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008135 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008136 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008137 if (is_privileged)
8138 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008139 msg.rpc_argp = &calldata->args;
8140 msg.rpc_resp = &calldata->res;
8141 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008142 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008143
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008144 return rpc_run_task(&task_setup_data);
8145}
8146
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008147static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008148{
8149 struct rpc_task *task;
8150 int ret = 0;
8151
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008152 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008153 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008154 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008155 if (IS_ERR(task))
8156 ret = PTR_ERR(task);
8157 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008158 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008159 dprintk("<-- %s status=%d\n", __func__, ret);
8160 return ret;
8161}
8162
8163static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8164{
8165 struct rpc_task *task;
8166 int ret;
8167
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008168 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008169 if (IS_ERR(task)) {
8170 ret = PTR_ERR(task);
8171 goto out;
8172 }
8173 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008174 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008175 ret = task->tk_status;
8176 rpc_put_task(task);
8177out:
8178 dprintk("<-- %s status=%d\n", __func__, ret);
8179 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008180}
8181
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008182struct nfs4_reclaim_complete_data {
8183 struct nfs_client *clp;
8184 struct nfs41_reclaim_complete_args arg;
8185 struct nfs41_reclaim_complete_res res;
8186};
8187
8188static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8189{
8190 struct nfs4_reclaim_complete_data *calldata = data;
8191
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008192 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008193 &calldata->arg.seq_args,
8194 &calldata->res.seq_res,
8195 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008196}
8197
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008198static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8199{
8200 switch(task->tk_status) {
8201 case 0:
8202 case -NFS4ERR_COMPLETE_ALREADY:
8203 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8204 break;
8205 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008206 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008207 /* fall through */
8208 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008209 return -EAGAIN;
8210 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008211 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008212 }
8213 return 0;
8214}
8215
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008216static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8217{
8218 struct nfs4_reclaim_complete_data *calldata = data;
8219 struct nfs_client *clp = calldata->clp;
8220 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8221
8222 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008223 if (!nfs41_sequence_done(task, res))
8224 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008225
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008226 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008227 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8228 rpc_restart_call_prepare(task);
8229 return;
8230 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008231 dprintk("<-- %s\n", __func__);
8232}
8233
8234static void nfs4_free_reclaim_complete_data(void *data)
8235{
8236 struct nfs4_reclaim_complete_data *calldata = data;
8237
8238 kfree(calldata);
8239}
8240
8241static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8242 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8243 .rpc_call_done = nfs4_reclaim_complete_done,
8244 .rpc_release = nfs4_free_reclaim_complete_data,
8245};
8246
8247/*
8248 * Issue a global reclaim complete.
8249 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008250static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8251 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008252{
8253 struct nfs4_reclaim_complete_data *calldata;
8254 struct rpc_task *task;
8255 struct rpc_message msg = {
8256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008257 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008258 };
8259 struct rpc_task_setup task_setup_data = {
8260 .rpc_client = clp->cl_rpcclient,
8261 .rpc_message = &msg,
8262 .callback_ops = &nfs4_reclaim_complete_call_ops,
8263 .flags = RPC_TASK_ASYNC,
8264 };
8265 int status = -ENOMEM;
8266
8267 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008268 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008269 if (calldata == NULL)
8270 goto out;
8271 calldata->clp = clp;
8272 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008273
Chuck Levera9c92d62013-08-09 12:48:18 -04008274 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008275 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008276 msg.rpc_argp = &calldata->arg;
8277 msg.rpc_resp = &calldata->res;
8278 task_setup_data.callback_data = calldata;
8279 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008280 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008281 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008282 goto out;
8283 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008284 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008285 if (status == 0)
8286 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008287 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008288 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008289out:
8290 dprintk("<-- %s status=%d\n", __func__, status);
8291 return status;
8292}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008293
8294static void
8295nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8296{
8297 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008298 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008299
8300 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008301 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008302 &lgp->res.seq_res, task);
8303 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008304}
8305
8306static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8307{
8308 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008309
8310 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008311 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008312 dprintk("<-- %s\n", __func__);
8313}
8314
8315static int
8316nfs4_layoutget_handle_exception(struct rpc_task *task,
8317 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8318{
Trond Myklebustee314c22012-10-01 17:25:48 -07008319 struct inode *inode = lgp->args.inode;
8320 struct nfs_server *server = NFS_SERVER(inode);
8321 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008322 int nfs4err = task->tk_status;
8323 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008324 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008325
Boaz Harroshed7e5422014-01-22 20:34:54 +02008326 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008327
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008328 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008329 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008330 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008331
8332 /*
8333 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8334 * on the file. set tk_status to -ENODATA to tell upper layer to
8335 * retry go inband.
8336 */
8337 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008338 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008339 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008340 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008341 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8342 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8343 */
8344 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008345 status = -EOVERFLOW;
8346 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008347 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008348 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008349 * (or clients) writing to the same RAID stripe except when
8350 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008351 *
8352 * Treat it like we would RECALLCONFLICT -- we retry for a little
8353 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008354 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008355 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008356 if (lgp->args.minlength == 0) {
8357 status = -EOVERFLOW;
8358 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008359 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008360 status = -EBUSY;
8361 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008362 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008363 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008364 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008365 case -NFS4ERR_DELEG_REVOKED:
8366 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008367 case -NFS4ERR_EXPIRED:
8368 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008369 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008370 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008371 lo = NFS_I(inode)->layout;
8372 /* If the open stateid was bad, then recover it. */
8373 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8374 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008375 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008376 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008377 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008378 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008379 break;
8380 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008381
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008382 /*
8383 * Mark the bad layout state as invalid, then retry
8384 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008385 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008386 spin_unlock(&inode->i_lock);
8387 pnfs_free_lseg_list(&head);
8388 status = -EAGAIN;
8389 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008390 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008391
Trond Myklebust8ac09252017-01-23 22:44:12 -05008392 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008393 err = nfs4_handle_exception(server, nfs4err, exception);
8394 if (!status) {
8395 if (exception->retry)
8396 status = -EAGAIN;
8397 else
8398 status = err;
8399 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008400out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008401 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008402 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008403}
8404
Idan Kedar85541162012-08-02 11:47:10 +03008405static size_t max_response_pages(struct nfs_server *server)
8406{
8407 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8408 return nfs_page_array_len(0, max_resp_sz);
8409}
8410
8411static void nfs4_free_pages(struct page **pages, size_t size)
8412{
8413 int i;
8414
8415 if (!pages)
8416 return;
8417
8418 for (i = 0; i < size; i++) {
8419 if (!pages[i])
8420 break;
8421 __free_page(pages[i]);
8422 }
8423 kfree(pages);
8424}
8425
8426static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8427{
8428 struct page **pages;
8429 int i;
8430
8431 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8432 if (!pages) {
8433 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8434 return NULL;
8435 }
8436
8437 for (i = 0; i < size; i++) {
8438 pages[i] = alloc_page(gfp_flags);
8439 if (!pages[i]) {
8440 dprintk("%s: failed to allocate page\n", __func__);
8441 nfs4_free_pages(pages, size);
8442 return NULL;
8443 }
8444 }
8445
8446 return pages;
8447}
8448
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008449static void nfs4_layoutget_release(void *calldata)
8450{
8451 struct nfs4_layoutget *lgp = calldata;
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
8456 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008457 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008458 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008459 put_nfs_open_context(lgp->args.ctx);
8460 kfree(calldata);
8461 dprintk("<-- %s\n", __func__);
8462}
8463
8464static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8465 .rpc_call_prepare = nfs4_layoutget_prepare,
8466 .rpc_call_done = nfs4_layoutget_done,
8467 .rpc_release = nfs4_layoutget_release,
8468};
8469
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008470struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008471nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008472{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008473 struct inode *inode = lgp->args.inode;
8474 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008475 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008476 struct rpc_task *task;
8477 struct rpc_message msg = {
8478 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8479 .rpc_argp = &lgp->args,
8480 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008481 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008482 };
8483 struct rpc_task_setup task_setup_data = {
8484 .rpc_client = server->client,
8485 .rpc_message = &msg,
8486 .callback_ops = &nfs4_layoutget_call_ops,
8487 .callback_data = lgp,
8488 .flags = RPC_TASK_ASYNC,
8489 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008490 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008491 struct nfs4_exception exception = {
8492 .inode = inode,
8493 .timeout = *timeout,
8494 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008495 int status = 0;
8496
8497 dprintk("--> %s\n", __func__);
8498
Peng Tao4bd5a982014-11-17 11:05:17 +08008499 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8500 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8501
Idan Kedar85541162012-08-02 11:47:10 +03008502 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8503 if (!lgp->args.layout.pages) {
8504 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008505 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008506 }
8507 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8508
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008509 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008510 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008511 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008512
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008513 task = rpc_run_task(&task_setup_data);
8514 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008515 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008516 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008517 if (status == 0) {
8518 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8519 *timeout = exception.timeout;
8520 }
8521
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008522 trace_nfs4_layoutget(lgp->args.ctx,
8523 &lgp->args.range,
8524 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008525 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008526 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008527
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008528 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8529 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008530 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008531 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008532 rpc_put_task(task);
8533 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008534 if (status)
8535 return ERR_PTR(status);
8536 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008537}
8538
Benny Halevycbe82602011-05-22 19:52:37 +03008539static void
8540nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8541{
8542 struct nfs4_layoutreturn *lrp = calldata;
8543
8544 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008545 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008546 &lrp->args.seq_args,
8547 &lrp->res.seq_res,
8548 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008549}
8550
8551static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8552{
8553 struct nfs4_layoutreturn *lrp = calldata;
8554 struct nfs_server *server;
8555
8556 dprintk("--> %s\n", __func__);
8557
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008558 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008559 return;
8560
8561 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008562 switch (task->tk_status) {
8563 default:
8564 task->tk_status = 0;
8565 case 0:
8566 break;
8567 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008568 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008569 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008570 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008571 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008572 return;
8573 }
Benny Halevycbe82602011-05-22 19:52:37 +03008574 dprintk("<-- %s\n", __func__);
8575}
8576
8577static void nfs4_layoutreturn_release(void *calldata)
8578{
8579 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008580 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008581
8582 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008583 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008584 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008585 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008586 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8587 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008588 pnfs_put_layout_hdr(lrp->args.layout);
8589 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008590 kfree(calldata);
8591 dprintk("<-- %s\n", __func__);
8592}
8593
8594static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8595 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8596 .rpc_call_done = nfs4_layoutreturn_done,
8597 .rpc_release = nfs4_layoutreturn_release,
8598};
8599
Peng Tao6c166052014-11-17 09:30:40 +08008600int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008601{
8602 struct rpc_task *task;
8603 struct rpc_message msg = {
8604 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8605 .rpc_argp = &lrp->args,
8606 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008607 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008608 };
8609 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008610 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008611 .rpc_message = &msg,
8612 .callback_ops = &nfs4_layoutreturn_call_ops,
8613 .callback_data = lrp,
8614 };
Peng Tao6c166052014-11-17 09:30:40 +08008615 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008616
Andrew Elble99ade3c2015-12-02 09:39:51 -05008617 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8618 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8619 &task_setup_data.rpc_client, &msg);
8620
Benny Halevycbe82602011-05-22 19:52:37 +03008621 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008622 if (!sync) {
8623 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8624 if (!lrp->inode) {
8625 nfs4_layoutreturn_release(lrp);
8626 return -EAGAIN;
8627 }
8628 task_setup_data.flags |= RPC_TASK_ASYNC;
8629 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008630 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008631 task = rpc_run_task(&task_setup_data);
8632 if (IS_ERR(task))
8633 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008634 if (sync)
8635 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008636 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008637 dprintk("<-- %s status=%d\n", __func__, status);
8638 rpc_put_task(task);
8639 return status;
8640}
8641
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008642static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008643_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8644 struct pnfs_device *pdev,
8645 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008646{
8647 struct nfs4_getdeviceinfo_args args = {
8648 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008649 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8650 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008651 };
8652 struct nfs4_getdeviceinfo_res res = {
8653 .pdev = pdev,
8654 };
8655 struct rpc_message msg = {
8656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8657 .rpc_argp = &args,
8658 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008659 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008660 };
8661 int status;
8662
8663 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008664 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008665 if (res.notification & ~args.notify_types)
8666 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008667 if (res.notification != args.notify_types)
8668 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008669
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008670 dprintk("<-- %s status=%d\n", __func__, status);
8671
8672 return status;
8673}
8674
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008675int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8676 struct pnfs_device *pdev,
8677 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008678{
8679 struct nfs4_exception exception = { };
8680 int err;
8681
8682 do {
8683 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008684 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008685 &exception);
8686 } while (exception.retry);
8687 return err;
8688}
8689EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8690
Andy Adamson863a3c62011-03-23 13:27:54 +00008691static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8692{
8693 struct nfs4_layoutcommit_data *data = calldata;
8694 struct nfs_server *server = NFS_SERVER(data->args.inode);
8695
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008696 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008697 &data->args.seq_args,
8698 &data->res.seq_res,
8699 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008700}
8701
8702static void
8703nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8704{
8705 struct nfs4_layoutcommit_data *data = calldata;
8706 struct nfs_server *server = NFS_SERVER(data->args.inode);
8707
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008708 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008709 return;
8710
8711 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008712 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8713 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8714 case -NFS4ERR_BADLAYOUT: /* no layout */
8715 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008716 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008717 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008718 break;
8719 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008720 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008721 rpc_restart_call_prepare(task);
8722 return;
8723 }
8724 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008725}
8726
8727static void nfs4_layoutcommit_release(void *calldata)
8728{
8729 struct nfs4_layoutcommit_data *data = calldata;
8730
Andy Adamsondb29c082011-07-30 20:52:38 -04008731 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008732 nfs_post_op_update_inode_force_wcc(data->args.inode,
8733 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008734 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008735 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008736 kfree(data);
8737}
8738
8739static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8740 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8741 .rpc_call_done = nfs4_layoutcommit_done,
8742 .rpc_release = nfs4_layoutcommit_release,
8743};
8744
8745int
Andy Adamsonef311532011-03-12 02:58:10 -05008746nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008747{
8748 struct rpc_message msg = {
8749 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8750 .rpc_argp = &data->args,
8751 .rpc_resp = &data->res,
8752 .rpc_cred = data->cred,
8753 };
8754 struct rpc_task_setup task_setup_data = {
8755 .task = &data->task,
8756 .rpc_client = NFS_CLIENT(data->args.inode),
8757 .rpc_message = &msg,
8758 .callback_ops = &nfs4_layoutcommit_ops,
8759 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008760 };
8761 struct rpc_task *task;
8762 int status = 0;
8763
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008764 dprintk("NFS: initiating layoutcommit call. sync %d "
8765 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008766 data->args.lastbytewritten,
8767 data->args.inode->i_ino);
8768
Trond Myklebust472e2592015-02-05 16:50:30 -05008769 if (!sync) {
8770 data->inode = nfs_igrab_and_active(data->args.inode);
8771 if (data->inode == NULL) {
8772 nfs4_layoutcommit_release(data);
8773 return -EAGAIN;
8774 }
8775 task_setup_data.flags = RPC_TASK_ASYNC;
8776 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008777 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008778 task = rpc_run_task(&task_setup_data);
8779 if (IS_ERR(task))
8780 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008781 if (sync)
8782 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008783 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008784 dprintk("%s: status %d\n", __func__, status);
8785 rpc_put_task(task);
8786 return status;
8787}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008788
Andy Adamson97431202013-08-08 10:57:56 -04008789/**
8790 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8791 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8792 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008793static int
8794_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008795 struct nfs_fsinfo *info,
8796 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008797{
8798 struct nfs41_secinfo_no_name_args args = {
8799 .style = SECINFO_STYLE_CURRENT_FH,
8800 };
8801 struct nfs4_secinfo_res res = {
8802 .flavors = flavors,
8803 };
8804 struct rpc_message msg = {
8805 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8806 .rpc_argp = &args,
8807 .rpc_resp = &res,
8808 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008809 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008810 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008811 int status;
8812
8813 if (use_integrity) {
8814 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008815 cred = nfs4_get_clid_cred(server->nfs_client);
8816 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008817 }
8818
8819 dprintk("--> %s\n", __func__);
8820 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8821 &res.seq_res, 0);
8822 dprintk("<-- %s status=%d\n", __func__, status);
8823
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008824 if (cred)
8825 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008826
8827 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008828}
8829
8830static int
8831nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8832 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8833{
8834 struct nfs4_exception exception = { };
8835 int err;
8836 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008837 /* first try using integrity protection */
8838 err = -NFS4ERR_WRONGSEC;
8839
8840 /* try to use integrity protection with machine cred */
8841 if (_nfs4_is_integrity_protected(server->nfs_client))
8842 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8843 flavors, true);
8844
8845 /*
8846 * if unable to use integrity protection, or SECINFO with
8847 * integrity protection returns NFS4ERR_WRONGSEC (which is
8848 * disallowed by spec, but exists in deployed servers) use
8849 * the current filesystem's rpc_client and the user cred.
8850 */
8851 if (err == -NFS4ERR_WRONGSEC)
8852 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8853 flavors, false);
8854
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008855 switch (err) {
8856 case 0:
8857 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008858 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008859 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008860 default:
8861 err = nfs4_handle_exception(server, err, &exception);
8862 }
8863 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008864out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008865 return err;
8866}
8867
8868static int
8869nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8870 struct nfs_fsinfo *info)
8871{
8872 int err;
8873 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008874 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008875 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008876 struct nfs4_secinfo4 *secinfo;
8877 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008878
8879 page = alloc_page(GFP_KERNEL);
8880 if (!page) {
8881 err = -ENOMEM;
8882 goto out;
8883 }
8884
8885 flavors = page_address(page);
8886 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8887
8888 /*
8889 * Fall back on "guess and check" method if
8890 * the server doesn't support SECINFO_NO_NAME
8891 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008892 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008893 err = nfs4_find_root_sec(server, fhandle, info);
8894 goto out_freepage;
8895 }
8896 if (err)
8897 goto out_freepage;
8898
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008899 for (i = 0; i < flavors->num_flavors; i++) {
8900 secinfo = &flavors->flavors[i];
8901
8902 switch (secinfo->flavor) {
8903 case RPC_AUTH_NULL:
8904 case RPC_AUTH_UNIX:
8905 case RPC_AUTH_GSS:
8906 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8907 &secinfo->flavor_info);
8908 break;
8909 default:
8910 flavor = RPC_AUTH_MAXFLAVOR;
8911 break;
8912 }
8913
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008914 if (!nfs_auth_info_match(&server->auth_info, flavor))
8915 flavor = RPC_AUTH_MAXFLAVOR;
8916
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008917 if (flavor != RPC_AUTH_MAXFLAVOR) {
8918 err = nfs4_lookup_root_sec(server, fhandle,
8919 info, flavor);
8920 if (!err)
8921 break;
8922 }
8923 }
8924
8925 if (flavor == RPC_AUTH_MAXFLAVOR)
8926 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008927
8928out_freepage:
8929 put_page(page);
8930 if (err == -EACCES)
8931 return -EPERM;
8932out:
8933 return err;
8934}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008935
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008936static int _nfs41_test_stateid(struct nfs_server *server,
8937 nfs4_stateid *stateid,
8938 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008939{
8940 int status;
8941 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008942 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008943 };
8944 struct nfs41_test_stateid_res res;
8945 struct rpc_message msg = {
8946 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8947 .rpc_argp = &args,
8948 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008949 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008950 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008951 struct rpc_clnt *rpc_client = server->client;
8952
8953 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8954 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008955
Chuck Lever38527b12012-07-11 16:30:23 -04008956 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008957 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008958 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008959 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008960 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008961 if (status != NFS_OK) {
8962 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008963 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008964 }
8965 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008966 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008967}
8968
Trond Myklebust43912bb2016-09-22 13:38:56 -04008969static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8970 int err, struct nfs4_exception *exception)
8971{
8972 exception->retry = 0;
8973 switch(err) {
8974 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008975 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008976 nfs4_handle_exception(server, err, exception);
8977 break;
8978 case -NFS4ERR_BADSESSION:
8979 case -NFS4ERR_BADSLOT:
8980 case -NFS4ERR_BAD_HIGH_SLOT:
8981 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8982 case -NFS4ERR_DEADSESSION:
8983 nfs4_do_handle_exception(server, err, exception);
8984 }
8985}
8986
Chuck Lever38527b12012-07-11 16:30:23 -04008987/**
8988 * nfs41_test_stateid - perform a TEST_STATEID operation
8989 *
8990 * @server: server / transport on which to perform the operation
8991 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008992 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008993 *
8994 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8995 * Otherwise a negative NFS4ERR value is returned if the operation
8996 * failed or the state ID is not currently valid.
8997 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008998static int nfs41_test_stateid(struct nfs_server *server,
8999 nfs4_stateid *stateid,
9000 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009001{
9002 struct nfs4_exception exception = { };
9003 int err;
9004 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009005 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009006 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009007 } while (exception.retry);
9008 return err;
9009}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009010
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009011struct nfs_free_stateid_data {
9012 struct nfs_server *server;
9013 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009014 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009015};
9016
9017static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9018{
9019 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009020 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009021 &data->args.seq_args,
9022 &data->res.seq_res,
9023 task);
9024}
9025
9026static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9027{
9028 struct nfs_free_stateid_data *data = calldata;
9029
9030 nfs41_sequence_done(task, &data->res.seq_res);
9031
9032 switch (task->tk_status) {
9033 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009034 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009035 rpc_restart_call_prepare(task);
9036 }
9037}
9038
9039static void nfs41_free_stateid_release(void *calldata)
9040{
9041 kfree(calldata);
9042}
9043
Trond Myklebust17f26b12013-08-21 15:48:42 -04009044static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009045 .rpc_call_prepare = nfs41_free_stateid_prepare,
9046 .rpc_call_done = nfs41_free_stateid_done,
9047 .rpc_release = nfs41_free_stateid_release,
9048};
9049
9050static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009051 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009052 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009053 bool privileged)
9054{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009055 struct rpc_message msg = {
9056 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009057 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009058 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009059 struct rpc_task_setup task_setup = {
9060 .rpc_client = server->client,
9061 .rpc_message = &msg,
9062 .callback_ops = &nfs41_free_stateid_ops,
9063 .flags = RPC_TASK_ASYNC,
9064 };
9065 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009066
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009067 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9068 &task_setup.rpc_client, &msg);
9069
Chuck Lever38527b12012-07-11 16:30:23 -04009070 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009071 data = kmalloc(sizeof(*data), GFP_NOFS);
9072 if (!data)
9073 return ERR_PTR(-ENOMEM);
9074 data->server = server;
9075 nfs4_stateid_copy(&data->args.stateid, stateid);
9076
9077 task_setup.callback_data = data;
9078
9079 msg.rpc_argp = &data->args;
9080 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009081 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009082 if (privileged)
9083 nfs4_set_sequence_privileged(&data->args.seq_args);
9084
9085 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009086}
9087
Chuck Lever38527b12012-07-11 16:30:23 -04009088/**
9089 * nfs41_free_stateid - perform a FREE_STATEID operation
9090 *
9091 * @server: server / transport on which to perform the operation
9092 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009093 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009094 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009095 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009096 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009097 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009098static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009099 const nfs4_stateid *stateid,
9100 struct rpc_cred *cred,
9101 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009102{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009103 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009104
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009105 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009106 if (IS_ERR(task))
9107 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009108 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009109 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009110}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009111
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009112static void
9113nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009114{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009115 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009116
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009117 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009118 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009119}
9120
Trond Myklebust36281ca2012-03-04 18:13:56 -05009121static bool nfs41_match_stateid(const nfs4_stateid *s1,
9122 const nfs4_stateid *s2)
9123{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009124 if (s1->type != s2->type)
9125 return false;
9126
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009127 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009128 return false;
9129
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009130 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009131 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009132 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009133 return true;
9134
9135 return false;
9136}
9137
Andy Adamson557134a2009-04-01 09:21:53 -04009138#endif /* CONFIG_NFS_V4_1 */
9139
Trond Myklebust36281ca2012-03-04 18:13:56 -05009140static bool nfs4_match_stateid(const nfs4_stateid *s1,
9141 const nfs4_stateid *s2)
9142{
Trond Myklebustf597c532012-03-04 18:13:56 -05009143 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009144}
9145
9146
Trond Myklebust17280172012-03-11 13:11:00 -04009147static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009148 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009149 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009150 .recover_open = nfs4_open_reclaim,
9151 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009152 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009153 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009154};
9155
Andy Adamson591d71c2009-04-01 09:22:47 -04009156#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009157static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009158 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9159 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9160 .recover_open = nfs4_open_reclaim,
9161 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009162 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009163 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009164 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009165};
9166#endif /* CONFIG_NFS_V4_1 */
9167
Trond Myklebust17280172012-03-11 13:11:00 -04009168static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009169 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009170 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009171 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009172 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009173 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009174};
9175
Andy Adamson591d71c2009-04-01 09:22:47 -04009176#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009177static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009178 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9179 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009180 .recover_open = nfs41_open_expired,
9181 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009182 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009183};
9184#endif /* CONFIG_NFS_V4_1 */
9185
Trond Myklebust17280172012-03-11 13:11:00 -04009186static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009187 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009188 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009189 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009190};
9191
9192#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009193static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009194 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009195 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009196 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009197};
9198#endif
9199
Chuck Leverec011fe2013-10-17 14:12:39 -04009200static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009201 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009202 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009203};
9204
9205#if defined(CONFIG_NFS_V4_1)
9206static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009207 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009208 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009209};
9210#endif /* CONFIG_NFS_V4_1 */
9211
Trond Myklebust97dc1352010-06-16 09:52:26 -04009212static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9213 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009214 .init_caps = NFS_CAP_READDIRPLUS
9215 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009216 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009217 .init_client = nfs40_init_client,
9218 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009219 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009220 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009221 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009222 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009223 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009224 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009225 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9226 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9227 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009228 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009229};
9230
9231#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009232static struct nfs_seqid *
9233nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9234{
9235 return NULL;
9236}
9237
Trond Myklebust97dc1352010-06-16 09:52:26 -04009238static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9239 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009240 .init_caps = NFS_CAP_READDIRPLUS
9241 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009242 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009243 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009244 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009245 .init_client = nfs41_init_client,
9246 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009247 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009248 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009249 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009250 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009251 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009252 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009253 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009254 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9255 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9256 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009257 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009258};
9259#endif
9260
Steve Dickson42c2c422013-05-22 12:50:38 -04009261#if defined(CONFIG_NFS_V4_2)
9262static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9263 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009264 .init_caps = NFS_CAP_READDIRPLUS
9265 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009266 | NFS_CAP_POSIX_LOCK
9267 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009268 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009269 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009270 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009271 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009272 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009273 | NFS_CAP_LAYOUTSTATS
9274 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009275 .init_client = nfs41_init_client,
9276 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009277 .match_stateid = nfs41_match_stateid,
9278 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009279 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009280 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009281 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009282 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009283 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009284 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9285 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9286 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009287 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009288};
9289#endif
9290
Trond Myklebust97dc1352010-06-16 09:52:26 -04009291const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9292 [0] = &nfs_v4_0_minor_ops,
9293#if defined(CONFIG_NFS_V4_1)
9294 [1] = &nfs_v4_1_minor_ops,
9295#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009296#if defined(CONFIG_NFS_V4_2)
9297 [2] = &nfs_v4_2_minor_ops,
9298#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009299};
9300
Trond Myklebust13997822016-07-24 17:10:52 -04009301static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009302{
9303 ssize_t error, error2;
9304
9305 error = generic_listxattr(dentry, list, size);
9306 if (error < 0)
9307 return error;
9308 if (list) {
9309 list += error;
9310 size -= error;
9311 }
9312
9313 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9314 if (error2 < 0)
9315 return error2;
9316 return error + error2;
9317}
9318
Trond Myklebust17f26b12013-08-21 15:48:42 -04009319static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009320 .create = nfs_create,
9321 .lookup = nfs_lookup,
9322 .atomic_open = nfs_atomic_open,
9323 .link = nfs_link,
9324 .unlink = nfs_unlink,
9325 .symlink = nfs_symlink,
9326 .mkdir = nfs_mkdir,
9327 .rmdir = nfs_rmdir,
9328 .mknod = nfs_mknod,
9329 .rename = nfs_rename,
9330 .permission = nfs_permission,
9331 .getattr = nfs_getattr,
9332 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009333 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009334};
9335
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009336static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009337 .permission = nfs_permission,
9338 .getattr = nfs_getattr,
9339 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009340 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009341};
9342
David Howells509de812006-08-22 20:06:11 -04009343const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009344 .version = 4, /* protocol version */
9345 .dentry_ops = &nfs4_dentry_operations,
9346 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009347 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009348 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009349 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009350 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009351 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009352 .getattr = nfs4_proc_getattr,
9353 .setattr = nfs4_proc_setattr,
9354 .lookup = nfs4_proc_lookup,
9355 .access = nfs4_proc_access,
9356 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009357 .create = nfs4_proc_create,
9358 .remove = nfs4_proc_remove,
9359 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009360 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009361 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009362 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009363 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009364 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009365 .link = nfs4_proc_link,
9366 .symlink = nfs4_proc_symlink,
9367 .mkdir = nfs4_proc_mkdir,
9368 .rmdir = nfs4_proc_remove,
9369 .readdir = nfs4_proc_readdir,
9370 .mknod = nfs4_proc_mknod,
9371 .statfs = nfs4_proc_statfs,
9372 .fsinfo = nfs4_proc_fsinfo,
9373 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009374 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009375 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009376 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009377 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009378 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009379 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009380 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009381 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009382 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009383 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009384 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009385 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009386 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009387 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009388 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009389 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009390 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009391 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009392 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009393 .create_server = nfs4_create_server,
9394 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009395};
9396
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009397static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009398 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009399 .list = nfs4_xattr_list_nfs4_acl,
9400 .get = nfs4_xattr_get_nfs4_acl,
9401 .set = nfs4_xattr_set_nfs4_acl,
9402};
9403
9404const struct xattr_handler *nfs4_xattr_handlers[] = {
9405 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009406#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9407 &nfs4_xattr_nfs4_label_handler,
9408#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009409 NULL
9410};
9411
Linus Torvalds1da177e2005-04-16 15:20:36 -07009412/*
9413 * Local variables:
9414 * c-basic-offset: 8
9415 * End:
9416 */