blob: b577e172c95b35bbc1f6d63d92a2735ba0aaa0d0 [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) {
1642 if (data->rpc_status) {
1643 ret = data->rpc_status;
1644 goto err;
1645 }
1646 /* cached opens have already been processed */
1647 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001648 }
1649
Andy Adamsone23008e2012-10-02 21:07:32 -04001650 ret = nfs_refresh_inode(inode, &data->f_attr);
1651 if (ret)
1652 goto err;
1653
1654 if (data->o_res.delegation_type != 0)
1655 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001656update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001657 update_open_stateid(state, &data->o_res.stateid, NULL,
1658 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001659 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001660
1661 return state;
1662err:
1663 return ERR_PTR(ret);
1664
1665}
1666
1667static struct nfs4_state *
1668_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001669{
1670 struct inode *inode;
1671 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001672 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001673
Trond Myklebustaac00a82007-07-05 19:02:21 -04001674 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001675 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001676 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001677 goto out;
1678 }
1679
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001680 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001681 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001682 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001683 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001684 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001685 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001686 goto err;
1687 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001688 state = nfs4_get_open_state(inode, data->owner);
1689 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001690 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001691 if (data->o_res.delegation_type != 0)
1692 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001693 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001694 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001695 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001696out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001697 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001698 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001699err_put_inode:
1700 iput(inode);
1701err:
1702 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001703}
1704
Andy Adamsone23008e2012-10-02 21:07:32 -04001705static struct nfs4_state *
1706nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1707{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001708 struct nfs4_state *ret;
1709
Andy Adamsone23008e2012-10-02 21:07:32 -04001710 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001711 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1712 else
1713 ret = _nfs4_opendata_to_nfs4_state(data);
1714 nfs4_sequence_free_slot(&data->o_res.seq_res);
1715 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001716}
1717
Trond Myklebust864472e2006-01-03 09:55:15 +01001718static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1719{
1720 struct nfs_inode *nfsi = NFS_I(state->inode);
1721 struct nfs_open_context *ctx;
1722
1723 spin_lock(&state->inode->i_lock);
1724 list_for_each_entry(ctx, &nfsi->open_files, list) {
1725 if (ctx->state != state)
1726 continue;
1727 get_nfs_open_context(ctx);
1728 spin_unlock(&state->inode->i_lock);
1729 return ctx;
1730 }
1731 spin_unlock(&state->inode->i_lock);
1732 return ERR_PTR(-ENOENT);
1733}
1734
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001735static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1736 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001737{
1738 struct nfs4_opendata *opendata;
1739
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001740 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001741 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001742 if (opendata == NULL)
1743 return ERR_PTR(-ENOMEM);
1744 opendata->state = state;
1745 atomic_inc(&state->count);
1746 return opendata;
1747}
1748
Trond Myklebust24311f82015-09-20 10:50:17 -04001749static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1750 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001751{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001752 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001753 int ret;
1754
Trond Myklebust24311f82015-09-20 10:50:17 -04001755 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001756 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001757 opendata->o_arg.open_flags = 0;
1758 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001759 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1760 NFS_SB(opendata->dentry->d_sb),
1761 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001762 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1763 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1764 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001765 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001766 if (ret != 0)
1767 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001768 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001769 if (IS_ERR(newstate))
1770 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001771 if (newstate != opendata->state)
1772 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001773 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001774 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001775}
1776
1777static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1778{
Trond Myklebust864472e2006-01-03 09:55:15 +01001779 int ret;
1780
Trond Myklebust4f14c192014-02-12 19:15:06 -05001781 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1782 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1783 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1784 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001785 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001786 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001787 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001788 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001789 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1790 if (ret != 0)
1791 return ret;
1792 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1793 if (ret != 0)
1794 return ret;
1795 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1796 if (ret != 0)
1797 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001798 /*
1799 * We may have performed cached opens for all three recoveries.
1800 * Check if we need to update the current stateid.
1801 */
1802 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001803 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001804 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001805 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001806 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001807 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001808 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001809 return 0;
1810}
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812/*
1813 * OPEN_RECLAIM:
1814 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001816static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001818 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001819 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001820 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 int status;
1822
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001823 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1824 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001825 if (IS_ERR(opendata))
1826 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001827 rcu_read_lock();
1828 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001829 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001830 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001831 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001832 opendata->o_arg.u.delegation_type = delegation_type;
1833 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001834 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return status;
1836}
1837
Trond Myklebust539cd032007-06-05 11:46:42 -04001838static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
1840 struct nfs_server *server = NFS_SERVER(state->inode);
1841 struct nfs4_exception exception = { };
1842 int err;
1843 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001844 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001845 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001846 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1847 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001848 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001849 break;
1850 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 } while (exception.retry);
1852 return err;
1853}
1854
Trond Myklebust864472e2006-01-03 09:55:15 +01001855static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1856{
1857 struct nfs_open_context *ctx;
1858 int ret;
1859
1860 ctx = nfs4_state_find_open_context(state);
1861 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001862 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001863 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001864 put_nfs_open_context(ctx);
1865 return ret;
1866}
1867
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001868static 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 -07001869{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001870 switch (err) {
1871 default:
1872 printk(KERN_ERR "NFS: %s: unhandled error "
1873 "%d.\n", __func__, err);
1874 case 0:
1875 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001876 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001877 case -ESTALE:
1878 break;
1879 case -NFS4ERR_BADSESSION:
1880 case -NFS4ERR_BADSLOT:
1881 case -NFS4ERR_BAD_HIGH_SLOT:
1882 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1883 case -NFS4ERR_DEADSESSION:
1884 set_bit(NFS_DELEGATED_STATE, &state->flags);
1885 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1886 return -EAGAIN;
1887 case -NFS4ERR_STALE_CLIENTID:
1888 case -NFS4ERR_STALE_STATEID:
1889 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001890 /* Don't recall a delegation if it was lost */
1891 nfs4_schedule_lease_recovery(server->nfs_client);
1892 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001893 case -NFS4ERR_MOVED:
1894 nfs4_schedule_migration_recovery(server);
1895 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001896 case -NFS4ERR_LEASE_MOVED:
1897 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1898 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001899 case -NFS4ERR_DELEG_REVOKED:
1900 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001901 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001902 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001903 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001904 nfs_inode_find_state_and_recover(state->inode,
1905 stateid);
1906 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001907 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001908 case -NFS4ERR_DELAY:
1909 case -NFS4ERR_GRACE:
1910 set_bit(NFS_DELEGATED_STATE, &state->flags);
1911 ssleep(1);
1912 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001913 case -ENOMEM:
1914 case -NFS4ERR_DENIED:
1915 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1916 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return err;
1919}
1920
Trond Myklebust24311f82015-09-20 10:50:17 -04001921int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1922 struct nfs4_state *state, const nfs4_stateid *stateid,
1923 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001924{
1925 struct nfs_server *server = NFS_SERVER(state->inode);
1926 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001927 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001928
1929 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1930 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1931 if (IS_ERR(opendata))
1932 return PTR_ERR(opendata);
1933 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001934 write_seqlock(&state->seqlock);
1935 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1936 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001937 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1938 switch (type & (FMODE_READ|FMODE_WRITE)) {
1939 case FMODE_READ|FMODE_WRITE:
1940 case FMODE_WRITE:
1941 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1942 if (err)
1943 break;
1944 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1945 if (err)
1946 break;
1947 case FMODE_READ:
1948 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1949 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001950 nfs4_opendata_put(opendata);
1951 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1952}
1953
Chuck Leverbe05c862013-08-09 12:49:47 -04001954static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1955{
1956 struct nfs4_opendata *data = calldata;
1957
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001958 nfs4_setup_sequence(data->o_arg.server->nfs_client,
1959 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001960}
1961
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001962static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1963{
1964 struct nfs4_opendata *data = calldata;
1965
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001966 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001967
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001968 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001969 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001970 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001971 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001972 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001973 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001974 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001975}
1976
1977static void nfs4_open_confirm_release(void *calldata)
1978{
1979 struct nfs4_opendata *data = calldata;
1980 struct nfs4_state *state = NULL;
1981
1982 /* If this request hasn't been cancelled, do nothing */
1983 if (data->cancelled == 0)
1984 goto out_free;
1985 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001986 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001987 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001988 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001989 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001990 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001991out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001992 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001993}
1994
1995static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001996 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001997 .rpc_call_done = nfs4_open_confirm_done,
1998 .rpc_release = nfs4_open_confirm_release,
1999};
2000
2001/*
2002 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2003 */
2004static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2005{
David Howells2b0143b2015-03-17 22:25:59 +00002006 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002007 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002008 struct rpc_message msg = {
2009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2010 .rpc_argp = &data->c_arg,
2011 .rpc_resp = &data->c_res,
2012 .rpc_cred = data->owner->so_cred,
2013 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002014 struct rpc_task_setup task_setup_data = {
2015 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002016 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002017 .callback_ops = &nfs4_open_confirm_ops,
2018 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002019 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002020 .flags = RPC_TASK_ASYNC,
2021 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 int status;
2023
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002024 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002025 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002026 data->rpc_done = 0;
2027 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002028 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002029 if (data->is_recover)
2030 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002031 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002032 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002033 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002034 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002035 if (status != 0) {
2036 data->cancelled = 1;
2037 smp_wmb();
2038 } else
2039 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002040 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 return status;
2042}
2043
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002044static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002046 struct nfs4_opendata *data = calldata;
2047 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002048 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002049 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002050
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002051 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002052 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002053 /*
2054 * Check if we still need to send an OPEN call, or if we can use
2055 * a delegation instead.
2056 */
2057 if (data->state != NULL) {
2058 struct nfs_delegation *delegation;
2059
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002060 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002061 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002062 rcu_read_lock();
2063 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002064 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002065 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002066 rcu_read_unlock();
2067 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002068 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002069 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002070 switch (claim) {
2071 default:
2072 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002073 case NFS4_OPEN_CLAIM_PREVIOUS:
2074 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2075 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002076 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002077 case NFS4_OPEN_CLAIM_FH:
2078 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002079 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2080 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002081 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002082 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002083 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002084 &data->o_res.seq_res,
2085 task) != 0)
2086 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002087
2088 /* Set the create mode (note dependency on the session type) */
2089 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2090 if (data->o_arg.open_flags & O_EXCL) {
2091 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2092 if (nfs4_has_persistent_session(clp))
2093 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2094 else if (clp->cl_mvops->minor_version > 0)
2095 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2096 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002097 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002098unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002099 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002100 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002101out_no_action:
2102 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002103out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002104 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002105}
2106
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002107static void nfs4_open_done(struct rpc_task *task, void *calldata)
2108{
2109 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002111 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002112
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002113 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002114 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002115
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002116 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002117 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2118 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002119 case S_IFREG:
2120 break;
2121 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002122 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002123 break;
2124 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002125 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002126 break;
2127 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002128 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002129 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002130 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002131 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002132 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2133 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002134 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002135 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002136}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002137
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002138static void nfs4_open_release(void *calldata)
2139{
2140 struct nfs4_opendata *data = calldata;
2141 struct nfs4_state *state = NULL;
2142
2143 /* If this request hasn't been cancelled, do nothing */
2144 if (data->cancelled == 0)
2145 goto out_free;
2146 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002147 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002148 goto out_free;
2149 /* In case we need an open_confirm, no cleanup! */
2150 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2151 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002152 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002153 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002154 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002155out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002156 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002157}
2158
2159static const struct rpc_call_ops nfs4_open_ops = {
2160 .rpc_call_prepare = nfs4_open_prepare,
2161 .rpc_call_done = nfs4_open_done,
2162 .rpc_release = nfs4_open_release,
2163};
2164
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002165static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002166{
David Howells2b0143b2015-03-17 22:25:59 +00002167 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002168 struct nfs_server *server = NFS_SERVER(dir);
2169 struct nfs_openargs *o_arg = &data->o_arg;
2170 struct nfs_openres *o_res = &data->o_res;
2171 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002172 struct rpc_message msg = {
2173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2174 .rpc_argp = o_arg,
2175 .rpc_resp = o_res,
2176 .rpc_cred = data->owner->so_cred,
2177 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002178 struct rpc_task_setup task_setup_data = {
2179 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002180 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002181 .callback_ops = &nfs4_open_ops,
2182 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002183 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002184 .flags = RPC_TASK_ASYNC,
2185 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002186 int status;
2187
Chuck Levera9c92d62013-08-09 12:48:18 -04002188 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002189 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002190 data->rpc_done = 0;
2191 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002192 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002193 data->is_recover = 0;
2194 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002195 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002196 data->is_recover = 1;
2197 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002198 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002199 if (IS_ERR(task))
2200 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002201 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002202 if (status != 0) {
2203 data->cancelled = 1;
2204 smp_wmb();
2205 } else
2206 status = data->rpc_status;
2207 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002208
2209 return status;
2210}
2211
2212static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2213{
David Howells2b0143b2015-03-17 22:25:59 +00002214 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002215 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002216 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002217
2218 status = nfs4_run_open_task(data, 1);
2219 if (status != 0 || !data->rpc_done)
2220 return status;
2221
Trond Myklebust6926afd2012-01-07 13:22:46 -05002222 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2223
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002224 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2225 status = _nfs4_proc_open_confirm(data);
2226 if (status != 0)
2227 return status;
2228 }
2229
2230 return status;
2231}
2232
Trond Myklebustf3792d62014-07-10 08:54:32 -04002233/*
2234 * Additional permission checks in order to distinguish between an
2235 * open for read, and an open for execute. This works around the
2236 * fact that NFSv4 OPEN treats read and execute permissions as being
2237 * the same.
2238 * Note that in the non-execute case, we want to turn off permission
2239 * checking if we just created a new file (POSIX open() semantics).
2240 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002241static int nfs4_opendata_access(struct rpc_cred *cred,
2242 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002243 struct nfs4_state *state, fmode_t fmode,
2244 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002245{
2246 struct nfs_access_entry cache;
2247 u32 mask;
2248
2249 /* access call failed or for some reason the server doesn't
2250 * support any access modes -- defer access call until later */
2251 if (opendata->o_res.access_supported == 0)
2252 return 0;
2253
2254 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002255 /*
2256 * Use openflags to check for exec, because fmode won't
2257 * always have FMODE_EXEC set when file open for exec.
2258 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002259 if (openflags & __FMODE_EXEC) {
2260 /* ONLY check for exec rights */
2261 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002262 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002263 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002264
2265 cache.cred = cred;
2266 cache.jiffies = jiffies;
2267 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2268 nfs_access_add_cache(state->inode, &cache);
2269
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002270 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002271 return 0;
2272
2273 /* even though OPEN succeeded, access is denied. Close the file */
2274 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002275 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002276}
2277
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002278/*
2279 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2280 */
2281static int _nfs4_proc_open(struct nfs4_opendata *data)
2282{
David Howells2b0143b2015-03-17 22:25:59 +00002283 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002284 struct nfs_server *server = NFS_SERVER(dir);
2285 struct nfs_openargs *o_arg = &data->o_arg;
2286 struct nfs_openres *o_res = &data->o_res;
2287 int status;
2288
2289 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002290 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002291 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002292 if (status != 0) {
2293 if (status == -NFS4ERR_BADNAME &&
2294 !(o_arg->open_flags & O_CREAT))
2295 return -ENOENT;
2296 return status;
2297 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002298
Trond Myklebust6926afd2012-01-07 13:22:46 -05002299 nfs_fattr_map_and_free_names(server, &data->f_attr);
2300
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002301 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002302 if (o_arg->open_flags & O_EXCL)
2303 data->file_created = 1;
2304 else if (o_res->cinfo.before != o_res->cinfo.after)
2305 data->file_created = 1;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002306 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002307 update_changeattr(dir, &o_res->cinfo,
2308 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002309 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002310 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2311 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002313 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002315 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 }
2317 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002318 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320}
2321
Andy Adamsond83217c2011-03-01 01:34:17 +00002322static int nfs4_recover_expired_lease(struct nfs_server *server)
2323{
2324 return nfs4_client_recover_expired_lease(server->nfs_client);
2325}
2326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327/*
2328 * OPEN_EXPIRED:
2329 * reclaim state on the server after a network partition.
2330 * Assumes caller holds the appropriate lock
2331 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002332static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002334 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002335 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002337 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002338 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002339 if (IS_ERR(opendata))
2340 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002341 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002342 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002343 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002344 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002345 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002348static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002349{
Trond Myklebust539cd032007-06-05 11:46:42 -04002350 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002351 struct nfs4_exception exception = { };
2352 int err;
2353
2354 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002355 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002356 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002357 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2358 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002359 switch (err) {
2360 default:
2361 goto out;
2362 case -NFS4ERR_GRACE:
2363 case -NFS4ERR_DELAY:
2364 nfs4_handle_exception(server, err, &exception);
2365 err = 0;
2366 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002367 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002368out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002369 return err;
2370}
2371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2373{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002375 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Trond Myklebust864472e2006-01-03 09:55:15 +01002377 ctx = nfs4_state_find_open_context(state);
2378 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002379 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002380 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002381 put_nfs_open_context(ctx);
2382 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
2384
Trond Myklebust41020b62016-09-22 13:38:58 -04002385static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2386 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002387{
Trond Myklebust41020b62016-09-22 13:38:58 -04002388 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002389 write_seqlock(&state->seqlock);
2390 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2391 write_sequnlock(&state->seqlock);
2392 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2393}
2394
2395static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2396{
2397 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002398 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002399}
2400
2401static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2402{
2403 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2404 nfs40_clear_delegation_stateid(state);
2405 return nfs4_open_expired(sp, state);
2406}
2407
Trond Myklebust45870d62016-09-22 13:38:59 -04002408static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2409 nfs4_stateid *stateid,
2410 struct rpc_cred *cred)
2411{
2412 return -NFS4ERR_BAD_STATEID;
2413}
2414
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002415#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002416static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2417 nfs4_stateid *stateid,
2418 struct rpc_cred *cred)
2419{
2420 int status;
2421
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002422 switch (stateid->type) {
2423 default:
2424 break;
2425 case NFS4_INVALID_STATEID_TYPE:
2426 case NFS4_SPECIAL_STATEID_TYPE:
2427 return -NFS4ERR_BAD_STATEID;
2428 case NFS4_REVOKED_STATEID_TYPE:
2429 goto out_free;
2430 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002431
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002432 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002433 switch (status) {
2434 case -NFS4ERR_EXPIRED:
2435 case -NFS4ERR_ADMIN_REVOKED:
2436 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002437 break;
2438 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002439 return status;
2440 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002441out_free:
2442 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002443 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002444 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002445}
2446
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002447static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002448{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002449 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002450 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002451 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002452 struct rpc_cred *cred;
2453 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002454
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002455 /* Get the delegation credential for use by test/free_stateid */
2456 rcu_read_lock();
2457 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002458 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002459 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002460 return;
2461 }
2462
2463 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002464 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2465 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002466 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002467 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002468 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002469
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002470 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2471 rcu_read_unlock();
2472 return;
2473 }
2474
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002475 cred = get_rpccred(delegation->cred);
2476 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002477 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002478 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002479 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002480 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002481
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002482 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002483}
2484
2485/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002486 * nfs41_check_expired_locks - possibly free a lock stateid
2487 *
2488 * @state: NFSv4 state for an inode
2489 *
2490 * Returns NFS_OK if recovery for this stateid is now finished.
2491 * Otherwise a negative NFS4ERR value is returned.
2492 */
2493static int nfs41_check_expired_locks(struct nfs4_state *state)
2494{
2495 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002496 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002497 struct nfs_server *server = NFS_SERVER(state->inode);
2498
2499 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2500 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002501
2502 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002503 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2504 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2505 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2506
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002507 atomic_inc(&lsp->ls_count);
2508 spin_unlock(&state->state_lock);
2509
2510 nfs4_put_lock_state(prev);
2511 prev = lsp;
2512
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002513 status = nfs41_test_and_free_expired_stateid(server,
2514 &lsp->ls_stateid,
2515 cred);
2516 trace_nfs4_test_lock_stateid(state, lsp, status);
2517 if (status == -NFS4ERR_EXPIRED ||
2518 status == -NFS4ERR_BAD_STATEID) {
2519 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002520 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002521 if (!recover_lost_locks)
2522 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2523 } else if (status != NFS_OK) {
2524 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002525 nfs4_put_lock_state(prev);
2526 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002527 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002528 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002529 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002530 }
2531 spin_unlock(&state->state_lock);
2532 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002533out:
2534 return ret;
2535}
2536
2537/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002538 * nfs41_check_open_stateid - possibly free an open stateid
2539 *
2540 * @state: NFSv4 state for an inode
2541 *
2542 * Returns NFS_OK if recovery for this stateid is now finished.
2543 * Otherwise a negative NFS4ERR value is returned.
2544 */
2545static int nfs41_check_open_stateid(struct nfs4_state *state)
2546{
2547 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002548 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002549 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002550 int status;
2551
Trond Myklebustb134fc42016-09-22 13:39:16 -04002552 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002553 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2554 if (nfs4_have_delegation(state->inode, state->state))
2555 return NFS_OK;
2556 return -NFS4ERR_OPENMODE;
2557 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002558 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002559 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002560 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002561 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002562 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002563 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2564 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2565 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002566 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002567 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002568 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002569 if (status != NFS_OK)
2570 return status;
2571 if (nfs_open_stateid_recover_openmode(state))
2572 return -NFS4ERR_OPENMODE;
2573 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002574}
2575
2576static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2577{
Chuck Levereb64cf92012-07-11 16:30:05 -04002578 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002579
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002580 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002581 status = nfs41_check_expired_locks(state);
2582 if (status != NFS_OK)
2583 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002584 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002585 if (status != NFS_OK)
2586 status = nfs4_open_expired(sp, state);
2587 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002588}
2589#endif
2590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002592 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2593 * fields corresponding to attributes that were used to store the verifier.
2594 * Make sure we clobber those fields in the later setattr call
2595 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002596static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2597 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002598{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002599 const u32 *attrset = opendata->o_res.attrset;
2600
2601 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002602 !(sattr->ia_valid & ATTR_ATIME_SET))
2603 sattr->ia_valid |= ATTR_ATIME;
2604
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002605 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002606 !(sattr->ia_valid & ATTR_MTIME_SET))
2607 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002608
2609 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002610 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2611 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002612 sattr->ia_valid &= ~ATTR_MODE;
2613
2614 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2615 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002616}
2617
Trond Myklebustc21443c2013-02-07 14:26:21 -05002618static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2619 fmode_t fmode,
2620 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002621 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002622{
2623 struct nfs4_state_owner *sp = opendata->owner;
2624 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002625 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002626 struct nfs4_state *state;
2627 unsigned int seq;
2628 int ret;
2629
2630 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2631
2632 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002633 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002634 goto out;
2635
2636 state = nfs4_opendata_to_nfs4_state(opendata);
2637 ret = PTR_ERR(state);
2638 if (IS_ERR(state))
2639 goto out;
2640 if (server->caps & NFS_CAP_POSIX_LOCK)
2641 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002642 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2643 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002644
Trond Myklebust275bb302013-05-29 13:11:28 -04002645 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002646 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002647 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002648 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002649 alias = d_exact_alias(dentry, state->inode);
2650 if (!alias)
2651 alias = d_splice_alias(igrab(state->inode), dentry);
2652 /* d_splice_alias() can't fail here - it's a non-directory */
2653 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002654 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002655 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002656 }
2657 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002658 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002659 }
2660
Trond Myklebustc21443c2013-02-07 14:26:21 -05002661 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2662 if (ret != 0)
2663 goto out;
2664
Trond Myklebust3efb9722013-05-29 13:17:04 -04002665 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002666 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002667 nfs_inode_attach_open_context(ctx);
2668 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2669 nfs4_schedule_stateid_recovery(server, state);
2670 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002671out:
2672 return ret;
2673}
2674
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002675/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002676 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 */
Andy Adamson82be4172012-05-23 05:02:35 -04002678static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002679 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002680 int flags,
2681 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002682 struct nfs4_label *label,
2683 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
2685 struct nfs4_state_owner *sp;
2686 struct nfs4_state *state = NULL;
2687 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002688 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002689 struct dentry *dentry = ctx->dentry;
2690 struct rpc_cred *cred = ctx->cred;
2691 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2692 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002693 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002694 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002695 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
2697 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002699 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2700 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2702 goto out_err;
2703 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002704 status = nfs4_recover_expired_lease(server);
2705 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002706 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002707 if (d_really_is_positive(dentry))
2708 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002709 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002710 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002711 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002712 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002713 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002714 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002715 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
David Quigley14c43f72013-05-22 12:50:43 -04002717 if (label) {
2718 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2719 if (IS_ERR(olabel)) {
2720 status = PTR_ERR(olabel);
2721 goto err_opendata_put;
2722 }
2723 }
2724
Trond Myklebuste911b812014-03-26 13:24:37 -07002725 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2726 if (!opendata->f_attr.mdsthreshold) {
2727 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2728 if (!opendata->f_attr.mdsthreshold)
2729 goto err_free_label;
2730 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002731 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002732 }
David Howells2b0143b2015-03-17 22:25:59 +00002733 if (d_really_is_positive(dentry))
2734 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002735
Trond Myklebust3efb9722013-05-29 13:17:04 -04002736 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002737 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002738 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002739 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002740
NeilBrownefcbc042015-07-30 13:00:56 +10002741 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002742 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002743 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002744 /*
2745 * send create attributes which was not set by open
2746 * with an extra setattr.
2747 */
2748 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2749 nfs_fattr_init(opendata->o_res.f_attr);
2750 status = nfs4_do_setattr(state->inode, cred,
2751 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002752 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002753 if (status == 0) {
2754 nfs_setattr_update_inode(state->inode, sattr,
2755 opendata->o_res.f_attr);
2756 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2757 }
David Quigley1775fd32013-05-22 12:50:42 -04002758 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002759 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002760 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002761 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002762
Trond Myklebuste911b812014-03-26 13:24:37 -07002763 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002764 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002765 opendata->f_attr.mdsthreshold = NULL;
2766 }
Andy Adamson82be4172012-05-23 05:02:35 -04002767
David Quigley14c43f72013-05-22 12:50:43 -04002768 nfs4_label_free(olabel);
2769
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002770 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002773err_free_label:
2774 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002775err_opendata_put:
2776 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002777err_put_state_owner:
2778 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return status;
2781}
2782
2783
Andy Adamson82be4172012-05-23 05:02:35 -04002784static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002785 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002786 int flags,
2787 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002788 struct nfs4_label *label,
2789 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002791 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 struct nfs4_exception exception = { };
2793 struct nfs4_state *res;
2794 int status;
2795
2796 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002797 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002798 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002799 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 if (status == 0)
2801 break;
2802 /* NOTE: BAD_SEQID means the server and client disagree about the
2803 * book-keeping w.r.t. state-changing operations
2804 * (OPEN/CLOSE/LOCK/LOCKU...)
2805 * It is actually a sign of a bug on the client or on the server.
2806 *
2807 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002808 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 * have unhashed the old state_owner for us, and that we can
2810 * therefore safely retry using a new one. We should still warn
2811 * the user though...
2812 */
2813 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002814 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002815 " returned a bad sequence-id error!\n",
2816 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 exception.retry = 1;
2818 continue;
2819 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002820 /*
2821 * BAD_STATEID on OPEN means that the server cancelled our
2822 * state before it received the OPEN_CONFIRM.
2823 * Recover by retrying the request as per the discussion
2824 * on Page 181 of RFC3530.
2825 */
2826 if (status == -NFS4ERR_BAD_STATEID) {
2827 exception.retry = 1;
2828 continue;
2829 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002830 if (status == -EAGAIN) {
2831 /* We must have found a delegation */
2832 exception.retry = 1;
2833 continue;
2834 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002835 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2836 continue;
2837 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 status, &exception));
2839 } while (exception.retry);
2840 return res;
2841}
2842
Trond Myklebust8487c472016-06-26 08:44:35 -04002843static int _nfs4_do_setattr(struct inode *inode,
2844 struct nfs_setattrargs *arg,
2845 struct nfs_setattrres *res,
2846 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002847 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002848{
2849 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002850 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04002851 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2852 .rpc_argp = arg,
2853 .rpc_resp = res,
2854 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002855 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002856 struct rpc_cred *delegation_cred = NULL;
2857 unsigned long timestamp = jiffies;
2858 fmode_t fmode;
2859 bool truncate;
2860 int status;
2861
2862 nfs_fattr_init(res->fattr);
2863
2864 /* Servers should only apply open mode checks for file size changes */
2865 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2866 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2867
2868 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2869 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002870 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002871 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002872 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002873 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002874 l_ctx = nfs_get_lock_context(ctx);
2875 if (IS_ERR(l_ctx))
2876 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002877 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2878 &arg->stateid, &delegation_cred);
2879 nfs_put_lock_context(l_ctx);
2880 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002881 return -EBADF;
2882 } else
2883 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2884 if (delegation_cred)
2885 msg.rpc_cred = delegation_cred;
2886
2887 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2888
2889 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002890 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002891 renew_lease(server, timestamp);
2892 trace_nfs4_setattr(inode, &arg->stateid, status);
2893 return status;
2894}
2895
2896static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2897 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002898 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002899 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002901 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002902 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002903 struct nfs_setattrargs arg = {
2904 .fh = NFS_FH(inode),
2905 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 .server = server,
2907 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002908 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002909 };
2910 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002912 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002914 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002915 struct nfs4_exception exception = {
2916 .state = state,
2917 .inode = inode,
2918 .stateid = &arg.stateid,
2919 };
2920 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
David Quigleyaa9c2662013-05-22 12:50:44 -04002922 arg.bitmask = nfs4_bitmask(server, ilabel);
2923 if (ilabel)
2924 arg.bitmask = nfs4_bitmask(server, olabel);
2925
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 do {
NeilBrown29b59f92016-10-13 15:26:47 +11002927 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04002928 switch (err) {
2929 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002930 if (!(sattr->ia_valid & ATTR_SIZE)) {
2931 pr_warn_once("NFSv4: server %s is incorrectly "
2932 "applying open mode checks to "
2933 "a SETATTR that is not "
2934 "changing file size.\n",
2935 server->nfs_client->cl_hostname);
2936 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002937 if (state && !(state->state & FMODE_WRITE)) {
2938 err = -EBADF;
2939 if (sattr->ia_valid & ATTR_OPEN)
2940 err = -EACCES;
2941 goto out;
2942 }
2943 }
2944 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002946out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 return err;
2948}
2949
Peng Tao500d7012015-09-22 11:35:22 +08002950static bool
2951nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2952{
2953 if (inode == NULL || !nfs_have_layout(inode))
2954 return false;
2955
2956 return pnfs_wait_on_layoutreturn(inode, task);
2957}
2958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959struct nfs4_closedata {
2960 struct inode *inode;
2961 struct nfs4_state *state;
2962 struct nfs_closeargs arg;
2963 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05002964 struct {
2965 struct nfs4_layoutreturn_args arg;
2966 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04002967 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05002968 u32 roc_barrier;
2969 bool roc;
2970 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002971 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002972 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973};
2974
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002975static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002976{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002977 struct nfs4_closedata *calldata = data;
2978 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002979 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002980
Trond Myklebustcf805162016-11-15 14:56:07 -05002981 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05002982 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
2983 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07002984 nfs4_put_open_state(calldata->state);
2985 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002986 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002987 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002988 kfree(calldata);
2989}
2990
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002991static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002993 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002996 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
Chuck Levera3ca5652012-03-01 17:00:40 -05002998 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002999 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3000 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003001 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003002
3003 /* Handle Layoutreturn errors */
3004 if (calldata->arg.lr_args && task->tk_status != 0) {
3005 switch (calldata->res.lr_ret) {
3006 default:
3007 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3008 break;
3009 case 0:
3010 calldata->arg.lr_args = NULL;
3011 calldata->res.lr_res = NULL;
3012 break;
3013 case -NFS4ERR_ADMIN_REVOKED:
3014 case -NFS4ERR_DELEG_REVOKED:
3015 case -NFS4ERR_EXPIRED:
3016 case -NFS4ERR_BAD_STATEID:
3017 case -NFS4ERR_OLD_STATEID:
3018 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3019 case -NFS4ERR_WRONG_CRED:
3020 calldata->arg.lr_args = NULL;
3021 calldata->res.lr_res = NULL;
3022 calldata->res.lr_ret = 0;
3023 rpc_restart_call_prepare(task);
3024 return;
3025 }
3026 }
3027
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003028 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 * the state_owner. we keep this around to process errors
3030 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 switch (task->tk_status) {
3032 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003033 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003034 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003035 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003036 case -NFS4ERR_ACCESS:
3037 if (calldata->arg.bitmask != NULL) {
3038 calldata->arg.bitmask = NULL;
3039 calldata->res.fattr = NULL;
3040 task->tk_status = 0;
3041 rpc_restart_call_prepare(task);
3042 goto out_release;
3043
3044 }
3045 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003046 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003048 case -NFS4ERR_EXPIRED:
3049 nfs4_free_revoked_stateid(server,
3050 &calldata->arg.stateid,
3051 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003052 case -NFS4ERR_OLD_STATEID:
3053 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003054 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003055 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003056 rpc_restart_call_prepare(task);
3057 goto out_release;
3058 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003059 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003060 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003062 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003063 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003064 goto out_release;
3065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003067 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3068 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003069out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003070 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003071 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003072 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073}
3074
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003075static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003077 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003078 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003079 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003080 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003081 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003082
Chuck Levera3ca5652012-03-01 17:00:40 -05003083 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003084 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003085 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003086
Trond Myklebust88069f72009-12-08 08:33:16 -05003087 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003088 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003089 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3090 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3091 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003092 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003093 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003094 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003095 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003096 if (state->n_rdonly == 0)
3097 call_close |= is_rdonly;
3098 else if (is_rdonly)
3099 calldata->arg.fmode |= FMODE_READ;
3100 if (state->n_wronly == 0)
3101 call_close |= is_wronly;
3102 else if (is_wronly)
3103 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003104 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3105 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003106 } else if (is_rdwr)
3107 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3108
Trond Myklebust5cc78612016-11-14 11:19:56 -05003109 if (!nfs4_valid_open_stateid(state) ||
3110 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003111 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003112 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003113
3114 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003115 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003116 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003117 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003118
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003119 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003120 nfs_release_seqid(calldata->arg.seqid);
3121 goto out_wait;
3122 }
3123
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003124 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003125 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003126
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003127 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003128 /* Close-to-open cache consistency revalidation */
3129 if (!nfs4_have_delegation(inode, FMODE_READ))
3130 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3131 else
3132 calldata->arg.bitmask = NULL;
3133 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003134
Trond Myklebust6ae37332015-01-30 14:21:14 -05003135 calldata->arg.share_access =
3136 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3137 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003138
Trond Myklebustd8d84982016-12-19 12:14:44 -05003139 if (calldata->res.fattr == NULL)
3140 calldata->arg.bitmask = NULL;
3141 else if (calldata->arg.bitmask == NULL)
3142 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003143 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003144 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003145 &calldata->arg.seq_args,
3146 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003147 task) != 0)
3148 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003149 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003150 return;
3151out_no_action:
3152 task->tk_action = NULL;
3153out_wait:
3154 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155}
3156
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003157static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003158 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003159 .rpc_call_done = nfs4_close_done,
3160 .rpc_release = nfs4_free_closedata,
3161};
3162
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163/*
3164 * It is possible for data to be read/written from a mem-mapped file
3165 * after the sys_close call (which hits the vfs layer as a flush).
3166 * This means that we can't safely call nfsv4 close on a file until
3167 * the inode is cleared. This in turn means that we are not good
3168 * NFSv4 citizens - we do not indicate to the server to update the file's
3169 * share state even when we are done with one of the three share
3170 * stateid's in the inode.
3171 *
3172 * NOTE: Caller must be holding the sp->so_owner semaphore!
3173 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003174int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003176 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003177 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003179 struct nfs4_state_owner *sp = state->owner;
3180 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003181 struct rpc_message msg = {
3182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3183 .rpc_cred = state->owner->so_cred,
3184 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003185 struct rpc_task_setup task_setup_data = {
3186 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003187 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003188 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003189 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003190 .flags = RPC_TASK_ASYNC,
3191 };
Trond Myklebust95121352005-10-18 14:20:12 -07003192 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003194 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3195 &task_setup_data.rpc_client, &msg);
3196
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003197 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003199 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003200 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003201 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003203 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003205 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3206 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003207 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003208 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003209 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003210 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003211 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003212 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003213 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003214 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003215 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003216 calldata->lr.roc = pnfs_roc(state->inode,
3217 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3218 if (calldata->lr.roc) {
3219 calldata->arg.lr_args = &calldata->lr.arg;
3220 calldata->res.lr_res = &calldata->lr.res;
3221 }
Al Viro643168c2011-06-22 18:20:23 -04003222 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003223
Trond Myklebust1174dd12010-12-21 10:52:24 -05003224 msg.rpc_argp = &calldata->arg;
3225 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003226 task_setup_data.callback_data = calldata;
3227 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003228 if (IS_ERR(task))
3229 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003230 status = 0;
3231 if (wait)
3232 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003233 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003234 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003235out_free_calldata:
3236 kfree(calldata);
3237out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003238 nfs4_put_open_state(state);
3239 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003240 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241}
3242
Trond Myklebust2b484292010-09-17 10:56:51 -04003243static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003244nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3245 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003248 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3249
3250 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
Trond Myklebust565277f2007-10-15 18:17:53 -04003252 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003253 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003254
3255 nfs4_label_release_security(label);
3256
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003257 if (IS_ERR(state))
3258 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003259 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260}
3261
Trond Myklebust1185a552009-12-03 15:54:02 -05003262static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003263{
3264 if (ctx->state == NULL)
3265 return;
3266 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003267 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003268 else
Al Viro643168c2011-06-22 18:20:23 -04003269 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003270}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Trond Myklebustb944dba2013-11-04 15:20:20 -05003272#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3273#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003274#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3277{
Kinglong Mee8c612822015-08-26 21:12:58 +08003278 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003279 struct nfs4_server_caps_arg args = {
3280 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003281 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003282 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 struct nfs4_server_caps_res res = {};
3284 struct rpc_message msg = {
3285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003286 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 .rpc_resp = &res,
3288 };
3289 int status;
3290
Kinglong Mee8c612822015-08-26 21:12:58 +08003291 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3292 FATTR4_WORD0_FH_EXPIRE_TYPE |
3293 FATTR4_WORD0_LINK_SUPPORT |
3294 FATTR4_WORD0_SYMLINK_SUPPORT |
3295 FATTR4_WORD0_ACLSUPPORT;
3296 if (minorversion)
3297 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3298
Bryan Schumaker7c513052011-03-24 17:12:24 +00003299 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003301 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003302 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003303 case 0:
3304 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3305 res.attr_bitmask[2] = 0;
3306 break;
3307 case 1:
3308 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3309 break;
3310 case 2:
3311 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003314 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3315 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3316 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3317 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003318 NFS_CAP_CTIME|NFS_CAP_MTIME|
3319 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003320 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3321 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 server->caps |= NFS_CAP_ACLS;
3323 if (res.has_links != 0)
3324 server->caps |= NFS_CAP_HARDLINKS;
3325 if (res.has_symlinks != 0)
3326 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003327 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3328 server->caps |= NFS_CAP_FILEID;
3329 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3330 server->caps |= NFS_CAP_MODE;
3331 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3332 server->caps |= NFS_CAP_NLINK;
3333 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3334 server->caps |= NFS_CAP_OWNER;
3335 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3336 server->caps |= NFS_CAP_OWNER_GROUP;
3337 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3338 server->caps |= NFS_CAP_ATIME;
3339 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3340 server->caps |= NFS_CAP_CTIME;
3341 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3342 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003343#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3344 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3345 server->caps |= NFS_CAP_SECURITY_LABEL;
3346#endif
3347 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3348 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003349 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003350
Trond Myklebusta65318b2009-03-11 14:10:28 -04003351 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3352 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3353 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003354 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003355 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3356 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003358 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003360
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 return status;
3362}
3363
Trond Myklebust55a97592006-06-09 09:34:19 -04003364int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365{
3366 struct nfs4_exception exception = { };
3367 int err;
3368 do {
3369 err = nfs4_handle_exception(server,
3370 _nfs4_server_capabilities(server, fhandle),
3371 &exception);
3372 } while (exception.retry);
3373 return err;
3374}
3375
3376static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3377 struct nfs_fsinfo *info)
3378{
David Quigleyaa9c2662013-05-22 12:50:44 -04003379 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003381 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 };
3383 struct nfs4_lookup_res res = {
3384 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003385 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 .fh = fhandle,
3387 };
3388 struct rpc_message msg = {
3389 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3390 .rpc_argp = &args,
3391 .rpc_resp = &res,
3392 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003393
David Quigleyaa9c2662013-05-22 12:50:44 -04003394 bitmask[0] = nfs4_fattr_bitmap[0];
3395 bitmask[1] = nfs4_fattr_bitmap[1];
3396 /*
3397 * Process the label in the upcoming getfattr
3398 */
3399 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3400
Trond Myklebust0e574af2005-10-27 22:12:38 -04003401 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003402 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403}
3404
3405static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3406 struct nfs_fsinfo *info)
3407{
3408 struct nfs4_exception exception = { };
3409 int err;
3410 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003411 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003412 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003413 switch (err) {
3414 case 0:
3415 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003416 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003417 default:
3418 err = nfs4_handle_exception(server, err, &exception);
3419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003421out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 return err;
3423}
3424
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003425static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3426 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3427{
Trond Myklebustc2190662013-08-26 19:23:04 -04003428 struct rpc_auth_create_args auth_args = {
3429 .pseudoflavor = flavor,
3430 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003431 struct rpc_auth *auth;
3432 int ret;
3433
Trond Myklebustc2190662013-08-26 19:23:04 -04003434 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003435 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003436 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003437 goto out;
3438 }
3439 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003440out:
3441 return ret;
3442}
3443
Chuck Lever9a744ba2013-03-16 15:56:02 -04003444/*
3445 * Retry pseudoroot lookup with various security flavors. We do this when:
3446 *
3447 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3448 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3449 *
3450 * Returns zero on success, or a negative NFS4ERR value, or a
3451 * negative errno value.
3452 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003453static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003454 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003456 /* Per 3530bis 15.33.5 */
3457 static const rpc_authflavor_t flav_array[] = {
3458 RPC_AUTH_GSS_KRB5P,
3459 RPC_AUTH_GSS_KRB5I,
3460 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003461 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003462 RPC_AUTH_NULL,
3463 };
3464 int status = -EPERM;
3465 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003467 if (server->auth_info.flavor_len > 0) {
3468 /* try each flavor specified by user */
3469 for (i = 0; i < server->auth_info.flavor_len; i++) {
3470 status = nfs4_lookup_root_sec(server, fhandle, info,
3471 server->auth_info.flavors[i]);
3472 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3473 continue;
3474 break;
3475 }
3476 } else {
3477 /* no flavors specified by user, try default list */
3478 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3479 status = nfs4_lookup_root_sec(server, fhandle, info,
3480 flav_array[i]);
3481 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3482 continue;
3483 break;
3484 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003485 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003486
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003487 /*
3488 * -EACCESS could mean that the user doesn't have correct permissions
3489 * to access the mount. It could also mean that we tried to mount
3490 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3491 * existing mount programs don't handle -EACCES very well so it should
3492 * be mapped to -EPERM instead.
3493 */
3494 if (status == -EACCES)
3495 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003496 return status;
3497}
3498
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003499/**
3500 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3501 * @server: initialized nfs_server handle
3502 * @fhandle: we fill in the pseudo-fs root file handle
3503 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003504 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003505 *
3506 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003507 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003508int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003509 struct nfs_fsinfo *info,
3510 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003511{
Andre Przywarac7757072015-04-23 17:17:40 +01003512 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003513
Andre Przywarac7757072015-04-23 17:17:40 +01003514 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003515 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003516
3517 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003518 status = server->nfs_client->cl_mvops->find_root_sec(server,
3519 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003520
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 if (status == 0)
3522 status = nfs4_server_capabilities(server, fhandle);
3523 if (status == 0)
3524 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003525
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003526 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527}
3528
Bryan Schumakerbae36242012-05-10 15:07:31 -04003529static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3530 struct nfs_fsinfo *info)
3531{
3532 int error;
3533 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003534 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003535
3536 error = nfs4_server_capabilities(server, mntfh);
3537 if (error < 0) {
3538 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3539 return error;
3540 }
3541
David Quigley14c43f72013-05-22 12:50:43 -04003542 label = nfs4_label_alloc(server, GFP_KERNEL);
3543 if (IS_ERR(label))
3544 return PTR_ERR(label);
3545
David Quigley1775fd32013-05-22 12:50:42 -04003546 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003547 if (error < 0) {
3548 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003549 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003550 }
3551
3552 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3553 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3554 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3555
David Quigley14c43f72013-05-22 12:50:43 -04003556err_free_label:
3557 nfs4_label_free(label);
3558
Bryan Schumakerbae36242012-05-10 15:07:31 -04003559 return error;
3560}
3561
Manoj Naik6b97fd32006-06-09 09:34:29 -04003562/*
3563 * Get locations and (maybe) other attributes of a referral.
3564 * Note that we'll actually follow the referral later when
3565 * we detect fsid mismatch in inode revalidation
3566 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003567static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3568 const struct qstr *name, struct nfs_fattr *fattr,
3569 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003570{
3571 int status = -ENOMEM;
3572 struct page *page = NULL;
3573 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003574
3575 page = alloc_page(GFP_KERNEL);
3576 if (page == NULL)
3577 goto out;
3578 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3579 if (locations == NULL)
3580 goto out;
3581
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003582 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003583 if (status != 0)
3584 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003585
3586 /*
3587 * If the fsid didn't change, this is a migration event, not a
3588 * referral. Cause us to drop into the exception handler, which
3589 * will kick off migration recovery.
3590 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003591 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003592 dprintk("%s: server did not return a different fsid for"
3593 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003594 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003595 goto out;
3596 }
Andy Adamson533eb462011-06-13 18:25:56 -04003597 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3598 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003599
Andy Adamson533eb462011-06-13 18:25:56 -04003600 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003601 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003602 memset(fhandle, 0, sizeof(struct nfs_fh));
3603out:
3604 if (page)
3605 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003606 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003607 return status;
3608}
3609
David Quigley1775fd32013-05-22 12:50:42 -04003610static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3611 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612{
3613 struct nfs4_getattr_arg args = {
3614 .fh = fhandle,
3615 .bitmask = server->attr_bitmask,
3616 };
3617 struct nfs4_getattr_res res = {
3618 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003619 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 .server = server,
3621 };
3622 struct rpc_message msg = {
3623 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3624 .rpc_argp = &args,
3625 .rpc_resp = &res,
3626 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003627
3628 args.bitmask = nfs4_bitmask(server, label);
3629
Trond Myklebust0e574af2005-10-27 22:12:38 -04003630 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003631 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632}
3633
David Quigley1775fd32013-05-22 12:50:42 -04003634static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3635 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636{
3637 struct nfs4_exception exception = { };
3638 int err;
3639 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003640 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3641 trace_nfs4_getattr(server, fhandle, fattr, err);
3642 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 &exception);
3644 } while (exception.retry);
3645 return err;
3646}
3647
3648/*
3649 * The file is not closed if it is opened due to the a request to change
3650 * the size of the file. The open call will not be needed once the
3651 * VFS layer lookup-intents are implemented.
3652 *
3653 * Close is called when the inode is destroyed.
3654 * If we haven't opened the file for O_WRONLY, we
3655 * need to in the size_change case to obtain a stateid.
3656 *
3657 * Got race?
3658 * Because OPEN is always done by name in nfsv4, it is
3659 * possible that we opened a different file by the same
3660 * name. We can recognize this race condition, but we
3661 * can't do anything about it besides returning an error.
3662 *
3663 * This will be fixed with VFS changes (lookup-intent).
3664 */
3665static int
3666nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3667 struct iattr *sattr)
3668{
David Howells2b0143b2015-03-17 22:25:59 +00003669 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003670 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003671 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003672 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 int status;
3674
Peng Tao88ac8152014-09-12 11:04:10 +08003675 if (pnfs_ld_layoutret_on_setattr(inode) &&
3676 sattr->ia_valid & ATTR_SIZE &&
3677 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003678 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003679
Trond Myklebust0e574af2005-10-27 22:12:38 -04003680 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
Andy Adamson26699402012-05-30 16:12:24 -04003682 /* Deal with open(O_TRUNC) */
3683 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003684 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003685
3686 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003687 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003688 return 0;
3689
Trond Myklebustd5308382005-11-04 15:33:38 -05003690 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003691 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003692
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003693 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003694 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003695 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003696 }
3697
David Quigley14c43f72013-05-22 12:50:43 -04003698 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3699 if (IS_ERR(label))
3700 return PTR_ERR(label);
3701
NeilBrown29b59f92016-10-13 15:26:47 +11003702 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003703 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003704 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003705 nfs_setsecurity(inode, fattr, label);
3706 }
David Quigley14c43f72013-05-22 12:50:43 -04003707 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 return status;
3709}
3710
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003711static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3712 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003713 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003714{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003715 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003716 int status;
3717 struct nfs4_lookup_arg args = {
3718 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003719 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003720 .name = name,
3721 };
3722 struct nfs4_lookup_res res = {
3723 .server = server,
3724 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003725 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003726 .fh = fhandle,
3727 };
3728 struct rpc_message msg = {
3729 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3730 .rpc_argp = &args,
3731 .rpc_resp = &res,
3732 };
3733
David Quigleyaa9c2662013-05-22 12:50:44 -04003734 args.bitmask = nfs4_bitmask(server, label);
3735
David Howells2b3de442006-08-22 20:06:09 -04003736 nfs_fattr_init(fattr);
3737
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003739 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 dprintk("NFS reply lookup: %d\n", status);
3741 return status;
3742}
3743
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003744static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003745{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003746 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003747 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003748 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3749 fattr->nlink = 2;
3750}
3751
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003752static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003753 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003754 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003755{
3756 struct nfs4_exception exception = { };
3757 struct rpc_clnt *client = *clnt;
3758 int err;
3759 do {
David Quigley1775fd32013-05-22 12:50:42 -04003760 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003761 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003762 switch (err) {
3763 case -NFS4ERR_BADNAME:
3764 err = -ENOENT;
3765 goto out;
3766 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003767 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003768 if (err == -NFS4ERR_MOVED)
3769 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003770 goto out;
3771 case -NFS4ERR_WRONGSEC:
3772 err = -EPERM;
3773 if (client != *clnt)
3774 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003775 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003776 if (IS_ERR(client))
3777 return PTR_ERR(client);
3778
3779 exception.retry = 1;
3780 break;
3781 default:
3782 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3783 }
3784 } while (exception.retry);
3785
3786out:
3787 if (err == 0)
3788 *clnt = client;
3789 else if (client != *clnt)
3790 rpc_shutdown_client(client);
3791
3792 return err;
3793}
3794
Al Virobeffb8f2016-07-20 16:34:42 -04003795static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003796 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3797 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003799 int status;
3800 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003801
David Quigley1775fd32013-05-22 12:50:42 -04003802 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003803 if (client != NFS_CLIENT(dir)) {
3804 rpc_shutdown_client(client);
3805 nfs_fixup_secinfo_attributes(fattr);
3806 }
3807 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808}
3809
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003810struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003811nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003812 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3813{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003814 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003815 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003816
David Quigley1775fd32013-05-22 12:50:42 -04003817 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003818 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003819 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003820 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003821}
3822
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3824{
Trond Myklebust76b32992007-08-10 17:45:11 -04003825 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 struct nfs4_accessargs args = {
3827 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003828 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003830 struct nfs4_accessres res = {
3831 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003832 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 struct rpc_message msg = {
3834 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3835 .rpc_argp = &args,
3836 .rpc_resp = &res,
3837 .rpc_cred = entry->cred,
3838 };
3839 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003840 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841
3842 /*
3843 * Determine which access bits we want to ask for...
3844 */
3845 if (mode & MAY_READ)
3846 args.access |= NFS4_ACCESS_READ;
3847 if (S_ISDIR(inode->i_mode)) {
3848 if (mode & MAY_WRITE)
3849 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3850 if (mode & MAY_EXEC)
3851 args.access |= NFS4_ACCESS_LOOKUP;
3852 } else {
3853 if (mode & MAY_WRITE)
3854 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3855 if (mode & MAY_EXEC)
3856 args.access |= NFS4_ACCESS_EXECUTE;
3857 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003858
3859 res.fattr = nfs_alloc_fattr();
3860 if (res.fattr == NULL)
3861 return -ENOMEM;
3862
Bryan Schumaker7c513052011-03-24 17:12:24 +00003863 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003865 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003866 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003868 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 return status;
3870}
3871
3872static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3873{
3874 struct nfs4_exception exception = { };
3875 int err;
3876 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003877 err = _nfs4_proc_access(inode, entry);
3878 trace_nfs4_access(inode, err);
3879 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 &exception);
3881 } while (exception.retry);
3882 return err;
3883}
3884
3885/*
3886 * TODO: For the time being, we don't try to get any attributes
3887 * along with any of the zero-copy operations READ, READDIR,
3888 * READLINK, WRITE.
3889 *
3890 * In the case of the first three, we want to put the GETATTR
3891 * after the read-type operation -- this is because it is hard
3892 * to predict the length of a GETATTR response in v4, and thus
3893 * align the READ data correctly. This means that the GETATTR
3894 * may end up partially falling into the page cache, and we should
3895 * shift it into the 'tail' of the xdr_buf before processing.
3896 * To do this efficiently, we need to know the total length
3897 * of data received, which doesn't seem to be available outside
3898 * of the RPC layer.
3899 *
3900 * In the case of WRITE, we also want to put the GETATTR after
3901 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003902 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 *
3904 * Both of these changes to the XDR layer would in fact be quite
3905 * minor, but I decided to leave them for a subsequent patch.
3906 */
3907static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3908 unsigned int pgbase, unsigned int pglen)
3909{
3910 struct nfs4_readlink args = {
3911 .fh = NFS_FH(inode),
3912 .pgbase = pgbase,
3913 .pglen = pglen,
3914 .pages = &page,
3915 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003916 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 struct rpc_message msg = {
3918 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3919 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003920 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 };
3922
Bryan Schumaker7c513052011-03-24 17:12:24 +00003923 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 -07003924}
3925
3926static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3927 unsigned int pgbase, unsigned int pglen)
3928{
3929 struct nfs4_exception exception = { };
3930 int err;
3931 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003932 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3933 trace_nfs4_readlink(inode, err);
3934 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 &exception);
3936 } while (exception.retry);
3937 return err;
3938}
3939
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003941 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943static int
3944nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003945 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003947 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04003948 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003949 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 int status = 0;
3952
NeilBrown532d4de2016-10-13 15:26:47 +11003953 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003954 if (IS_ERR(ctx))
3955 return PTR_ERR(ctx);
3956
David Quigleyaa9c2662013-05-22 12:50:44 -04003957 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3958
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003959 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
3960 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003961 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 if (IS_ERR(state)) {
3963 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003964 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003967 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003968 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 return status;
3970}
3971
Al Virobeffb8f2016-07-20 16:34:42 -04003972static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973{
Trond Myklebust16e42952005-10-27 22:12:44 -04003974 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003975 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003977 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003979 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003980 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003981 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003983 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3984 .rpc_argp = &args,
3985 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05003987 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04003988 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003989
Bryan Schumaker7c513052011-03-24 17:12:24 +00003990 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003991 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05003992 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 return status;
3994}
3995
Al Virobeffb8f2016-07-20 16:34:42 -04003996static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997{
3998 struct nfs4_exception exception = { };
3999 int err;
4000 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004001 err = _nfs4_proc_remove(dir, name);
4002 trace_nfs4_remove(dir, name, err);
4003 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 &exception);
4005 } while (exception.retry);
4006 return err;
4007}
4008
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004009static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004011 struct nfs_server *server = NFS_SERVER(dir);
4012 struct nfs_removeargs *args = msg->rpc_argp;
4013 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004015 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004017 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004018
4019 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020}
4021
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004022static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4023{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004024 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004025 &data->args.seq_args,
4026 &data->res.seq_res,
4027 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028}
4029
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004030static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004032 struct nfs_unlinkdata *data = task->tk_calldata;
4033 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004034
Trond Myklebust14516c32010-07-31 14:29:06 -04004035 if (!nfs4_sequence_done(task, &res->seq_res))
4036 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004037 if (nfs4_async_handle_error(task, res->server, NULL,
4038 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004039 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004040 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004041 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004042 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043}
4044
Jeff Laytond3d41522010-09-17 17:31:57 -04004045static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4046{
4047 struct nfs_server *server = NFS_SERVER(dir);
4048 struct nfs_renameargs *arg = msg->rpc_argp;
4049 struct nfs_renameres *res = msg->rpc_resp;
4050
4051 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004052 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004053 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004054}
4055
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004056static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4057{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004058 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004059 &data->args.seq_args,
4060 &data->res.seq_res,
4061 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004062}
4063
4064static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4065 struct inode *new_dir)
4066{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004067 struct nfs_renamedata *data = task->tk_calldata;
4068 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004069
4070 if (!nfs4_sequence_done(task, &res->seq_res))
4071 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004072 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004073 return 0;
4074
Trond Myklebustc733c492017-01-11 12:32:26 -05004075 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004076 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004077 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004078 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004079 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004080 return 1;
4081}
4082
Al Virobeffb8f2016-07-20 16:34:42 -04004083static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004085 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 struct nfs4_link_arg arg = {
4087 .fh = NFS_FH(inode),
4088 .dir_fh = NFS_FH(dir),
4089 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004090 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004092 struct nfs4_link_res res = {
4093 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004094 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004095 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 struct rpc_message msg = {
4097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4098 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004099 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004101 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102
Trond Myklebust136f2622010-04-16 16:22:49 -04004103 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004104 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004105 goto out;
4106
David Quigley14c43f72013-05-22 12:50:43 -04004107 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4108 if (IS_ERR(res.label)) {
4109 status = PTR_ERR(res.label);
4110 goto out;
4111 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004112 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004113
Bryan Schumaker7c513052011-03-24 17:12:24 +00004114 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004115 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004116 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004117 status = nfs_post_op_update_inode(inode, res.fattr);
4118 if (!status)
4119 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004120 }
David Quigley14c43f72013-05-22 12:50:43 -04004121
4122
4123 nfs4_label_free(res.label);
4124
Trond Myklebust136f2622010-04-16 16:22:49 -04004125out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004126 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 return status;
4128}
4129
Al Virobeffb8f2016-07-20 16:34:42 -04004130static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131{
4132 struct nfs4_exception exception = { };
4133 int err;
4134 do {
4135 err = nfs4_handle_exception(NFS_SERVER(inode),
4136 _nfs4_proc_link(inode, dir, name),
4137 &exception);
4138 } while (exception.retry);
4139 return err;
4140}
4141
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004142struct nfs4_createdata {
4143 struct rpc_message msg;
4144 struct nfs4_create_arg arg;
4145 struct nfs4_create_res res;
4146 struct nfs_fh fh;
4147 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004148 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004149};
4150
4151static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004152 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004153{
4154 struct nfs4_createdata *data;
4155
4156 data = kzalloc(sizeof(*data), GFP_KERNEL);
4157 if (data != NULL) {
4158 struct nfs_server *server = NFS_SERVER(dir);
4159
David Quigley14c43f72013-05-22 12:50:43 -04004160 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4161 if (IS_ERR(data->label))
4162 goto out_free;
4163
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004164 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4165 data->msg.rpc_argp = &data->arg;
4166 data->msg.rpc_resp = &data->res;
4167 data->arg.dir_fh = NFS_FH(dir);
4168 data->arg.server = server;
4169 data->arg.name = name;
4170 data->arg.attrs = sattr;
4171 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004172 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004173 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004174 data->res.server = server;
4175 data->res.fh = &data->fh;
4176 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004177 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004178 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004179 }
4180 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004181out_free:
4182 kfree(data);
4183 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004184}
4185
4186static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4187{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004188 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004189 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004190 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004191 update_changeattr(dir, &data->res.dir_cinfo,
4192 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004193 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004194 }
4195 return status;
4196}
4197
4198static void nfs4_free_createdata(struct nfs4_createdata *data)
4199{
David Quigley14c43f72013-05-22 12:50:43 -04004200 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004201 kfree(data);
4202}
4203
Chuck Lever4f390c12006-08-22 20:06:22 -04004204static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004205 struct page *page, unsigned int len, struct iattr *sattr,
4206 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004208 struct nfs4_createdata *data;
4209 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
Chuck Lever94a6d752006-08-22 20:06:23 -04004211 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004212 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004213
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004214 status = -ENOMEM;
4215 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4216 if (data == NULL)
4217 goto out;
4218
4219 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4220 data->arg.u.symlink.pages = &page;
4221 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004222 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004224 status = nfs4_do_create(dir, dentry, data);
4225
4226 nfs4_free_createdata(data);
4227out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 return status;
4229}
4230
Chuck Lever4f390c12006-08-22 20:06:22 -04004231static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004232 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233{
4234 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004235 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004237
4238 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4239
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004241 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4242 trace_nfs4_symlink(dir, &dentry->d_name, err);
4243 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 &exception);
4245 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004246
4247 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 return err;
4249}
4250
4251static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004252 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004254 struct nfs4_createdata *data;
4255 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004257 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4258 if (data == NULL)
4259 goto out;
4260
David Quigley1775fd32013-05-22 12:50:42 -04004261 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004262 status = nfs4_do_create(dir, dentry, data);
4263
4264 nfs4_free_createdata(data);
4265out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 return status;
4267}
4268
4269static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4270 struct iattr *sattr)
4271{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004272 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004274 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004276
David Quigleyaa9c2662013-05-22 12:50:44 -04004277 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4278
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004279 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4280 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004282 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4283 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4284 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 &exception);
4286 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004287 nfs4_label_release_security(label);
4288
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 return err;
4290}
4291
4292static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004293 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294{
David Howells2b0143b2015-03-17 22:25:59 +00004295 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 struct nfs4_readdir_arg args = {
4297 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004298 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 .pgbase = 0,
4300 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004301 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004302 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 };
4304 struct nfs4_readdir_res res;
4305 struct rpc_message msg = {
4306 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4307 .rpc_argp = &args,
4308 .rpc_resp = &res,
4309 .rpc_cred = cred,
4310 };
4311 int status;
4312
Al Viro6de14722013-09-16 10:53:17 -04004313 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4314 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004315 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004316 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004318 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 -05004319 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004320 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004321 status += args.pgbase;
4322 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004323
4324 nfs_invalidate_atime(dir);
4325
Harvey Harrison3110ff82008-05-02 13:42:44 -07004326 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 return status;
4328}
4329
4330static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004331 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332{
4333 struct nfs4_exception exception = { };
4334 int err;
4335 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004336 err = _nfs4_proc_readdir(dentry, cred, cookie,
4337 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004338 trace_nfs4_readdir(d_inode(dentry), err);
4339 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 &exception);
4341 } while (exception.retry);
4342 return err;
4343}
4344
4345static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004346 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004348 struct nfs4_createdata *data;
4349 int mode = sattr->ia_mode;
4350 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004352 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4353 if (data == NULL)
4354 goto out;
4355
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004357 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004359 data->arg.ftype = NF4BLK;
4360 data->arg.u.device.specdata1 = MAJOR(rdev);
4361 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 }
4363 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004364 data->arg.ftype = NF4CHR;
4365 data->arg.u.device.specdata1 = MAJOR(rdev);
4366 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004367 } else if (!S_ISSOCK(mode)) {
4368 status = -EINVAL;
4369 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 }
David Quigley1775fd32013-05-22 12:50:42 -04004371
David Quigleyaa9c2662013-05-22 12:50:44 -04004372 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004373 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004374out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004375 nfs4_free_createdata(data);
4376out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 return status;
4378}
4379
4380static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4381 struct iattr *sattr, dev_t rdev)
4382{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004383 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004385 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004387
David Quigleyaa9c2662013-05-22 12:50:44 -04004388 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4389
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004390 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4391 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004393 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4394 trace_nfs4_mknod(dir, &dentry->d_name, err);
4395 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 &exception);
4397 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004398
4399 nfs4_label_release_security(label);
4400
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 return err;
4402}
4403
4404static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4405 struct nfs_fsstat *fsstat)
4406{
4407 struct nfs4_statfs_arg args = {
4408 .fh = fhandle,
4409 .bitmask = server->attr_bitmask,
4410 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004411 struct nfs4_statfs_res res = {
4412 .fsstat = fsstat,
4413 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 struct rpc_message msg = {
4415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4416 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004417 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 };
4419
Trond Myklebust0e574af2005-10-27 22:12:38 -04004420 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004421 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422}
4423
4424static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4425{
4426 struct nfs4_exception exception = { };
4427 int err;
4428 do {
4429 err = nfs4_handle_exception(server,
4430 _nfs4_proc_statfs(server, fhandle, fsstat),
4431 &exception);
4432 } while (exception.retry);
4433 return err;
4434}
4435
4436static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4437 struct nfs_fsinfo *fsinfo)
4438{
4439 struct nfs4_fsinfo_arg args = {
4440 .fh = fhandle,
4441 .bitmask = server->attr_bitmask,
4442 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004443 struct nfs4_fsinfo_res res = {
4444 .fsinfo = fsinfo,
4445 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 struct rpc_message msg = {
4447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4448 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004449 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 };
4451
Bryan Schumaker7c513052011-03-24 17:12:24 +00004452 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453}
4454
4455static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4456{
4457 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004458 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 int err;
4460
4461 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004462 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004463 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004464 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004465 nfs4_set_lease_period(server->nfs_client,
4466 fsinfo->lease_time * HZ,
4467 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004468 break;
4469 }
4470 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 } while (exception.retry);
4472 return err;
4473}
4474
4475static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4476{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004477 int error;
4478
Trond Myklebust0e574af2005-10-27 22:12:38 -04004479 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004480 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004481 if (error == 0) {
4482 /* block layout checks this! */
4483 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004484 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004485 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004486
4487 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488}
4489
4490static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4491 struct nfs_pathconf *pathconf)
4492{
4493 struct nfs4_pathconf_arg args = {
4494 .fh = fhandle,
4495 .bitmask = server->attr_bitmask,
4496 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004497 struct nfs4_pathconf_res res = {
4498 .pathconf = pathconf,
4499 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 struct rpc_message msg = {
4501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4502 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004503 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 };
4505
4506 /* None of the pathconf attributes are mandatory to implement */
4507 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4508 memset(pathconf, 0, sizeof(*pathconf));
4509 return 0;
4510 }
4511
Trond Myklebust0e574af2005-10-27 22:12:38 -04004512 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004513 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514}
4515
4516static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4517 struct nfs_pathconf *pathconf)
4518{
4519 struct nfs4_exception exception = { };
4520 int err;
4521
4522 do {
4523 err = nfs4_handle_exception(server,
4524 _nfs4_proc_pathconf(server, fhandle, pathconf),
4525 &exception);
4526 } while (exception.retry);
4527 return err;
4528}
4529
Trond Myklebust5521abf2013-03-16 20:54:34 -04004530int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004531 const struct nfs_open_context *ctx,
4532 const struct nfs_lock_context *l_ctx,
4533 fmode_t fmode)
4534{
NeilBrown17393472016-10-13 15:26:47 +11004535 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004536}
4537EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4538
Trond Myklebust5521abf2013-03-16 20:54:34 -04004539static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4540 const struct nfs_open_context *ctx,
4541 const struct nfs_lock_context *l_ctx,
4542 fmode_t fmode)
4543{
4544 nfs4_stateid current_stateid;
4545
Trond Myklebuste1253be2014-03-05 08:44:23 -05004546 /* If the current stateid represents a lost lock, then exit */
4547 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4548 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004549 return nfs4_stateid_match(stateid, &current_stateid);
4550}
4551
4552static bool nfs4_error_stateid_expired(int err)
4553{
4554 switch (err) {
4555 case -NFS4ERR_DELEG_REVOKED:
4556 case -NFS4ERR_ADMIN_REVOKED:
4557 case -NFS4ERR_BAD_STATEID:
4558 case -NFS4ERR_STALE_STATEID:
4559 case -NFS4ERR_OLD_STATEID:
4560 case -NFS4ERR_OPENMODE:
4561 case -NFS4ERR_EXPIRED:
4562 return true;
4563 }
4564 return false;
4565}
4566
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004567static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004569 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004571 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004572 if (task->tk_status < 0) {
4573 struct nfs4_exception exception = {
4574 .inode = hdr->inode,
4575 .state = hdr->args.context->state,
4576 .stateid = &hdr->args.stateid,
4577 };
4578 task->tk_status = nfs4_async_handle_exception(task,
4579 server, task->tk_status, &exception);
4580 if (exception.retry) {
4581 rpc_restart_call_prepare(task);
4582 return -EAGAIN;
4583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004585
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004587 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589}
4590
Trond Myklebust5521abf2013-03-16 20:54:34 -04004591static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004592 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004593{
4594
4595 if (!nfs4_error_stateid_expired(task->tk_status) ||
4596 nfs4_stateid_is_current(&args->stateid,
4597 args->context,
4598 args->lock_context,
4599 FMODE_READ))
4600 return false;
4601 rpc_restart_call_prepare(task);
4602 return true;
4603}
4604
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004605static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004606{
4607
4608 dprintk("--> %s\n", __func__);
4609
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004610 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004611 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004612 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004613 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004614 if (task->tk_status > 0)
4615 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004616 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4617 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004618}
4619
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004620static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4621 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004623 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004624 if (!hdr->pgio_done_cb)
4625 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004626 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004627 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628}
4629
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004630static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4631 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004632{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004633 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004634 &hdr->args.seq_args,
4635 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004636 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004637 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004638 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4639 hdr->args.lock_context,
4640 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004641 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004642 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004643 return -EIO;
4644 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645}
4646
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004647static int nfs4_write_done_cb(struct rpc_task *task,
4648 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004650 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004651
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004652 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004653 if (task->tk_status < 0) {
4654 struct nfs4_exception exception = {
4655 .inode = hdr->inode,
4656 .state = hdr->args.context->state,
4657 .stateid = &hdr->args.stateid,
4658 };
4659 task->tk_status = nfs4_async_handle_exception(task,
4660 NFS_SERVER(inode), task->tk_status,
4661 &exception);
4662 if (exception.retry) {
4663 rpc_restart_call_prepare(task);
4664 return -EAGAIN;
4665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004667 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004668 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004669 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004670 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004671 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672}
4673
Trond Myklebust5521abf2013-03-16 20:54:34 -04004674static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004675 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004676{
4677
4678 if (!nfs4_error_stateid_expired(task->tk_status) ||
4679 nfs4_stateid_is_current(&args->stateid,
4680 args->context,
4681 args->lock_context,
4682 FMODE_WRITE))
4683 return false;
4684 rpc_restart_call_prepare(task);
4685 return true;
4686}
4687
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004688static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004689{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004690 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004691 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004692 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004693 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004694 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4695 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004696}
4697
Trond Myklebust5a37f852012-04-28 14:55:16 -04004698static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004699bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004700{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004701 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004702 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004703 return false;
4704 /* Otherwise, request attributes if and only if we don't hold
4705 * a delegation
4706 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004707 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004708}
Fred Isamana69aef12011-03-03 15:13:47 +00004709
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004710static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4711 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004713 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004714
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004715 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4716 hdr->args.bitmask = NULL;
4717 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004718 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004719 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004720
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004721 if (!hdr->pgio_done_cb)
4722 hdr->pgio_done_cb = nfs4_write_done_cb;
4723 hdr->res.server = server;
4724 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004726 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004727 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728}
4729
Fred Isaman0b7c0152012-04-20 14:47:39 -04004730static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4731{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004732 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004733 &data->args.seq_args,
4734 &data->res.seq_res,
4735 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736}
4737
Fred Isaman0b7c0152012-04-20 14:47:39 -04004738static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004741
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004742 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004743 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4744 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004745 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004746 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004748 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749}
4750
Fred Isaman0b7c0152012-04-20 14:47:39 -04004751static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004752{
4753 if (!nfs4_sequence_done(task, &data->res.seq_res))
4754 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004755 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004756}
4757
Fred Isaman0b7c0152012-04-20 14:47:39 -04004758static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004760 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004761
Fred Isaman0b7c0152012-04-20 14:47:39 -04004762 if (data->commit_done_cb == NULL)
4763 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004764 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004765 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004766 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767}
4768
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004769struct nfs4_renewdata {
4770 struct nfs_client *client;
4771 unsigned long timestamp;
4772};
4773
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774/*
4775 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4776 * standalone procedure for queueing an asynchronous RENEW.
4777 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004778static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004779{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004780 struct nfs4_renewdata *data = calldata;
4781 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004782
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004783 if (atomic_read(&clp->cl_count) > 1)
4784 nfs4_schedule_state_renewal(clp);
4785 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004786 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004787}
4788
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004789static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004791 struct nfs4_renewdata *data = calldata;
4792 struct nfs_client *clp = data->client;
4793 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004795 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004796 switch (task->tk_status) {
4797 case 0:
4798 break;
4799 case -NFS4ERR_LEASE_MOVED:
4800 nfs4_schedule_lease_moved_recovery(clp);
4801 break;
4802 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004803 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004804 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4805 return;
4806 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004807 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004808 return;
4809 }
4810 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004812 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813}
4814
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004815static const struct rpc_call_ops nfs4_renew_ops = {
4816 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004817 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004818};
4819
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004820static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821{
4822 struct rpc_message msg = {
4823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4824 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004825 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004827 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004829 if (renew_flags == 0)
4830 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004831 if (!atomic_inc_not_zero(&clp->cl_count))
4832 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004833 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004834 if (data == NULL)
4835 return -ENOMEM;
4836 data->client = clp;
4837 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004838 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004839 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840}
4841
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004842static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843{
4844 struct rpc_message msg = {
4845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4846 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004847 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 };
4849 unsigned long now = jiffies;
4850 int status;
4851
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004852 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 if (status < 0)
4854 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004855 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 return 0;
4857}
4858
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004859static inline int nfs4_server_supports_acls(struct nfs_server *server)
4860{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004861 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004862}
4863
Trond Myklebust21f498c2012-08-24 10:59:25 -04004864/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4865 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004866 * the stack.
4867 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004868#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004869
Neil Hormane9e3d722011-03-04 19:26:03 -05004870static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004871 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004872{
4873 struct page *newpage, **spages;
4874 int rc = 0;
4875 size_t len;
4876 spages = pages;
4877
4878 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004879 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004880 newpage = alloc_page(GFP_KERNEL);
4881
4882 if (newpage == NULL)
4883 goto unwind;
4884 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05004885 buf += len;
4886 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05004887 *pages++ = newpage;
4888 rc++;
4889 } while (buflen != 0);
4890
4891 return rc;
4892
4893unwind:
4894 for(; rc > 0; rc--)
4895 __free_page(spages[rc-1]);
4896 return -ENOMEM;
4897}
4898
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004899struct nfs4_cached_acl {
4900 int cached;
4901 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004902 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004903};
4904
4905static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004906{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004907 struct nfs_inode *nfsi = NFS_I(inode);
4908
4909 spin_lock(&inode->i_lock);
4910 kfree(nfsi->nfs4_acl);
4911 nfsi->nfs4_acl = acl;
4912 spin_unlock(&inode->i_lock);
4913}
4914
4915static void nfs4_zap_acl_attr(struct inode *inode)
4916{
4917 nfs4_set_cached_acl(inode, NULL);
4918}
4919
4920static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4921{
4922 struct nfs_inode *nfsi = NFS_I(inode);
4923 struct nfs4_cached_acl *acl;
4924 int ret = -ENOENT;
4925
4926 spin_lock(&inode->i_lock);
4927 acl = nfsi->nfs4_acl;
4928 if (acl == NULL)
4929 goto out;
4930 if (buf == NULL) /* user is just asking for length */
4931 goto out_len;
4932 if (acl->cached == 0)
4933 goto out;
4934 ret = -ERANGE; /* see getxattr(2) man page */
4935 if (acl->len > buflen)
4936 goto out;
4937 memcpy(buf, acl->data, acl->len);
4938out_len:
4939 ret = acl->len;
4940out:
4941 spin_unlock(&inode->i_lock);
4942 return ret;
4943}
4944
Sachin Prabhu5794d212012-04-17 14:36:40 +01004945static 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 +00004946{
4947 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004948 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004949
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004950 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004951 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004952 if (acl == NULL)
4953 goto out;
4954 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004955 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004956 } else {
4957 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4958 if (acl == NULL)
4959 goto out;
4960 acl->cached = 0;
4961 }
4962 acl->len = acl_len;
4963out:
4964 nfs4_set_cached_acl(inode, acl);
4965}
4966
Andy Adamsonbf118a32011-12-07 11:55:27 -05004967/*
4968 * The getxattr API returns the required buffer length when called with a
4969 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4970 * the required buf. On a NULL buf, we send a page of data to the server
4971 * guessing that the ACL request can be serviced by a page. If so, we cache
4972 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4973 * the cache. If not so, we throw away the page, and cache the required
4974 * length. The next getxattr call will then produce another round trip to
4975 * the server, this time with the input buf of the required size.
4976 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004977static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004978{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004979 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004980 struct nfs_getaclargs args = {
4981 .fh = NFS_FH(inode),
4982 .acl_pages = pages,
4983 .acl_len = buflen,
4984 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004985 struct nfs_getaclres res = {
4986 .acl_len = buflen,
4987 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004988 struct rpc_message msg = {
4989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4990 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004991 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004992 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004993 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4994 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004995
Andy Adamsonbf118a32011-12-07 11:55:27 -05004996 /* As long as we're doing a round trip to the server anyway,
4997 * let's be prepared for a page of acl data. */
4998 if (npages == 0)
4999 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005000 if (npages > ARRAY_SIZE(pages))
5001 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005002
Andy Adamsonbf118a32011-12-07 11:55:27 -05005003 for (i = 0; i < npages; i++) {
5004 pages[i] = alloc_page(GFP_KERNEL);
5005 if (!pages[i])
5006 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005007 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005008
5009 /* for decoding across pages */
5010 res.acl_scratch = alloc_page(GFP_KERNEL);
5011 if (!res.acl_scratch)
5012 goto out_free;
5013
Andy Adamsonbf118a32011-12-07 11:55:27 -05005014 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005015
Peng Taode040be2012-01-10 22:42:47 +08005016 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005017 __func__, buf, buflen, npages, args.acl_len);
5018 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5019 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005020 if (ret)
5021 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005022
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005023 /* Handle the case where the passed-in buffer is too short */
5024 if (res.acl_flags & NFS4_ACL_TRUNC) {
5025 /* Did the user only issue a request for the acl length? */
5026 if (buf == NULL)
5027 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005028 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005029 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005030 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005031 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005032 if (buf) {
5033 if (res.acl_len > buflen) {
5034 ret = -ERANGE;
5035 goto out_free;
5036 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005037 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005038 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005039out_ok:
5040 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005041out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005042 for (i = 0; i < npages; i++)
5043 if (pages[i])
5044 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005045 if (res.acl_scratch)
5046 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005047 return ret;
5048}
5049
Trond Myklebust16b42892006-08-24 12:27:15 -04005050static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5051{
5052 struct nfs4_exception exception = { };
5053 ssize_t ret;
5054 do {
5055 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005056 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005057 if (ret >= 0)
5058 break;
5059 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5060 } while (exception.retry);
5061 return ret;
5062}
5063
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005064static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5065{
5066 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005067 int ret;
5068
5069 if (!nfs4_server_supports_acls(server))
5070 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005071 ret = nfs_revalidate_inode(server, inode);
5072 if (ret < 0)
5073 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005074 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5075 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005076 ret = nfs4_read_cached_acl(inode, buf, buflen);
5077 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005078 /* -ENOENT is returned if there is no ACL or if there is an ACL
5079 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005080 return ret;
5081 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005082}
5083
Trond Myklebust16b42892006-08-24 12:27:15 -04005084static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005085{
5086 struct nfs_server *server = NFS_SERVER(inode);
5087 struct page *pages[NFS4ACL_MAXPAGES];
5088 struct nfs_setaclargs arg = {
5089 .fh = NFS_FH(inode),
5090 .acl_pages = pages,
5091 .acl_len = buflen,
5092 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005093 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005094 struct rpc_message msg = {
5095 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5096 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005097 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005098 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005099 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005100 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005101
5102 if (!nfs4_server_supports_acls(server))
5103 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005104 if (npages > ARRAY_SIZE(pages))
5105 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005106 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005107 if (i < 0)
5108 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005109 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005110 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005111
5112 /*
5113 * Free each page after tx, so the only ref left is
5114 * held by the network stack
5115 */
5116 for (; i > 0; i--)
5117 put_page(pages[i-1]);
5118
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005119 /*
5120 * Acl update can result in inode attribute update.
5121 * so mark the attribute cache invalid.
5122 */
5123 spin_lock(&inode->i_lock);
5124 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5125 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005126 nfs_access_zap_cache(inode);
5127 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005128 return ret;
5129}
5130
Trond Myklebust16b42892006-08-24 12:27:15 -04005131static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5132{
5133 struct nfs4_exception exception = { };
5134 int err;
5135 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005136 err = __nfs4_proc_set_acl(inode, buf, buflen);
5137 trace_nfs4_set_acl(inode, err);
5138 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005139 &exception);
5140 } while (exception.retry);
5141 return err;
5142}
5143
David Quigleyaa9c2662013-05-22 12:50:44 -04005144#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5145static int _nfs4_get_security_label(struct inode *inode, void *buf,
5146 size_t buflen)
5147{
5148 struct nfs_server *server = NFS_SERVER(inode);
5149 struct nfs_fattr fattr;
5150 struct nfs4_label label = {0, 0, buflen, buf};
5151
5152 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005153 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005154 .fh = NFS_FH(inode),
5155 .bitmask = bitmask,
5156 };
5157 struct nfs4_getattr_res res = {
5158 .fattr = &fattr,
5159 .label = &label,
5160 .server = server,
5161 };
5162 struct rpc_message msg = {
5163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005164 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005165 .rpc_resp = &res,
5166 };
5167 int ret;
5168
5169 nfs_fattr_init(&fattr);
5170
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005171 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005172 if (ret)
5173 return ret;
5174 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5175 return -ENOENT;
5176 if (buflen < label.len)
5177 return -ERANGE;
5178 return 0;
5179}
5180
5181static int nfs4_get_security_label(struct inode *inode, void *buf,
5182 size_t buflen)
5183{
5184 struct nfs4_exception exception = { };
5185 int err;
5186
5187 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5188 return -EOPNOTSUPP;
5189
5190 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005191 err = _nfs4_get_security_label(inode, buf, buflen);
5192 trace_nfs4_get_security_label(inode, err);
5193 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005194 &exception);
5195 } while (exception.retry);
5196 return err;
5197}
5198
5199static int _nfs4_do_set_security_label(struct inode *inode,
5200 struct nfs4_label *ilabel,
5201 struct nfs_fattr *fattr,
5202 struct nfs4_label *olabel)
5203{
5204
5205 struct iattr sattr = {0};
5206 struct nfs_server *server = NFS_SERVER(inode);
5207 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005208 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005209 .fh = NFS_FH(inode),
5210 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005211 .server = server,
5212 .bitmask = bitmask,
5213 .label = ilabel,
5214 };
5215 struct nfs_setattrres res = {
5216 .fattr = fattr,
5217 .label = olabel,
5218 .server = server,
5219 };
5220 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005221 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5222 .rpc_argp = &arg,
5223 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005224 };
5225 int status;
5226
Jeff Layton12207f62013-11-01 10:49:32 -04005227 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005228
Jeff Layton12207f62013-11-01 10:49:32 -04005229 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005230 if (status)
5231 dprintk("%s failed: %d\n", __func__, status);
5232
5233 return status;
5234}
5235
5236static int nfs4_do_set_security_label(struct inode *inode,
5237 struct nfs4_label *ilabel,
5238 struct nfs_fattr *fattr,
5239 struct nfs4_label *olabel)
5240{
5241 struct nfs4_exception exception = { };
5242 int err;
5243
5244 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005245 err = _nfs4_do_set_security_label(inode, ilabel,
5246 fattr, olabel);
5247 trace_nfs4_set_security_label(inode, err);
5248 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005249 &exception);
5250 } while (exception.retry);
5251 return err;
5252}
5253
5254static int
Al Viro59301222016-05-27 10:19:30 -04005255nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005256{
5257 struct nfs4_label ilabel, *olabel = NULL;
5258 struct nfs_fattr fattr;
5259 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005260 int status;
5261
5262 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5263 return -EOPNOTSUPP;
5264
5265 nfs_fattr_init(&fattr);
5266
5267 ilabel.pi = 0;
5268 ilabel.lfs = 0;
5269 ilabel.label = (char *)buf;
5270 ilabel.len = buflen;
5271
5272 cred = rpc_lookup_cred();
5273 if (IS_ERR(cred))
5274 return PTR_ERR(cred);
5275
5276 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5277 if (IS_ERR(olabel)) {
5278 status = -PTR_ERR(olabel);
5279 goto out;
5280 }
5281
5282 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5283 if (status == 0)
5284 nfs_setsecurity(inode, &fattr, olabel);
5285
5286 nfs4_label_free(olabel);
5287out:
5288 put_rpccred(cred);
5289 return status;
5290}
5291#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5292
5293
Chuck Leverf0920752012-05-21 22:45:41 -04005294static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5295 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005296{
5297 __be32 verf[2];
5298
Chuck Lever2c820d92012-05-21 22:45:33 -04005299 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5300 /* An impossible timestamp guarantees this value
5301 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005302 verf[0] = cpu_to_be32(U32_MAX);
5303 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005304 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005305 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005306 u64 ns = ktime_to_ns(nn->boot_time);
5307
5308 verf[0] = cpu_to_be32(ns >> 32);
5309 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005310 }
Chuck Levercd937102012-03-02 17:14:31 -05005311 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5312}
5313
Jeff Laytona3192682015-06-09 19:43:59 -04005314static int
5315nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005316{
Jeff Laytona3192682015-06-09 19:43:59 -04005317 size_t len;
5318 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005319
Trond Myklebustceb3a162015-01-03 15:16:04 -05005320 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005321 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005322
Jeff Laytona3192682015-06-09 19:43:59 -04005323 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005324 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005325 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5326 1 +
5327 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5328 1;
5329 rcu_read_unlock();
5330
5331 if (len > NFS4_OPAQUE_LIMIT + 1)
5332 return -EINVAL;
5333
5334 /*
5335 * Since this string is allocated at mount time, and held until the
5336 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5337 * about a memory-reclaim deadlock.
5338 */
5339 str = kmalloc(len, GFP_KERNEL);
5340 if (!str)
5341 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005342
Chuck Levere984a552012-09-14 17:24:21 -04005343 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005344 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005345 clp->cl_ipaddr,
5346 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5347 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005348 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005349
Jeff Laytona3192682015-06-09 19:43:59 -04005350 clp->cl_owner_id = str;
5351 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005352}
5353
Jeff Layton873e3852015-06-09 19:44:00 -04005354static int
5355nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005356{
Jeff Layton873e3852015-06-09 19:44:00 -04005357 size_t len;
5358 char *str;
5359
5360 len = 10 + 10 + 1 + 10 + 1 +
5361 strlen(nfs4_client_id_uniquifier) + 1 +
5362 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5363
5364 if (len > NFS4_OPAQUE_LIMIT + 1)
5365 return -EINVAL;
5366
5367 /*
5368 * Since this string is allocated at mount time, and held until the
5369 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5370 * about a memory-reclaim deadlock.
5371 */
5372 str = kmalloc(len, GFP_KERNEL);
5373 if (!str)
5374 return -ENOMEM;
5375
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005376 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005377 clp->rpc_ops->version, clp->cl_minorversion,
5378 nfs4_client_id_uniquifier,
5379 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005380 clp->cl_owner_id = str;
5381 return 0;
5382}
5383
5384static int
5385nfs4_init_uniform_client_string(struct nfs_client *clp)
5386{
Jeff Layton873e3852015-06-09 19:44:00 -04005387 size_t len;
5388 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005389
5390 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005391 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005392
5393 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005394 return nfs4_init_uniquifier_client_string(clp);
5395
5396 len = 10 + 10 + 1 + 10 + 1 +
5397 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5398
5399 if (len > NFS4_OPAQUE_LIMIT + 1)
5400 return -EINVAL;
5401
5402 /*
5403 * Since this string is allocated at mount time, and held until the
5404 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5405 * about a memory-reclaim deadlock.
5406 */
5407 str = kmalloc(len, GFP_KERNEL);
5408 if (!str)
5409 return -ENOMEM;
5410
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005411 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005412 clp->rpc_ops->version, clp->cl_minorversion,
5413 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005414 clp->cl_owner_id = str;
5415 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005416}
5417
Chuck Lever706cb8d2014-03-12 12:51:47 -04005418/*
5419 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5420 * services. Advertise one based on the address family of the
5421 * clientaddr.
5422 */
5423static unsigned int
5424nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5425{
5426 if (strchr(clp->cl_ipaddr, ':') != NULL)
5427 return scnprintf(buf, len, "tcp6");
5428 else
5429 return scnprintf(buf, len, "tcp");
5430}
5431
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005432static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5433{
5434 struct nfs4_setclientid *sc = calldata;
5435
5436 if (task->tk_status == 0)
5437 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5438}
5439
5440static const struct rpc_call_ops nfs4_setclientid_ops = {
5441 .rpc_call_done = nfs4_setclientid_done,
5442};
5443
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005444/**
5445 * nfs4_proc_setclientid - Negotiate client ID
5446 * @clp: state data structure
5447 * @program: RPC program for NFSv4 callback service
5448 * @port: IP port number for NFS4 callback service
5449 * @cred: RPC credential to use for this call
5450 * @res: where to place the result
5451 *
5452 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5453 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005454int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5455 unsigned short port, struct rpc_cred *cred,
5456 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457{
5458 nfs4_verifier sc_verifier;
5459 struct nfs4_setclientid setclientid = {
5460 .sc_verifier = &sc_verifier,
5461 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005462 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 };
5464 struct rpc_message msg = {
5465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5466 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005467 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005468 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005470 struct rpc_task *task;
5471 struct rpc_task_setup task_setup_data = {
5472 .rpc_client = clp->cl_rpcclient,
5473 .rpc_message = &msg,
5474 .callback_ops = &nfs4_setclientid_ops,
5475 .callback_data = &setclientid,
5476 .flags = RPC_TASK_TIMEOUT,
5477 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005478 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479
Chuck Leverde734832012-07-11 16:30:50 -04005480 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005481 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005482
5483 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5484 status = nfs4_init_uniform_client_string(clp);
5485 else
Jeff Laytona3192682015-06-09 19:43:59 -04005486 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005487
5488 if (status)
5489 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005490
Chuck Leverde734832012-07-11 16:30:50 -04005491 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005492 setclientid.sc_netid_len =
5493 nfs4_init_callback_netid(clp,
5494 setclientid.sc_netid,
5495 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005496 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005497 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 clp->cl_ipaddr, port >> 8, port & 255);
5499
Jeff Layton3a6bb732015-06-09 19:43:57 -04005500 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005501 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005502 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005503 task = rpc_run_task(&task_setup_data);
5504 if (IS_ERR(task)) {
5505 status = PTR_ERR(task);
5506 goto out;
5507 }
5508 status = task->tk_status;
5509 if (setclientid.sc_cred) {
5510 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5511 put_rpccred(setclientid.sc_cred);
5512 }
5513 rpc_put_task(task);
5514out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005515 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005516 dprintk("NFS reply setclientid: %d\n", status);
5517 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518}
5519
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005520/**
5521 * nfs4_proc_setclientid_confirm - Confirm client ID
5522 * @clp: state data structure
5523 * @res: result of a previous SETCLIENTID
5524 * @cred: RPC credential to use for this call
5525 *
5526 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5527 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005528int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005529 struct nfs4_setclientid_res *arg,
5530 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 struct rpc_message msg = {
5533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005534 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005535 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 int status;
5538
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005539 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5540 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5541 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005542 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005543 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005544 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 return status;
5546}
5547
Trond Myklebustfe650402006-01-03 09:55:18 +01005548struct nfs4_delegreturndata {
5549 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005550 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005551 struct nfs_fh fh;
5552 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005553 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005554 struct {
5555 struct nfs4_layoutreturn_args arg;
5556 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005557 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005558 u32 roc_barrier;
5559 bool roc;
5560 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005561 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005562 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005563 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005564};
5565
Trond Myklebustfe650402006-01-03 09:55:18 +01005566static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5567{
5568 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005569
Trond Myklebust14516c32010-07-31 14:29:06 -04005570 if (!nfs4_sequence_done(task, &data->res.seq_res))
5571 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005572
Trond Myklebustca8acf82013-08-13 10:36:56 -04005573 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005574
5575 /* Handle Layoutreturn errors */
5576 if (data->args.lr_args && task->tk_status != 0) {
5577 switch(data->res.lr_ret) {
5578 default:
5579 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5580 break;
5581 case 0:
5582 data->args.lr_args = NULL;
5583 data->res.lr_res = NULL;
5584 break;
5585 case -NFS4ERR_ADMIN_REVOKED:
5586 case -NFS4ERR_DELEG_REVOKED:
5587 case -NFS4ERR_EXPIRED:
5588 case -NFS4ERR_BAD_STATEID:
5589 case -NFS4ERR_OLD_STATEID:
5590 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5591 case -NFS4ERR_WRONG_CRED:
5592 data->args.lr_args = NULL;
5593 data->res.lr_res = NULL;
5594 data->res.lr_ret = 0;
5595 rpc_restart_call_prepare(task);
5596 return;
5597 }
5598 }
5599
Ricardo Labiaga79708862009-12-07 09:23:21 -05005600 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005601 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005602 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005603 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005604 case -NFS4ERR_ADMIN_REVOKED:
5605 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005606 case -NFS4ERR_EXPIRED:
5607 nfs4_free_revoked_stateid(data->res.server,
5608 data->args.stateid,
5609 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005610 case -NFS4ERR_BAD_STATEID:
5611 case -NFS4ERR_OLD_STATEID:
5612 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005613 task->tk_status = 0;
5614 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005615 case -NFS4ERR_ACCESS:
5616 if (data->args.bitmask) {
5617 data->args.bitmask = NULL;
5618 data->res.fattr = NULL;
5619 task->tk_status = 0;
5620 rpc_restart_call_prepare(task);
5621 return;
5622 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005623 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005624 if (nfs4_async_handle_error(task, data->res.server,
5625 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005626 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005627 return;
5628 }
5629 }
5630 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005631}
5632
5633static void nfs4_delegreturn_release(void *calldata)
5634{
Peng Tao039b7562014-07-03 13:05:02 +08005635 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005636 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005637
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005638 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005639 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005640 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5641 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005642 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005643 nfs_iput_and_deactive(inode);
5644 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005645 kfree(calldata);
5646}
5647
Andy Adamson938e1012009-04-01 09:22:28 -04005648static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5649{
5650 struct nfs4_delegreturndata *d_data;
5651
5652 d_data = (struct nfs4_delegreturndata *)data;
5653
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005654 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005655 return;
5656
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005657 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005658 &d_data->args.seq_args,
5659 &d_data->res.seq_res,
5660 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005661}
Andy Adamson938e1012009-04-01 09:22:28 -04005662
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005663static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005664 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005665 .rpc_call_done = nfs4_delegreturn_done,
5666 .rpc_release = nfs4_delegreturn_release,
5667};
5668
Trond Myklebuste6f81072008-01-24 18:14:34 -05005669static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005670{
5671 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005672 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005673 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005674 struct rpc_message msg = {
5675 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5676 .rpc_cred = cred,
5677 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005678 struct rpc_task_setup task_setup_data = {
5679 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005680 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005681 .callback_ops = &nfs4_delegreturn_ops,
5682 .flags = RPC_TASK_ASYNC,
5683 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005684 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005685
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005686 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005687 if (data == NULL)
5688 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005689 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005690
5691 nfs4_state_protect(server->nfs_client,
5692 NFS_SP4_MACH_CRED_CLEANUP,
5693 &task_setup_data.rpc_client, &msg);
5694
Trond Myklebustfe650402006-01-03 09:55:18 +01005695 data->args.fhandle = &data->fh;
5696 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005697 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005698 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005699 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005700 data->res.fattr = &data->fattr;
5701 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005702 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005703 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005704 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005705 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005706 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005707 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005708 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005709 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005710 if (data->lr.roc) {
5711 data->args.lr_args = &data->lr.arg;
5712 data->res.lr_res = &data->lr.res;
5713 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005714 } else if (data->lr.roc) {
5715 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5716 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005717 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005718
Trond Myklebustc970aa82007-07-14 15:39:59 -04005719 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005720 msg.rpc_argp = &data->args;
5721 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005722 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005723 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005724 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005725 if (!issync)
5726 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005727 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005728 if (status != 0)
5729 goto out;
5730 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005731out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005732 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005733 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734}
5735
Trond Myklebuste6f81072008-01-24 18:14:34 -05005736int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737{
5738 struct nfs_server *server = NFS_SERVER(inode);
5739 struct nfs4_exception exception = { };
5740 int err;
5741 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005742 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005743 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 switch (err) {
5745 case -NFS4ERR_STALE_STATEID:
5746 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 case 0:
5748 return 0;
5749 }
5750 err = nfs4_handle_exception(server, err, &exception);
5751 } while (exception.retry);
5752 return err;
5753}
5754
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5756{
5757 struct inode *inode = state->inode;
5758 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005759 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005760 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005762 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005764 struct nfs_lockt_res res = {
5765 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 };
5767 struct rpc_message msg = {
5768 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005769 .rpc_argp = &arg,
5770 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 .rpc_cred = state->owner->so_cred,
5772 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 struct nfs4_lock_state *lsp;
5774 int status;
5775
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005776 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005777 status = nfs4_set_lock_state(state, request);
5778 if (status != 0)
5779 goto out;
5780 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005781 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005782 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005783 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005784 switch (status) {
5785 case 0:
5786 request->fl_type = F_UNLCK;
5787 break;
5788 case -NFS4ERR_DENIED:
5789 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005791 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005792 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005793out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 return status;
5795}
5796
5797static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5798{
5799 struct nfs4_exception exception = { };
5800 int err;
5801
5802 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005803 err = _nfs4_proc_getlk(state, cmd, request);
5804 trace_nfs4_get_lock(request, state, cmd, err);
5805 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 &exception);
5807 } while (exception.retry);
5808 return err;
5809}
5810
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005811struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005812 struct nfs_locku_args arg;
5813 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005814 struct nfs4_lock_state *lsp;
5815 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005816 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005817 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005818 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005819};
5820
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005821static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5822 struct nfs_open_context *ctx,
5823 struct nfs4_lock_state *lsp,
5824 struct nfs_seqid *seqid)
5825{
5826 struct nfs4_unlockdata *p;
5827 struct inode *inode = lsp->ls_state->inode;
5828
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005829 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005830 if (p == NULL)
5831 return NULL;
5832 p->arg.fh = NFS_FH(inode);
5833 p->arg.fl = &p->fl;
5834 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005835 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005836 p->lsp = lsp;
5837 atomic_inc(&lsp->ls_count);
5838 /* Ensure we don't close file until we're done freeing locks! */
5839 p->ctx = get_nfs_open_context(ctx);
5840 memcpy(&p->fl, fl, sizeof(p->fl));
5841 p->server = NFS_SERVER(inode);
5842 return p;
5843}
5844
Trond Myklebust06f814a2006-01-03 09:55:07 +01005845static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005846{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005847 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005848 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005849 nfs4_put_lock_state(calldata->lsp);
5850 put_nfs_open_context(calldata->ctx);
5851 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005852}
5853
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005854static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005855{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005856 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005857
Trond Myklebust14516c32010-07-31 14:29:06 -04005858 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5859 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005860 switch (task->tk_status) {
5861 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005862 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005863 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005864 if (nfs4_update_lock_stateid(calldata->lsp,
5865 &calldata->res.stateid))
5866 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005867 case -NFS4ERR_ADMIN_REVOKED:
5868 case -NFS4ERR_EXPIRED:
5869 nfs4_free_revoked_stateid(calldata->server,
5870 &calldata->arg.stateid,
5871 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005872 case -NFS4ERR_BAD_STATEID:
5873 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005874 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005875 if (!nfs4_stateid_match(&calldata->arg.stateid,
5876 &calldata->lsp->ls_stateid))
5877 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005878 break;
5879 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005880 if (nfs4_async_handle_error(task, calldata->server,
5881 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005882 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005883 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005884 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005885}
5886
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005887static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005888{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005889 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005891 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005892 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005893 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005894 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005895 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005896 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005897 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005898 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005899 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04005900 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005901 &calldata->res.seq_res,
5902 task) != 0)
5903 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005904 return;
5905out_no_action:
5906 task->tk_action = NULL;
5907out_wait:
5908 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909}
5910
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005911static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005912 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005913 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005914 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005915};
5916
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005917static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5918 struct nfs_open_context *ctx,
5919 struct nfs4_lock_state *lsp,
5920 struct nfs_seqid *seqid)
5921{
5922 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005923 struct rpc_message msg = {
5924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5925 .rpc_cred = ctx->cred,
5926 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005927 struct rpc_task_setup task_setup_data = {
5928 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005929 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005930 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005931 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005932 .flags = RPC_TASK_ASYNC,
5933 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005934
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005935 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5936 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5937
Frank Filz137d6ac2007-07-09 15:32:29 -07005938 /* Ensure this is an unlock - when canceling a lock, the
5939 * canceled lock is passed in, and it won't be an unlock.
5940 */
5941 fl->fl_type = F_UNLCK;
5942
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005943 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5944 if (data == NULL) {
5945 nfs_free_seqid(seqid);
5946 return ERR_PTR(-ENOMEM);
5947 }
5948
Chuck Levera9c92d62013-08-09 12:48:18 -04005949 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005950 msg.rpc_argp = &data->arg;
5951 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005952 task_setup_data.callback_data = data;
5953 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005954}
5955
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5957{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005958 struct inode *inode = state->inode;
5959 struct nfs4_state_owner *sp = state->owner;
5960 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005961 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005962 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005963 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005964 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005965 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005966 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967
Trond Myklebust9b073572006-06-29 16:38:34 -04005968 status = nfs4_set_lock_state(state, request);
5969 /* Unlock _before_ we do the RPC call */
5970 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005971 /* Exclude nfs_delegation_claim_locks() */
5972 mutex_lock(&sp->so_delegreturn_mutex);
5973 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005974 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005975 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005976 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005977 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005978 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005979 }
5980 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005981 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005982 if (status != 0)
5983 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005984 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005985 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005986 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5987 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005988 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5989 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005990 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005991 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005992 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005993 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005994 status = PTR_ERR(task);
5995 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005996 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005997 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005998 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005999out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006000 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006001 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006002 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003}
6004
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006005struct nfs4_lockdata {
6006 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006007 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006008 struct nfs4_lock_state *lsp;
6009 struct nfs_open_context *ctx;
6010 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006011 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006012 int rpc_status;
6013 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006014 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006015};
6016
6017static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006018 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6019 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006020{
6021 struct nfs4_lockdata *p;
6022 struct inode *inode = lsp->ls_state->inode;
6023 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006024 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006025
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006026 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006027 if (p == NULL)
6028 return NULL;
6029
6030 p->arg.fh = NFS_FH(inode);
6031 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006032 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006033 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006034 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006035 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6036 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006037 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006038 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006039 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006040 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006041 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006042 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006043 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006044 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006045 atomic_inc(&lsp->ls_count);
6046 p->ctx = get_nfs_open_context(ctx);
6047 memcpy(&p->fl, fl, sizeof(p->fl));
6048 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006049out_free_seqid:
6050 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006051out_free:
6052 kfree(p);
6053 return NULL;
6054}
6055
6056static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6057{
6058 struct nfs4_lockdata *data = calldata;
6059 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060
Harvey Harrison3110ff82008-05-02 13:42:44 -07006061 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006062 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006063 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006064 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006065 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006066 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006067 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006068 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006069 nfs4_stateid_copy(&data->arg.open_stateid,
6070 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006071 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006072 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006073 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006074 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006075 nfs4_stateid_copy(&data->arg.lock_stateid,
6076 &data->lsp->ls_stateid);
6077 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006078 if (!nfs4_valid_open_stateid(state)) {
6079 data->rpc_status = -EBADF;
6080 task->tk_action = NULL;
6081 goto out_release_open_seqid;
6082 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006083 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006084 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006085 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006086 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006087 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006088 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006089out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006090 nfs_release_seqid(data->arg.open_seqid);
6091out_release_lock_seqid:
6092 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006093out_wait:
6094 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006095 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006096}
6097
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006098static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6099{
6100 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006101 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006102
Harvey Harrison3110ff82008-05-02 13:42:44 -07006103 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006104
Trond Myklebust14516c32010-07-31 14:29:06 -04006105 if (!nfs4_sequence_done(task, &data->res.seq_res))
6106 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006107
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006108 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006109 switch (task->tk_status) {
6110 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006111 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006112 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006113 if (data->arg.new_lock) {
6114 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006115 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006116 rpc_restart_call_prepare(task);
6117 break;
6118 }
6119 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006120 if (data->arg.new_lock_owner != 0) {
6121 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6122 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6123 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6124 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6125 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006126 break;
6127 case -NFS4ERR_BAD_STATEID:
6128 case -NFS4ERR_OLD_STATEID:
6129 case -NFS4ERR_STALE_STATEID:
6130 case -NFS4ERR_EXPIRED:
6131 if (data->arg.new_lock_owner != 0) {
6132 if (!nfs4_stateid_match(&data->arg.open_stateid,
6133 &lsp->ls_state->open_stateid))
6134 rpc_restart_call_prepare(task);
6135 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6136 &lsp->ls_stateid))
6137 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006138 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006139 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006140}
6141
6142static void nfs4_lock_release(void *calldata)
6143{
6144 struct nfs4_lockdata *data = calldata;
6145
Harvey Harrison3110ff82008-05-02 13:42:44 -07006146 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006147 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006148 if (data->cancelled != 0) {
6149 struct rpc_task *task;
6150 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6151 data->arg.lock_seqid);
6152 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006153 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006154 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006155 } else
6156 nfs_free_seqid(data->arg.lock_seqid);
6157 nfs4_put_lock_state(data->lsp);
6158 put_nfs_open_context(data->ctx);
6159 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006160 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006161}
6162
6163static const struct rpc_call_ops nfs4_lock_ops = {
6164 .rpc_call_prepare = nfs4_lock_prepare,
6165 .rpc_call_done = nfs4_lock_done,
6166 .rpc_release = nfs4_lock_release,
6167};
6168
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006169static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6170{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006171 switch (error) {
6172 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006173 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006174 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006175 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006176 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006177 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006178 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006179 break;
6180 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006181 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006182 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006183 };
6184}
6185
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006186static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006187{
6188 struct nfs4_lockdata *data;
6189 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006190 struct rpc_message msg = {
6191 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6192 .rpc_cred = state->owner->so_cred,
6193 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006194 struct rpc_task_setup task_setup_data = {
6195 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006196 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006197 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006198 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006199 .flags = RPC_TASK_ASYNC,
6200 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006201 int ret;
6202
Harvey Harrison3110ff82008-05-02 13:42:44 -07006203 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006204 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006205 fl->fl_u.nfs4_fl.owner,
6206 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006207 if (data == NULL)
6208 return -ENOMEM;
6209 if (IS_SETLKW(cmd))
6210 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006211 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006212 msg.rpc_argp = &data->arg;
6213 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006214 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006215 if (recovery_type > NFS_LOCK_NEW) {
6216 if (recovery_type == NFS_LOCK_RECLAIM)
6217 data->arg.reclaim = NFS_LOCK_RECLAIM;
6218 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006219 } else
6220 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006221 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006222 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006223 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006224 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006225 if (ret == 0) {
6226 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006227 if (ret)
6228 nfs4_handle_setlk_error(data->server, data->lsp,
6229 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006230 } else
6231 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006232 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006233 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006234 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006235 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236}
6237
6238static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6239{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006240 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006241 struct nfs4_exception exception = {
6242 .inode = state->inode,
6243 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006244 int err;
6245
6246 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006247 /* Cache the lock if possible... */
6248 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6249 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006250 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006251 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006252 break;
6253 nfs4_handle_exception(server, err, &exception);
6254 } while (exception.retry);
6255 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256}
6257
6258static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6259{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006260 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006261 struct nfs4_exception exception = {
6262 .inode = state->inode,
6263 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006264 int err;
6265
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006266 err = nfs4_set_lock_state(state, request);
6267 if (err != 0)
6268 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006269 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006270 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6271 return 0;
6272 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006273 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006274 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6275 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006276 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006277 switch (err) {
6278 default:
6279 goto out;
6280 case -NFS4ERR_GRACE:
6281 case -NFS4ERR_DELAY:
6282 nfs4_handle_exception(server, err, &exception);
6283 err = 0;
6284 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006285 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006286out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006287 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288}
6289
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006290#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006291static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6292{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006293 struct nfs4_lock_state *lsp;
6294 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006295
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006296 status = nfs4_set_lock_state(state, request);
6297 if (status != 0)
6298 return status;
6299 lsp = request->fl_u.nfs4_fl.owner;
6300 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6301 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6302 return 0;
6303 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006304 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006305}
6306#endif
6307
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6309{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006310 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006311 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006312 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006313 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314
Trond Myklebust01c3b862006-06-29 16:38:39 -04006315 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006316 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006317 if (status < 0)
6318 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006319 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006320 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006321 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006322 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006323 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006324 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006325 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006326 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006327 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006328 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006329 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006330 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006331 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006332 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006333out:
6334 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 return status;
6336}
6337
6338static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6339{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006340 struct nfs4_exception exception = {
6341 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006342 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006343 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344 int err;
6345
6346 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006347 err = _nfs4_proc_setlk(state, cmd, request);
6348 if (err == -NFS4ERR_DENIED)
6349 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006351 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 } while (exception.retry);
6353 return err;
6354}
6355
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006356#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6357#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6358
6359static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006360nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6361 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006362{
6363 int status = -ERESTARTSYS;
6364 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6365
6366 while(!signalled()) {
6367 status = nfs4_proc_setlk(state, cmd, request);
6368 if ((status != -EAGAIN) || IS_SETLK(cmd))
6369 break;
6370 freezable_schedule_timeout_interruptible(timeout);
6371 timeout *= 2;
6372 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6373 status = -ERESTARTSYS;
6374 }
6375 return status;
6376}
6377
Jeff Laytona1d617d82016-09-17 18:17:39 -04006378#ifdef CONFIG_NFS_V4_1
6379struct nfs4_lock_waiter {
6380 struct task_struct *task;
6381 struct inode *inode;
6382 struct nfs_lowner *owner;
6383 bool notified;
6384};
6385
6386static int
6387nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6388{
6389 int ret;
6390 struct cb_notify_lock_args *cbnl = key;
6391 struct nfs4_lock_waiter *waiter = wait->private;
6392 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6393 *wowner = waiter->owner;
6394
6395 /* Only wake if the callback was for the same owner */
6396 if (lowner->clientid != wowner->clientid ||
6397 lowner->id != wowner->id ||
6398 lowner->s_dev != wowner->s_dev)
6399 return 0;
6400
6401 /* Make sure it's for the right inode */
6402 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6403 return 0;
6404
6405 waiter->notified = true;
6406
6407 /* override "private" so we can use default_wake_function */
6408 wait->private = waiter->task;
6409 ret = autoremove_wake_function(wait, mode, flags, key);
6410 wait->private = waiter;
6411 return ret;
6412}
6413
6414static int
6415nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6416{
6417 int status = -ERESTARTSYS;
6418 unsigned long flags;
6419 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6420 struct nfs_server *server = NFS_SERVER(state->inode);
6421 struct nfs_client *clp = server->nfs_client;
6422 wait_queue_head_t *q = &clp->cl_lock_waitq;
6423 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6424 .id = lsp->ls_seqid.owner_id,
6425 .s_dev = server->s_dev };
6426 struct nfs4_lock_waiter waiter = { .task = current,
6427 .inode = state->inode,
6428 .owner = &owner,
6429 .notified = false };
6430 wait_queue_t wait;
6431
6432 /* Don't bother with waitqueue if we don't expect a callback */
6433 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6434 return nfs4_retry_setlk_simple(state, cmd, request);
6435
6436 init_wait(&wait);
6437 wait.private = &waiter;
6438 wait.func = nfs4_wake_lock_waiter;
6439 add_wait_queue(q, &wait);
6440
6441 while(!signalled()) {
6442 status = nfs4_proc_setlk(state, cmd, request);
6443 if ((status != -EAGAIN) || IS_SETLK(cmd))
6444 break;
6445
6446 status = -ERESTARTSYS;
6447 spin_lock_irqsave(&q->lock, flags);
6448 if (waiter.notified) {
6449 spin_unlock_irqrestore(&q->lock, flags);
6450 continue;
6451 }
6452 set_current_state(TASK_INTERRUPTIBLE);
6453 spin_unlock_irqrestore(&q->lock, flags);
6454
6455 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6456 }
6457
6458 finish_wait(q, &wait);
6459 return status;
6460}
6461#else /* !CONFIG_NFS_V4_1 */
6462static inline int
6463nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6464{
6465 return nfs4_retry_setlk_simple(state, cmd, request);
6466}
6467#endif
6468
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469static int
6470nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6471{
6472 struct nfs_open_context *ctx;
6473 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 int status;
6475
6476 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006477 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478 state = ctx->state;
6479
6480 if (request->fl_start < 0 || request->fl_end < 0)
6481 return -EINVAL;
6482
Trond Myklebustd9531262009-07-21 19:22:38 -04006483 if (IS_GETLK(cmd)) {
6484 if (state != NULL)
6485 return nfs4_proc_getlk(state, F_GETLK, request);
6486 return 0;
6487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488
6489 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6490 return -EINVAL;
6491
Trond Myklebustd9531262009-07-21 19:22:38 -04006492 if (request->fl_type == F_UNLCK) {
6493 if (state != NULL)
6494 return nfs4_proc_unlck(state, cmd, request);
6495 return 0;
6496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497
Trond Myklebustd9531262009-07-21 19:22:38 -04006498 if (state == NULL)
6499 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006500
6501 if ((request->fl_flags & FL_POSIX) &&
6502 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6503 return -ENOLCK;
6504
Trond Myklebust55725512012-04-18 12:48:35 -04006505 /*
6506 * Don't rely on the VFS having checked the file open mode,
6507 * since it won't do this for flock() locks.
6508 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006509 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006510 case F_RDLCK:
6511 if (!(filp->f_mode & FMODE_READ))
6512 return -EBADF;
6513 break;
6514 case F_WRLCK:
6515 if (!(filp->f_mode & FMODE_WRITE))
6516 return -EBADF;
6517 }
6518
Jeff Layton1ea67db2016-09-17 18:17:37 -04006519 status = nfs4_set_lock_state(state, request);
6520 if (status != 0)
6521 return status;
6522
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006523 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524}
6525
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006526int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006527{
6528 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006529 int err;
6530
6531 err = nfs4_set_lock_state(state, fl);
6532 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006533 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006534 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006535 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006536}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006537
Trond Myklebustcf470c32012-03-07 13:49:12 -05006538struct nfs_release_lockowner_data {
6539 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006540 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006541 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006542 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006543 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006544};
6545
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006546static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6547{
6548 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006549 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006550 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6551 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006552 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006553 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006554}
6555
6556static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6557{
6558 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006559 struct nfs_server *server = data->server;
6560
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006561 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006562
6563 switch (task->tk_status) {
6564 case 0:
6565 renew_lease(server, data->timestamp);
6566 break;
6567 case -NFS4ERR_STALE_CLIENTID:
6568 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006569 nfs4_schedule_lease_recovery(server->nfs_client);
6570 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006571 case -NFS4ERR_LEASE_MOVED:
6572 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006573 if (nfs4_async_handle_error(task, server,
6574 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006575 rpc_restart_call_prepare(task);
6576 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006577}
6578
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006579static void nfs4_release_lockowner_release(void *calldata)
6580{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006581 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006582 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006583 kfree(calldata);
6584}
6585
Trond Myklebust17280172012-03-11 13:11:00 -04006586static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006587 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6588 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006589 .rpc_release = nfs4_release_lockowner_release,
6590};
6591
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006592static void
6593nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006594{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006595 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006596 struct rpc_message msg = {
6597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6598 };
6599
6600 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006601 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006602
Trond Myklebustcf470c32012-03-07 13:49:12 -05006603 data = kmalloc(sizeof(*data), GFP_NOFS);
6604 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006605 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006606 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006607 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006608 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6609 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6610 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006611
Trond Myklebustcf470c32012-03-07 13:49:12 -05006612 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006613 msg.rpc_resp = &data->res;
6614 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006615 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006616}
6617
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006618#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6619
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006620static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006621 struct dentry *unused, struct inode *inode,
6622 const char *key, const void *buf,
6623 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006624{
Al Viro59301222016-05-27 10:19:30 -04006625 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006626}
6627
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006628static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006629 struct dentry *unused, struct inode *inode,
6630 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006631{
Al Virob2968212016-04-10 20:48:24 -04006632 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006633}
6634
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006635static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006636{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006637 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006638}
6639
David Quigleyc9bccef2013-05-22 12:50:45 -04006640#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006641
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006642static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006643 struct dentry *unused, struct inode *inode,
6644 const char *key, const void *buf,
6645 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006646{
6647 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006648 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006649
6650 return -EOPNOTSUPP;
6651}
6652
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006653static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006654 struct dentry *unused, struct inode *inode,
6655 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006656{
6657 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006658 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006659 return -EOPNOTSUPP;
6660}
6661
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006662static ssize_t
6663nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006664{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006665 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006666
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006667 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6668 len = security_inode_listsecurity(inode, list, list_len);
6669 if (list_len && len > list_len)
6670 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006671 }
6672 return len;
6673}
6674
6675static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6676 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006677 .get = nfs4_xattr_get_nfs4_label,
6678 .set = nfs4_xattr_set_nfs4_label,
6679};
David Quigleyc9bccef2013-05-22 12:50:45 -04006680
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006681#else
6682
6683static ssize_t
6684nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6685{
6686 return 0;
6687}
6688
6689#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006690
Andy Adamson533eb462011-06-13 18:25:56 -04006691/*
6692 * nfs_fhget will use either the mounted_on_fileid or the fileid
6693 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006694static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6695{
Andy Adamson533eb462011-06-13 18:25:56 -04006696 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6697 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6698 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006699 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006700 return;
6701
6702 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006703 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006704 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6705 fattr->nlink = 2;
6706}
6707
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006708static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6709 const struct qstr *name,
6710 struct nfs4_fs_locations *fs_locations,
6711 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006712{
6713 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006714 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006715 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006716 };
6717 struct nfs4_fs_locations_arg args = {
6718 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006719 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006720 .page = page,
6721 .bitmask = bitmask,
6722 };
Benny Halevy22958462009-04-01 09:22:02 -04006723 struct nfs4_fs_locations_res res = {
6724 .fs_locations = fs_locations,
6725 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006726 struct rpc_message msg = {
6727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6728 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006729 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006730 };
6731 int status;
6732
Harvey Harrison3110ff82008-05-02 13:42:44 -07006733 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006734
6735 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6736 * is not supported */
6737 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6738 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6739 else
6740 bitmask[0] |= FATTR4_WORD0_FILEID;
6741
Trond Myklebustc228fd32007-01-13 02:28:11 -05006742 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006743 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006744 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006745 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006746 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006747 return status;
6748}
6749
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006750int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6751 const struct qstr *name,
6752 struct nfs4_fs_locations *fs_locations,
6753 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006754{
6755 struct nfs4_exception exception = { };
6756 int err;
6757 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006758 err = _nfs4_proc_fs_locations(client, dir, name,
6759 fs_locations, page);
6760 trace_nfs4_get_fs_locations(dir, name, err);
6761 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006762 &exception);
6763 } while (exception.retry);
6764 return err;
6765}
6766
Chuck Leverb03d7352013-10-17 14:12:50 -04006767/*
6768 * This operation also signals the server that this client is
6769 * performing migration recovery. The server can stop returning
6770 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6771 * appended to this compound to identify the client ID which is
6772 * performing recovery.
6773 */
6774static int _nfs40_proc_get_locations(struct inode *inode,
6775 struct nfs4_fs_locations *locations,
6776 struct page *page, struct rpc_cred *cred)
6777{
6778 struct nfs_server *server = NFS_SERVER(inode);
6779 struct rpc_clnt *clnt = server->client;
6780 u32 bitmask[2] = {
6781 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6782 };
6783 struct nfs4_fs_locations_arg args = {
6784 .clientid = server->nfs_client->cl_clientid,
6785 .fh = NFS_FH(inode),
6786 .page = page,
6787 .bitmask = bitmask,
6788 .migration = 1, /* skip LOOKUP */
6789 .renew = 1, /* append RENEW */
6790 };
6791 struct nfs4_fs_locations_res res = {
6792 .fs_locations = locations,
6793 .migration = 1,
6794 .renew = 1,
6795 };
6796 struct rpc_message msg = {
6797 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6798 .rpc_argp = &args,
6799 .rpc_resp = &res,
6800 .rpc_cred = cred,
6801 };
6802 unsigned long now = jiffies;
6803 int status;
6804
6805 nfs_fattr_init(&locations->fattr);
6806 locations->server = server;
6807 locations->nlocations = 0;
6808
6809 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6810 nfs4_set_sequence_privileged(&args.seq_args);
6811 status = nfs4_call_sync_sequence(clnt, server, &msg,
6812 &args.seq_args, &res.seq_res);
6813 if (status)
6814 return status;
6815
6816 renew_lease(server, now);
6817 return 0;
6818}
6819
6820#ifdef CONFIG_NFS_V4_1
6821
6822/*
6823 * This operation also signals the server that this client is
6824 * performing migration recovery. The server can stop asserting
6825 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6826 * performing this operation is identified in the SEQUENCE
6827 * operation in this compound.
6828 *
6829 * When the client supports GETATTR(fs_locations_info), it can
6830 * be plumbed in here.
6831 */
6832static int _nfs41_proc_get_locations(struct inode *inode,
6833 struct nfs4_fs_locations *locations,
6834 struct page *page, struct rpc_cred *cred)
6835{
6836 struct nfs_server *server = NFS_SERVER(inode);
6837 struct rpc_clnt *clnt = server->client;
6838 u32 bitmask[2] = {
6839 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6840 };
6841 struct nfs4_fs_locations_arg args = {
6842 .fh = NFS_FH(inode),
6843 .page = page,
6844 .bitmask = bitmask,
6845 .migration = 1, /* skip LOOKUP */
6846 };
6847 struct nfs4_fs_locations_res res = {
6848 .fs_locations = locations,
6849 .migration = 1,
6850 };
6851 struct rpc_message msg = {
6852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6853 .rpc_argp = &args,
6854 .rpc_resp = &res,
6855 .rpc_cred = cred,
6856 };
6857 int status;
6858
6859 nfs_fattr_init(&locations->fattr);
6860 locations->server = server;
6861 locations->nlocations = 0;
6862
6863 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6864 nfs4_set_sequence_privileged(&args.seq_args);
6865 status = nfs4_call_sync_sequence(clnt, server, &msg,
6866 &args.seq_args, &res.seq_res);
6867 if (status == NFS4_OK &&
6868 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6869 status = -NFS4ERR_LEASE_MOVED;
6870 return status;
6871}
6872
6873#endif /* CONFIG_NFS_V4_1 */
6874
6875/**
6876 * nfs4_proc_get_locations - discover locations for a migrated FSID
6877 * @inode: inode on FSID that is migrating
6878 * @locations: result of query
6879 * @page: buffer
6880 * @cred: credential to use for this operation
6881 *
6882 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6883 * operation failed, or a negative errno if a local error occurred.
6884 *
6885 * On success, "locations" is filled in, but if the server has
6886 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6887 * asserted.
6888 *
6889 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6890 * from this client that require migration recovery.
6891 */
6892int nfs4_proc_get_locations(struct inode *inode,
6893 struct nfs4_fs_locations *locations,
6894 struct page *page, struct rpc_cred *cred)
6895{
6896 struct nfs_server *server = NFS_SERVER(inode);
6897 struct nfs_client *clp = server->nfs_client;
6898 const struct nfs4_mig_recovery_ops *ops =
6899 clp->cl_mvops->mig_recovery_ops;
6900 struct nfs4_exception exception = { };
6901 int status;
6902
6903 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6904 (unsigned long long)server->fsid.major,
6905 (unsigned long long)server->fsid.minor,
6906 clp->cl_hostname);
6907 nfs_display_fhandle(NFS_FH(inode), __func__);
6908
6909 do {
6910 status = ops->get_locations(inode, locations, page, cred);
6911 if (status != -NFS4ERR_DELAY)
6912 break;
6913 nfs4_handle_exception(server, status, &exception);
6914 } while (exception.retry);
6915 return status;
6916}
6917
Chuck Lever44c99932013-10-17 14:13:30 -04006918/*
6919 * This operation also signals the server that this client is
6920 * performing "lease moved" recovery. The server can stop
6921 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6922 * is appended to this compound to identify the client ID which is
6923 * performing recovery.
6924 */
6925static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6926{
6927 struct nfs_server *server = NFS_SERVER(inode);
6928 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6929 struct rpc_clnt *clnt = server->client;
6930 struct nfs4_fsid_present_arg args = {
6931 .fh = NFS_FH(inode),
6932 .clientid = clp->cl_clientid,
6933 .renew = 1, /* append RENEW */
6934 };
6935 struct nfs4_fsid_present_res res = {
6936 .renew = 1,
6937 };
6938 struct rpc_message msg = {
6939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6940 .rpc_argp = &args,
6941 .rpc_resp = &res,
6942 .rpc_cred = cred,
6943 };
6944 unsigned long now = jiffies;
6945 int status;
6946
6947 res.fh = nfs_alloc_fhandle();
6948 if (res.fh == NULL)
6949 return -ENOMEM;
6950
6951 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6952 nfs4_set_sequence_privileged(&args.seq_args);
6953 status = nfs4_call_sync_sequence(clnt, server, &msg,
6954 &args.seq_args, &res.seq_res);
6955 nfs_free_fhandle(res.fh);
6956 if (status)
6957 return status;
6958
6959 do_renew_lease(clp, now);
6960 return 0;
6961}
6962
6963#ifdef CONFIG_NFS_V4_1
6964
6965/*
6966 * This operation also signals the server that this client is
6967 * performing "lease moved" recovery. The server can stop asserting
6968 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6969 * this operation is identified in the SEQUENCE operation in this
6970 * compound.
6971 */
6972static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6973{
6974 struct nfs_server *server = NFS_SERVER(inode);
6975 struct rpc_clnt *clnt = server->client;
6976 struct nfs4_fsid_present_arg args = {
6977 .fh = NFS_FH(inode),
6978 };
6979 struct nfs4_fsid_present_res res = {
6980 };
6981 struct rpc_message msg = {
6982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6983 .rpc_argp = &args,
6984 .rpc_resp = &res,
6985 .rpc_cred = cred,
6986 };
6987 int status;
6988
6989 res.fh = nfs_alloc_fhandle();
6990 if (res.fh == NULL)
6991 return -ENOMEM;
6992
6993 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6994 nfs4_set_sequence_privileged(&args.seq_args);
6995 status = nfs4_call_sync_sequence(clnt, server, &msg,
6996 &args.seq_args, &res.seq_res);
6997 nfs_free_fhandle(res.fh);
6998 if (status == NFS4_OK &&
6999 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7000 status = -NFS4ERR_LEASE_MOVED;
7001 return status;
7002}
7003
7004#endif /* CONFIG_NFS_V4_1 */
7005
7006/**
7007 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7008 * @inode: inode on FSID to check
7009 * @cred: credential to use for this operation
7010 *
7011 * Server indicates whether the FSID is present, moved, or not
7012 * recognized. This operation is necessary to clear a LEASE_MOVED
7013 * condition for this client ID.
7014 *
7015 * Returns NFS4_OK if the FSID is present on this server,
7016 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7017 * NFS4ERR code if some error occurred on the server, or a
7018 * negative errno if a local failure occurred.
7019 */
7020int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7021{
7022 struct nfs_server *server = NFS_SERVER(inode);
7023 struct nfs_client *clp = server->nfs_client;
7024 const struct nfs4_mig_recovery_ops *ops =
7025 clp->cl_mvops->mig_recovery_ops;
7026 struct nfs4_exception exception = { };
7027 int status;
7028
7029 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7030 (unsigned long long)server->fsid.major,
7031 (unsigned long long)server->fsid.minor,
7032 clp->cl_hostname);
7033 nfs_display_fhandle(NFS_FH(inode), __func__);
7034
7035 do {
7036 status = ops->fsid_present(inode, cred);
7037 if (status != -NFS4ERR_DELAY)
7038 break;
7039 nfs4_handle_exception(server, status, &exception);
7040 } while (exception.retry);
7041 return status;
7042}
7043
Andy Adamson5ec16a82013-08-08 10:57:55 -04007044/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007045 * If 'use_integrity' is true and the state managment nfs_client
7046 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7047 * and the machine credential as per RFC3530bis and RFC5661 Security
7048 * Considerations sections. Otherwise, just use the user cred with the
7049 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007050 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007051static 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 +00007052{
7053 int status;
7054 struct nfs4_secinfo_arg args = {
7055 .dir_fh = NFS_FH(dir),
7056 .name = name,
7057 };
7058 struct nfs4_secinfo_res res = {
7059 .flavors = flavors,
7060 };
7061 struct rpc_message msg = {
7062 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7063 .rpc_argp = &args,
7064 .rpc_resp = &res,
7065 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007066 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007067 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007068
7069 if (use_integrity) {
7070 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007071 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7072 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007073 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007074
7075 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007076
7077 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7078 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7079
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007080 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7081 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007082 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007083
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007084 if (cred)
7085 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007086
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007087 return status;
7088}
7089
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007090int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7091 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007092{
7093 struct nfs4_exception exception = { };
7094 int err;
7095 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007096 err = -NFS4ERR_WRONGSEC;
7097
7098 /* try to use integrity protection with machine cred */
7099 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7100 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7101
7102 /*
7103 * if unable to use integrity protection, or SECINFO with
7104 * integrity protection returns NFS4ERR_WRONGSEC (which is
7105 * disallowed by spec, but exists in deployed servers) use
7106 * the current filesystem's rpc_client and the user cred.
7107 */
7108 if (err == -NFS4ERR_WRONGSEC)
7109 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7110
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007111 trace_nfs4_secinfo(dir, name, err);
7112 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007113 &exception);
7114 } while (exception.retry);
7115 return err;
7116}
7117
Andy Adamson557134a2009-04-01 09:21:53 -04007118#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007119/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007120 * Check the exchange flags returned by the server for invalid flags, having
7121 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7122 * DS flags set.
7123 */
7124static int nfs4_check_cl_exchange_flags(u32 flags)
7125{
7126 if (flags & ~EXCHGID4_FLAG_MASK_R)
7127 goto out_inval;
7128 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7129 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7130 goto out_inval;
7131 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7132 goto out_inval;
7133 return NFS_OK;
7134out_inval:
7135 return -NFS4ERR_INVAL;
7136}
7137
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007138static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007139nfs41_same_server_scope(struct nfs41_server_scope *a,
7140 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007141{
7142 if (a->server_scope_sz == b->server_scope_sz &&
7143 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7144 return true;
7145
7146 return false;
7147}
7148
Andy Adamson02a95de2016-02-05 16:08:37 -05007149static void
7150nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7151{
7152}
7153
7154static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7155 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7156};
7157
Andy Adamson357f54d2010-12-14 10:11:57 -05007158/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007159 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007160 *
7161 * The 4.1 client currently uses the same TCP connection for the
7162 * fore and backchannel.
7163 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007164static
7165int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7166 struct rpc_xprt *xprt,
7167 struct nfs_client *clp,
7168 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007169{
7170 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007171 struct nfs41_bind_conn_to_session_args args = {
7172 .client = clp,
7173 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7174 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007175 struct nfs41_bind_conn_to_session_res res;
7176 struct rpc_message msg = {
7177 .rpc_proc =
7178 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007179 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007180 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007181 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007182 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007183 struct rpc_task_setup task_setup_data = {
7184 .rpc_client = clnt,
7185 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007186 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007187 .rpc_message = &msg,
7188 .flags = RPC_TASK_TIMEOUT,
7189 };
7190 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007191
7192 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007193
Trond Myklebust71a097c2015-02-18 09:27:18 -08007194 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7195 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7196 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007197
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007198 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7199 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7200 args.dir = NFS4_CDFC4_FORE;
7201
7202 task = rpc_run_task(&task_setup_data);
7203 if (!IS_ERR(task)) {
7204 status = task->tk_status;
7205 rpc_put_task(task);
7206 } else
7207 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007208 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007209 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007210 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007211 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7212 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7213 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007214 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007215 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007216 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007217 dprintk("NFS: %s: Unexpected direction from server\n",
7218 __func__);
7219 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007220 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007221 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007222 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007223 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7224 __func__);
7225 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007226 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007227 }
7228 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007229out:
7230 dprintk("<-- %s status= %d\n", __func__, status);
7231 return status;
7232}
7233
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007234struct rpc_bind_conn_calldata {
7235 struct nfs_client *clp;
7236 struct rpc_cred *cred;
7237};
7238
7239static int
7240nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7241 struct rpc_xprt *xprt,
7242 void *calldata)
7243{
7244 struct rpc_bind_conn_calldata *p = calldata;
7245
7246 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7247}
7248
7249int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7250{
7251 struct rpc_bind_conn_calldata data = {
7252 .clp = clp,
7253 .cred = cred,
7254 };
7255 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7256 nfs4_proc_bind_conn_to_session_callback, &data);
7257}
7258
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007259/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007260 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7261 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007262 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007263static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7264 .how = SP4_MACH_CRED,
7265 .enforce.u.words = {
7266 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7267 1 << (OP_EXCHANGE_ID - 32) |
7268 1 << (OP_CREATE_SESSION - 32) |
7269 1 << (OP_DESTROY_SESSION - 32) |
7270 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007271 },
7272 .allow.u.words = {
7273 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007274 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007275 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007276 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007277 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007278 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007279 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007280 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007281 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007282 1 << (OP_FREE_STATEID - 32) |
7283 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007284 }
7285};
7286
7287/*
7288 * Select the state protection mode for client `clp' given the server results
7289 * from exchange_id in `sp'.
7290 *
7291 * Returns 0 on success, negative errno otherwise.
7292 */
7293static int nfs4_sp4_select_mode(struct nfs_client *clp,
7294 struct nfs41_state_protection *sp)
7295{
7296 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7297 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7298 1 << (OP_EXCHANGE_ID - 32) |
7299 1 << (OP_CREATE_SESSION - 32) |
7300 1 << (OP_DESTROY_SESSION - 32) |
7301 1 << (OP_DESTROY_CLIENTID - 32)
7302 };
7303 unsigned int i;
7304
7305 if (sp->how == SP4_MACH_CRED) {
7306 /* Print state protect result */
7307 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7308 for (i = 0; i <= LAST_NFS4_OP; i++) {
7309 if (test_bit(i, sp->enforce.u.longs))
7310 dfprintk(MOUNT, " enforce op %d\n", i);
7311 if (test_bit(i, sp->allow.u.longs))
7312 dfprintk(MOUNT, " allow op %d\n", i);
7313 }
7314
7315 /* make sure nothing is on enforce list that isn't supported */
7316 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7317 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7318 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7319 return -EINVAL;
7320 }
7321 }
7322
7323 /*
7324 * Minimal mode - state operations are allowed to use machine
7325 * credential. Note this already happens by default, so the
7326 * client doesn't have to do anything more than the negotiation.
7327 *
7328 * NOTE: we don't care if EXCHANGE_ID is in the list -
7329 * we're already using the machine cred for exchange_id
7330 * and will never use a different cred.
7331 */
7332 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7333 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7334 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7335 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7336 dfprintk(MOUNT, "sp4_mach_cred:\n");
7337 dfprintk(MOUNT, " minimal mode enabled\n");
7338 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7339 } else {
7340 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7341 return -EINVAL;
7342 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007343
7344 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007345 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7346 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007347 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7348 dfprintk(MOUNT, " cleanup mode enabled\n");
7349 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7350 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007351
Andrew Elble99ade3c2015-12-02 09:39:51 -05007352 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7353 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7354 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7355 &clp->cl_sp4_flags);
7356 }
7357
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007358 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7359 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7360 dfprintk(MOUNT, " secinfo mode enabled\n");
7361 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7362 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007363
7364 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7365 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7366 dfprintk(MOUNT, " stateid mode enabled\n");
7367 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7368 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007369
7370 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7371 dfprintk(MOUNT, " write mode enabled\n");
7372 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7373 }
7374
7375 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7376 dfprintk(MOUNT, " commit mode enabled\n");
7377 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7378 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007379 }
7380
7381 return 0;
7382}
7383
Andy Adamson8d89bd72016-09-09 09:22:18 -04007384struct nfs41_exchange_id_data {
7385 struct nfs41_exchange_id_res res;
7386 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007387 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007388 int rpc_status;
7389};
7390
7391static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007392{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007393 struct nfs41_exchange_id_data *cdata =
7394 (struct nfs41_exchange_id_data *)data;
7395 struct nfs_client *clp = cdata->args.client;
7396 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007397
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007398 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007399
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007400 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007401 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007402
7403 if (cdata->xprt && status == 0) {
7404 status = nfs4_detect_session_trunking(clp, &cdata->res,
7405 cdata->xprt);
7406 goto out;
7407 }
7408
Andy Adamson8d89bd72016-09-09 09:22:18 -04007409 if (status == 0)
7410 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007411
Chuck Lever177313f2012-05-21 22:44:58 -04007412 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007413 clp->cl_clientid = cdata->res.clientid;
7414 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007415 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007416 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007417 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007418 &clp->cl_session->session_state);
7419 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007420 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007421
Chuck Leveracdeb692012-05-21 22:46:16 -04007422 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007423 clp->cl_serverowner = cdata->res.server_owner;
7424 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007425
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007426 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007427 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007428 clp->cl_implid = cdata->res.impl_id;
7429 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007430
Chuck Lever177313f2012-05-21 22:44:58 -04007431 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007432 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007433 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007434 dprintk("%s: server_scope mismatch detected\n",
7435 __func__);
7436 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007437 kfree(clp->cl_serverscope);
7438 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007439 }
7440
Chuck Lever177313f2012-05-21 22:44:58 -04007441 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007442 clp->cl_serverscope = cdata->res.server_scope;
7443 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007444 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007445 /* Save the EXCHANGE_ID verifier session trunk tests */
7446 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7447 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007448 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007449out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007450 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007451 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007452}
7453
7454static void nfs4_exchange_id_release(void *data)
7455{
7456 struct nfs41_exchange_id_data *cdata =
7457 (struct nfs41_exchange_id_data *)data;
7458
7459 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007460 if (cdata->xprt) {
7461 xprt_put(cdata->xprt);
7462 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7463 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007464 kfree(cdata->res.impl_id);
7465 kfree(cdata->res.server_scope);
7466 kfree(cdata->res.server_owner);
7467 kfree(cdata);
7468}
7469
7470static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7471 .rpc_call_done = nfs4_exchange_id_done,
7472 .rpc_release = nfs4_exchange_id_release,
7473};
7474
Benny Halevy99fe60d2009-04-01 09:22:29 -04007475/*
7476 * _nfs4_proc_exchange_id()
7477 *
7478 * Wrapper for EXCHANGE_ID operation.
7479 */
7480static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007481 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007482{
7483 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007484 struct rpc_message msg = {
7485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007486 .rpc_cred = cred,
7487 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007488 struct rpc_task_setup task_setup_data = {
7489 .rpc_client = clp->cl_rpcclient,
7490 .callback_ops = &nfs4_exchange_id_call_ops,
7491 .rpc_message = &msg,
7492 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7493 };
7494 struct nfs41_exchange_id_data *calldata;
7495 struct rpc_task *task;
7496 int status = -EIO;
7497
7498 if (!atomic_inc_not_zero(&clp->cl_count))
7499 goto out;
7500
7501 status = -ENOMEM;
7502 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7503 if (!calldata)
7504 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007505
Andy Adamsonad0849a2016-09-09 09:22:28 -04007506 if (!xprt)
7507 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007508
7509 status = nfs4_init_uniform_client_string(clp);
7510 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007511 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007512
7513 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7514 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7515 clp->cl_owner_id);
7516
Andy Adamson8d89bd72016-09-09 09:22:18 -04007517 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7518 GFP_NOFS);
7519 status = -ENOMEM;
7520 if (unlikely(calldata->res.server_owner == NULL))
7521 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007522
Andy Adamson8d89bd72016-09-09 09:22:18 -04007523 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007524 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007525 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007526 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007527
Andy Adamson8d89bd72016-09-09 09:22:18 -04007528 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7529 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007530 goto out_server_scope;
7531
7532 switch (sp4_how) {
7533 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007534 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007535 break;
7536
7537 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007538 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007539 break;
7540
7541 default:
7542 /* unsupported! */
7543 WARN_ON_ONCE(1);
7544 status = -EINVAL;
7545 goto out_impl_id;
7546 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007547 if (xprt) {
7548 calldata->xprt = xprt;
7549 task_setup_data.rpc_xprt = xprt;
7550 task_setup_data.flags =
7551 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7552 calldata->args.verifier = &clp->cl_confirm;
7553 } else {
7554 calldata->args.verifier = &verifier;
7555 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007556 calldata->args.client = clp;
7557#ifdef CONFIG_NFS_V4_1_MIGRATION
7558 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7559 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7560 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7561#else
7562 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7563 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7564#endif
7565 msg.rpc_argp = &calldata->args;
7566 msg.rpc_resp = &calldata->res;
7567 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007568
Andy Adamson8d89bd72016-09-09 09:22:18 -04007569 task = rpc_run_task(&task_setup_data);
7570 if (IS_ERR(task)) {
7571 status = PTR_ERR(task);
7572 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007573 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007574
Andy Adamsonad0849a2016-09-09 09:22:28 -04007575 if (!xprt) {
7576 status = rpc_wait_for_completion_task(task);
7577 if (!status)
7578 status = calldata->rpc_status;
7579 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007580 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007581
Andy Adamson8d89bd72016-09-09 09:22:18 -04007582 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007583out:
Chuck Lever177313f2012-05-21 22:44:58 -04007584 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007585 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007586 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007587 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007588 clp->cl_implid->date.seconds,
7589 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007590 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007591 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007592
7593out_impl_id:
7594 kfree(calldata->res.impl_id);
7595out_server_scope:
7596 kfree(calldata->res.server_scope);
7597out_server_owner:
7598 kfree(calldata->res.server_owner);
7599out_calldata:
7600 kfree(calldata);
7601 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007602}
7603
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007604/*
7605 * nfs4_proc_exchange_id()
7606 *
7607 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7608 *
7609 * Since the clientid has expired, all compounds using sessions
7610 * associated with the stale clientid will be returning
7611 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7612 * be in some phase of session reset.
7613 *
7614 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7615 */
7616int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7617{
7618 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7619 int status;
7620
7621 /* try SP4_MACH_CRED if krb5i/p */
7622 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7623 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007624 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007625 if (!status)
7626 return 0;
7627 }
7628
7629 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007630 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007631}
7632
Andy Adamson04fa2c62016-09-09 09:22:29 -04007633/**
7634 * nfs4_test_session_trunk
7635 *
7636 * This is an add_xprt_test() test function called from
7637 * rpc_clnt_setup_test_and_add_xprt.
7638 *
7639 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7640 * and is dereferrenced in nfs4_exchange_id_release
7641 *
7642 * Upon success, add the new transport to the rpc_clnt
7643 *
7644 * @clnt: struct rpc_clnt to get new transport
7645 * @xprt: the rpc_xprt to test
7646 * @data: call data for _nfs4_proc_exchange_id.
7647 */
7648int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7649 void *data)
7650{
7651 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7652 u32 sp4_how;
7653
7654 dprintk("--> %s try %s\n", __func__,
7655 xprt->address_strings[RPC_DISPLAY_ADDR]);
7656
7657 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7658
7659 /* Test connection for session trunking. Async exchange_id call */
7660 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7661}
7662EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7663
Trond Myklebust66245532012-05-25 17:18:09 -04007664static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7665 struct rpc_cred *cred)
7666{
7667 struct rpc_message msg = {
7668 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7669 .rpc_argp = clp,
7670 .rpc_cred = cred,
7671 };
7672 int status;
7673
7674 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007675 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007676 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007677 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007678 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7679 return status;
7680}
7681
7682static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7683 struct rpc_cred *cred)
7684{
7685 unsigned int loop;
7686 int ret;
7687
7688 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7689 ret = _nfs4_proc_destroy_clientid(clp, cred);
7690 switch (ret) {
7691 case -NFS4ERR_DELAY:
7692 case -NFS4ERR_CLIENTID_BUSY:
7693 ssleep(1);
7694 break;
7695 default:
7696 return ret;
7697 }
7698 }
7699 return 0;
7700}
7701
7702int nfs4_destroy_clientid(struct nfs_client *clp)
7703{
7704 struct rpc_cred *cred;
7705 int ret = 0;
7706
7707 if (clp->cl_mvops->minor_version < 1)
7708 goto out;
7709 if (clp->cl_exchange_flags == 0)
7710 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007711 if (clp->cl_preserve_clid)
7712 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007713 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007714 ret = nfs4_proc_destroy_clientid(clp, cred);
7715 if (cred)
7716 put_rpccred(cred);
7717 switch (ret) {
7718 case 0:
7719 case -NFS4ERR_STALE_CLIENTID:
7720 clp->cl_exchange_flags = 0;
7721 }
7722out:
7723 return ret;
7724}
7725
Andy Adamson2050f0c2009-04-01 09:22:30 -04007726struct nfs4_get_lease_time_data {
7727 struct nfs4_get_lease_time_args *args;
7728 struct nfs4_get_lease_time_res *res;
7729 struct nfs_client *clp;
7730};
7731
7732static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7733 void *calldata)
7734{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007735 struct nfs4_get_lease_time_data *data =
7736 (struct nfs4_get_lease_time_data *)calldata;
7737
7738 dprintk("--> %s\n", __func__);
7739 /* just setup sequence, do not trigger session recovery
7740 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007741 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007742 &data->args->la_seq_args,
7743 &data->res->lr_seq_res,
7744 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007745 dprintk("<-- %s\n", __func__);
7746}
7747
7748/*
7749 * Called from nfs4_state_manager thread for session setup, so don't recover
7750 * from sequence operation or clientid errors.
7751 */
7752static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7753{
7754 struct nfs4_get_lease_time_data *data =
7755 (struct nfs4_get_lease_time_data *)calldata;
7756
7757 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007758 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7759 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007760 switch (task->tk_status) {
7761 case -NFS4ERR_DELAY:
7762 case -NFS4ERR_GRACE:
7763 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7764 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7765 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007766 /* fall through */
7767 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007768 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007769 return;
7770 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007771 dprintk("<-- %s\n", __func__);
7772}
7773
Trond Myklebust17280172012-03-11 13:11:00 -04007774static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007775 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7776 .rpc_call_done = nfs4_get_lease_time_done,
7777};
7778
7779int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7780{
7781 struct rpc_task *task;
7782 struct nfs4_get_lease_time_args args;
7783 struct nfs4_get_lease_time_res res = {
7784 .lr_fsinfo = fsinfo,
7785 };
7786 struct nfs4_get_lease_time_data data = {
7787 .args = &args,
7788 .res = &res,
7789 .clp = clp,
7790 };
7791 struct rpc_message msg = {
7792 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7793 .rpc_argp = &args,
7794 .rpc_resp = &res,
7795 };
7796 struct rpc_task_setup task_setup = {
7797 .rpc_client = clp->cl_rpcclient,
7798 .rpc_message = &msg,
7799 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007800 .callback_data = &data,
7801 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007802 };
7803 int status;
7804
Chuck Levera9c92d62013-08-09 12:48:18 -04007805 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007806 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007807 dprintk("--> %s\n", __func__);
7808 task = rpc_run_task(&task_setup);
7809
7810 if (IS_ERR(task))
7811 status = PTR_ERR(task);
7812 else {
7813 status = task->tk_status;
7814 rpc_put_task(task);
7815 }
7816 dprintk("<-- %s return %d\n", __func__, status);
7817
7818 return status;
7819}
7820
Andy Adamsonfc931582009-04-01 09:22:31 -04007821/*
7822 * Initialize the values to be used by the client in CREATE_SESSION
7823 * If nfs4_init_session set the fore channel request and response sizes,
7824 * use them.
7825 *
7826 * Set the back channel max_resp_sz_cached to zero to force the client to
7827 * always set csa_cachethis to FALSE because the current implementation
7828 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7829 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007830static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7831 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007832{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007833 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007834 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007835
Andy Adamson18aad3d2013-06-26 12:21:49 -04007836 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7837 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7838
Andy Adamsonfc931582009-04-01 09:22:31 -04007839 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007840 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7841 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007842 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007843 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007844
7845 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007846 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007847 __func__,
7848 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007849 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007850
7851 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007852 args->bc_attrs.max_rqst_sz = max_bc_payload;
7853 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007854 args->bc_attrs.max_resp_sz_cached = 0;
7855 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007856 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007857
7858 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7859 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7860 __func__,
7861 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7862 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7863 args->bc_attrs.max_reqs);
7864}
7865
Trond Myklebust79969dd2015-02-18 11:30:18 -08007866static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7867 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007868{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007869 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007870 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007871
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007872 if (rcvd->max_resp_sz > sent->max_resp_sz)
7873 return -EINVAL;
7874 /*
7875 * Our requested max_ops is the minimum we need; we're not
7876 * prepared to break up compounds into smaller pieces than that.
7877 * So, no point even trying to continue if the server won't
7878 * cooperate:
7879 */
7880 if (rcvd->max_ops < sent->max_ops)
7881 return -EINVAL;
7882 if (rcvd->max_reqs == 0)
7883 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007884 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7885 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007886 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007887}
7888
Trond Myklebust79969dd2015-02-18 11:30:18 -08007889static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7890 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007891{
7892 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007893 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007894
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007895 if (!(res->flags & SESSION4_BACK_CHAN))
7896 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007897 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7898 return -EINVAL;
7899 if (rcvd->max_resp_sz < sent->max_resp_sz)
7900 return -EINVAL;
7901 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7902 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007903 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007904 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007905 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007906 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007907out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007908 return 0;
7909}
Andy Adamson8d353012009-04-01 09:22:32 -04007910
Andy Adamson8d353012009-04-01 09:22:32 -04007911static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007912 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007913{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007914 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007915
Trond Myklebust79969dd2015-02-18 11:30:18 -08007916 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007917 if (ret)
7918 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007919 return nfs4_verify_back_channel_attrs(args, res);
7920}
7921
7922static void nfs4_update_session(struct nfs4_session *session,
7923 struct nfs41_create_session_res *res)
7924{
7925 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007926 /* Mark client id and session as being confirmed */
7927 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7928 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007929 session->flags = res->flags;
7930 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007931 if (res->flags & SESSION4_BACK_CHAN)
7932 memcpy(&session->bc_attrs, &res->bc_attrs,
7933 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007934}
7935
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007936static int _nfs4_proc_create_session(struct nfs_client *clp,
7937 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007938{
7939 struct nfs4_session *session = clp->cl_session;
7940 struct nfs41_create_session_args args = {
7941 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007942 .clientid = clp->cl_clientid,
7943 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007944 .cb_program = NFS4_CALLBACK,
7945 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007946 struct nfs41_create_session_res res;
7947
Andy Adamsonfc931582009-04-01 09:22:31 -04007948 struct rpc_message msg = {
7949 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7950 .rpc_argp = &args,
7951 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007952 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007953 };
7954 int status;
7955
Chuck Lever6b26cc82016-05-02 14:40:40 -04007956 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007957 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007958
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007959 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007960 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007961
Trond Myklebustb519d402016-09-11 14:50:01 -04007962 switch (status) {
7963 case -NFS4ERR_STALE_CLIENTID:
7964 case -NFS4ERR_DELAY:
7965 case -ETIMEDOUT:
7966 case -EACCES:
7967 case -EAGAIN:
7968 goto out;
7969 };
7970
7971 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007972 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007973 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007974 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007975 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007976 if (status)
7977 goto out;
7978 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007979 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007980out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007981 return status;
7982}
7983
7984/*
7985 * Issues a CREATE_SESSION operation to the server.
7986 * It is the responsibility of the caller to verify the session is
7987 * expired before calling this routine.
7988 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007989int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007990{
7991 int status;
7992 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007993 struct nfs4_session *session = clp->cl_session;
7994
7995 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7996
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007997 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007998 if (status)
7999 goto out;
8000
Andy Adamsonaacd5532011-11-09 13:58:21 -05008001 /* Init or reset the session slot tables */
8002 status = nfs4_setup_session_slot_tables(session);
8003 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008004 if (status)
8005 goto out;
8006
8007 ptr = (unsigned *)&session->sess_id.data[0];
8008 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8009 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008010out:
8011 dprintk("<-- %s\n", __func__);
8012 return status;
8013}
8014
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008015/*
8016 * Issue the over-the-wire RPC DESTROY_SESSION.
8017 * The caller must serialize access to this routine.
8018 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008019int nfs4_proc_destroy_session(struct nfs4_session *session,
8020 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008021{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008022 struct rpc_message msg = {
8023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8024 .rpc_argp = session,
8025 .rpc_cred = cred,
8026 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008027 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008028
8029 dprintk("--> nfs4_proc_destroy_session\n");
8030
8031 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008032 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8033 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008034
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008035 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008036 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008037
8038 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008039 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008040 "Session has been destroyed regardless...\n", status);
8041
8042 dprintk("<-- nfs4_proc_destroy_session\n");
8043 return status;
8044}
8045
Trond Myklebust7b38c362012-05-23 13:23:31 -04008046/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008047 * Renew the cl_session lease.
8048 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008049struct nfs4_sequence_data {
8050 struct nfs_client *clp;
8051 struct nfs4_sequence_args args;
8052 struct nfs4_sequence_res res;
8053};
8054
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008055static void nfs41_sequence_release(void *data)
8056{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008057 struct nfs4_sequence_data *calldata = data;
8058 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008059
Alexandros Batsakis71358402010-02-05 03:45:05 -08008060 if (atomic_read(&clp->cl_count) > 1)
8061 nfs4_schedule_state_renewal(clp);
8062 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008063 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008064}
8065
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008066static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8067{
8068 switch(task->tk_status) {
8069 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008070 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8071 return -EAGAIN;
8072 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008073 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008074 }
8075 return 0;
8076}
8077
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008078static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008079{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008080 struct nfs4_sequence_data *calldata = data;
8081 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008082
Trond Myklebust14516c32010-07-31 14:29:06 -04008083 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8084 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008085
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008086 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008087 if (task->tk_status < 0) {
8088 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008089 if (atomic_read(&clp->cl_count) == 1)
8090 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008091
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008092 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8093 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008094 return;
8095 }
8096 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008097 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008098out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008099 dprintk("<-- %s\n", __func__);
8100}
8101
8102static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8103{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008104 struct nfs4_sequence_data *calldata = data;
8105 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008106 struct nfs4_sequence_args *args;
8107 struct nfs4_sequence_res *res;
8108
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008109 args = task->tk_msg.rpc_argp;
8110 res = task->tk_msg.rpc_resp;
8111
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008112 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008113}
8114
8115static const struct rpc_call_ops nfs41_sequence_ops = {
8116 .rpc_call_done = nfs41_sequence_call_done,
8117 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008118 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008119};
8120
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008121static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8122 struct rpc_cred *cred,
8123 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008124{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008125 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008126 struct rpc_message msg = {
8127 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8128 .rpc_cred = cred,
8129 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008130 struct rpc_task_setup task_setup_data = {
8131 .rpc_client = clp->cl_rpcclient,
8132 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008133 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008134 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008135 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008136
Alexandros Batsakis71358402010-02-05 03:45:05 -08008137 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008138 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008139 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008140 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008141 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008142 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008143 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008144 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008145 if (is_privileged)
8146 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008147 msg.rpc_argp = &calldata->args;
8148 msg.rpc_resp = &calldata->res;
8149 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008150 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008151
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008152 return rpc_run_task(&task_setup_data);
8153}
8154
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008155static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008156{
8157 struct rpc_task *task;
8158 int ret = 0;
8159
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008160 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008161 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008162 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008163 if (IS_ERR(task))
8164 ret = PTR_ERR(task);
8165 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008166 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008167 dprintk("<-- %s status=%d\n", __func__, ret);
8168 return ret;
8169}
8170
8171static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8172{
8173 struct rpc_task *task;
8174 int ret;
8175
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008176 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008177 if (IS_ERR(task)) {
8178 ret = PTR_ERR(task);
8179 goto out;
8180 }
8181 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008182 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008183 ret = task->tk_status;
8184 rpc_put_task(task);
8185out:
8186 dprintk("<-- %s status=%d\n", __func__, ret);
8187 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008188}
8189
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008190struct nfs4_reclaim_complete_data {
8191 struct nfs_client *clp;
8192 struct nfs41_reclaim_complete_args arg;
8193 struct nfs41_reclaim_complete_res res;
8194};
8195
8196static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8197{
8198 struct nfs4_reclaim_complete_data *calldata = data;
8199
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008200 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008201 &calldata->arg.seq_args,
8202 &calldata->res.seq_res,
8203 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008204}
8205
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008206static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8207{
8208 switch(task->tk_status) {
8209 case 0:
8210 case -NFS4ERR_COMPLETE_ALREADY:
8211 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8212 break;
8213 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008214 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008215 /* fall through */
8216 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008217 return -EAGAIN;
8218 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008219 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008220 }
8221 return 0;
8222}
8223
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008224static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8225{
8226 struct nfs4_reclaim_complete_data *calldata = data;
8227 struct nfs_client *clp = calldata->clp;
8228 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8229
8230 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008231 if (!nfs41_sequence_done(task, res))
8232 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008233
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008234 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008235 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8236 rpc_restart_call_prepare(task);
8237 return;
8238 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008239 dprintk("<-- %s\n", __func__);
8240}
8241
8242static void nfs4_free_reclaim_complete_data(void *data)
8243{
8244 struct nfs4_reclaim_complete_data *calldata = data;
8245
8246 kfree(calldata);
8247}
8248
8249static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8250 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8251 .rpc_call_done = nfs4_reclaim_complete_done,
8252 .rpc_release = nfs4_free_reclaim_complete_data,
8253};
8254
8255/*
8256 * Issue a global reclaim complete.
8257 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008258static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8259 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008260{
8261 struct nfs4_reclaim_complete_data *calldata;
8262 struct rpc_task *task;
8263 struct rpc_message msg = {
8264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008265 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008266 };
8267 struct rpc_task_setup task_setup_data = {
8268 .rpc_client = clp->cl_rpcclient,
8269 .rpc_message = &msg,
8270 .callback_ops = &nfs4_reclaim_complete_call_ops,
8271 .flags = RPC_TASK_ASYNC,
8272 };
8273 int status = -ENOMEM;
8274
8275 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008276 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008277 if (calldata == NULL)
8278 goto out;
8279 calldata->clp = clp;
8280 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008281
Chuck Levera9c92d62013-08-09 12:48:18 -04008282 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008283 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008284 msg.rpc_argp = &calldata->arg;
8285 msg.rpc_resp = &calldata->res;
8286 task_setup_data.callback_data = calldata;
8287 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008288 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008289 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008290 goto out;
8291 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008292 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008293 if (status == 0)
8294 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008295 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008296 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008297out:
8298 dprintk("<-- %s status=%d\n", __func__, status);
8299 return status;
8300}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008301
8302static void
8303nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8304{
8305 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008306 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008307
8308 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008309 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008310 &lgp->res.seq_res, task);
8311 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008312}
8313
8314static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8315{
8316 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008317
8318 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008319 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008320 dprintk("<-- %s\n", __func__);
8321}
8322
8323static int
8324nfs4_layoutget_handle_exception(struct rpc_task *task,
8325 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8326{
Trond Myklebustee314c22012-10-01 17:25:48 -07008327 struct inode *inode = lgp->args.inode;
8328 struct nfs_server *server = NFS_SERVER(inode);
8329 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008330 int nfs4err = task->tk_status;
8331 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008332 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008333
Boaz Harroshed7e5422014-01-22 20:34:54 +02008334 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008335
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008336 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008337 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008338 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008339
8340 /*
8341 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8342 * on the file. set tk_status to -ENODATA to tell upper layer to
8343 * retry go inband.
8344 */
8345 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008346 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008347 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008348 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008349 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8350 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8351 */
8352 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008353 status = -EOVERFLOW;
8354 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008355 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008356 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008357 * (or clients) writing to the same RAID stripe except when
8358 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008359 *
8360 * Treat it like we would RECALLCONFLICT -- we retry for a little
8361 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008362 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008363 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008364 if (lgp->args.minlength == 0) {
8365 status = -EOVERFLOW;
8366 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008367 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008368 status = -EBUSY;
8369 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008370 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008371 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008372 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008373 case -NFS4ERR_DELEG_REVOKED:
8374 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008375 case -NFS4ERR_EXPIRED:
8376 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008377 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008378 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008379 lo = NFS_I(inode)->layout;
8380 /* If the open stateid was bad, then recover it. */
8381 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8382 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008383 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008384 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008385 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008386 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008387 break;
8388 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008389
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008390 /*
8391 * Mark the bad layout state as invalid, then retry
8392 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008393 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008394 spin_unlock(&inode->i_lock);
8395 pnfs_free_lseg_list(&head);
8396 status = -EAGAIN;
8397 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008398 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008399
Trond Myklebust8ac09252017-01-23 22:44:12 -05008400 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008401 err = nfs4_handle_exception(server, nfs4err, exception);
8402 if (!status) {
8403 if (exception->retry)
8404 status = -EAGAIN;
8405 else
8406 status = err;
8407 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008408out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008409 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008410 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008411}
8412
Idan Kedar85541162012-08-02 11:47:10 +03008413static size_t max_response_pages(struct nfs_server *server)
8414{
8415 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8416 return nfs_page_array_len(0, max_resp_sz);
8417}
8418
8419static void nfs4_free_pages(struct page **pages, size_t size)
8420{
8421 int i;
8422
8423 if (!pages)
8424 return;
8425
8426 for (i = 0; i < size; i++) {
8427 if (!pages[i])
8428 break;
8429 __free_page(pages[i]);
8430 }
8431 kfree(pages);
8432}
8433
8434static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8435{
8436 struct page **pages;
8437 int i;
8438
8439 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8440 if (!pages) {
8441 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8442 return NULL;
8443 }
8444
8445 for (i = 0; i < size; i++) {
8446 pages[i] = alloc_page(gfp_flags);
8447 if (!pages[i]) {
8448 dprintk("%s: failed to allocate page\n", __func__);
8449 nfs4_free_pages(pages, size);
8450 return NULL;
8451 }
8452 }
8453
8454 return pages;
8455}
8456
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008457static void nfs4_layoutget_release(void *calldata)
8458{
8459 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008460 struct inode *inode = lgp->args.inode;
8461 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008462 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008463
8464 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008465 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008466 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008467 put_nfs_open_context(lgp->args.ctx);
8468 kfree(calldata);
8469 dprintk("<-- %s\n", __func__);
8470}
8471
8472static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8473 .rpc_call_prepare = nfs4_layoutget_prepare,
8474 .rpc_call_done = nfs4_layoutget_done,
8475 .rpc_release = nfs4_layoutget_release,
8476};
8477
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008478struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008479nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008480{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008481 struct inode *inode = lgp->args.inode;
8482 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008483 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008484 struct rpc_task *task;
8485 struct rpc_message msg = {
8486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8487 .rpc_argp = &lgp->args,
8488 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008489 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008490 };
8491 struct rpc_task_setup task_setup_data = {
8492 .rpc_client = server->client,
8493 .rpc_message = &msg,
8494 .callback_ops = &nfs4_layoutget_call_ops,
8495 .callback_data = lgp,
8496 .flags = RPC_TASK_ASYNC,
8497 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008498 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008499 struct nfs4_exception exception = {
8500 .inode = inode,
8501 .timeout = *timeout,
8502 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008503 int status = 0;
8504
8505 dprintk("--> %s\n", __func__);
8506
Peng Tao4bd5a982014-11-17 11:05:17 +08008507 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8508 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8509
Idan Kedar85541162012-08-02 11:47:10 +03008510 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8511 if (!lgp->args.layout.pages) {
8512 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008513 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008514 }
8515 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8516
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008517 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008518 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008519 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008520
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008521 task = rpc_run_task(&task_setup_data);
8522 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008523 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008524 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008525 if (status == 0) {
8526 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8527 *timeout = exception.timeout;
8528 }
8529
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008530 trace_nfs4_layoutget(lgp->args.ctx,
8531 &lgp->args.range,
8532 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008533 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008534 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008535
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008536 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8537 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008538 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008539 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008540 rpc_put_task(task);
8541 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008542 if (status)
8543 return ERR_PTR(status);
8544 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008545}
8546
Benny Halevycbe82602011-05-22 19:52:37 +03008547static void
8548nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8549{
8550 struct nfs4_layoutreturn *lrp = calldata;
8551
8552 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008553 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008554 &lrp->args.seq_args,
8555 &lrp->res.seq_res,
8556 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008557}
8558
8559static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8560{
8561 struct nfs4_layoutreturn *lrp = calldata;
8562 struct nfs_server *server;
8563
8564 dprintk("--> %s\n", __func__);
8565
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008566 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008567 return;
8568
8569 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008570 switch (task->tk_status) {
8571 default:
8572 task->tk_status = 0;
8573 case 0:
8574 break;
8575 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008576 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008577 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008578 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008579 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008580 return;
8581 }
Benny Halevycbe82602011-05-22 19:52:37 +03008582 dprintk("<-- %s\n", __func__);
8583}
8584
8585static void nfs4_layoutreturn_release(void *calldata)
8586{
8587 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008588 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008589
8590 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008591 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008592 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008593 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008594 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8595 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008596 pnfs_put_layout_hdr(lrp->args.layout);
8597 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008598 kfree(calldata);
8599 dprintk("<-- %s\n", __func__);
8600}
8601
8602static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8603 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8604 .rpc_call_done = nfs4_layoutreturn_done,
8605 .rpc_release = nfs4_layoutreturn_release,
8606};
8607
Peng Tao6c166052014-11-17 09:30:40 +08008608int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008609{
8610 struct rpc_task *task;
8611 struct rpc_message msg = {
8612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8613 .rpc_argp = &lrp->args,
8614 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008615 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008616 };
8617 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008618 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008619 .rpc_message = &msg,
8620 .callback_ops = &nfs4_layoutreturn_call_ops,
8621 .callback_data = lrp,
8622 };
Peng Tao6c166052014-11-17 09:30:40 +08008623 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008624
Andrew Elble99ade3c2015-12-02 09:39:51 -05008625 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8626 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8627 &task_setup_data.rpc_client, &msg);
8628
Benny Halevycbe82602011-05-22 19:52:37 +03008629 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008630 if (!sync) {
8631 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8632 if (!lrp->inode) {
8633 nfs4_layoutreturn_release(lrp);
8634 return -EAGAIN;
8635 }
8636 task_setup_data.flags |= RPC_TASK_ASYNC;
8637 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008638 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008639 task = rpc_run_task(&task_setup_data);
8640 if (IS_ERR(task))
8641 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008642 if (sync)
8643 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008644 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008645 dprintk("<-- %s status=%d\n", __func__, status);
8646 rpc_put_task(task);
8647 return status;
8648}
8649
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008650static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008651_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8652 struct pnfs_device *pdev,
8653 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008654{
8655 struct nfs4_getdeviceinfo_args args = {
8656 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008657 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8658 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008659 };
8660 struct nfs4_getdeviceinfo_res res = {
8661 .pdev = pdev,
8662 };
8663 struct rpc_message msg = {
8664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8665 .rpc_argp = &args,
8666 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008667 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008668 };
8669 int status;
8670
8671 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008672 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008673 if (res.notification & ~args.notify_types)
8674 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008675 if (res.notification != args.notify_types)
8676 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008677
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008678 dprintk("<-- %s status=%d\n", __func__, status);
8679
8680 return status;
8681}
8682
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008683int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8684 struct pnfs_device *pdev,
8685 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008686{
8687 struct nfs4_exception exception = { };
8688 int err;
8689
8690 do {
8691 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008692 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008693 &exception);
8694 } while (exception.retry);
8695 return err;
8696}
8697EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8698
Andy Adamson863a3c62011-03-23 13:27:54 +00008699static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8700{
8701 struct nfs4_layoutcommit_data *data = calldata;
8702 struct nfs_server *server = NFS_SERVER(data->args.inode);
8703
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008704 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008705 &data->args.seq_args,
8706 &data->res.seq_res,
8707 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008708}
8709
8710static void
8711nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8712{
8713 struct nfs4_layoutcommit_data *data = calldata;
8714 struct nfs_server *server = NFS_SERVER(data->args.inode);
8715
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008716 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008717 return;
8718
8719 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008720 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8721 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8722 case -NFS4ERR_BADLAYOUT: /* no layout */
8723 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008724 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008725 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008726 break;
8727 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008728 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008729 rpc_restart_call_prepare(task);
8730 return;
8731 }
8732 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008733}
8734
8735static void nfs4_layoutcommit_release(void *calldata)
8736{
8737 struct nfs4_layoutcommit_data *data = calldata;
8738
Andy Adamsondb29c082011-07-30 20:52:38 -04008739 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008740 nfs_post_op_update_inode_force_wcc(data->args.inode,
8741 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008742 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008743 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008744 kfree(data);
8745}
8746
8747static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8748 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8749 .rpc_call_done = nfs4_layoutcommit_done,
8750 .rpc_release = nfs4_layoutcommit_release,
8751};
8752
8753int
Andy Adamsonef311532011-03-12 02:58:10 -05008754nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008755{
8756 struct rpc_message msg = {
8757 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8758 .rpc_argp = &data->args,
8759 .rpc_resp = &data->res,
8760 .rpc_cred = data->cred,
8761 };
8762 struct rpc_task_setup task_setup_data = {
8763 .task = &data->task,
8764 .rpc_client = NFS_CLIENT(data->args.inode),
8765 .rpc_message = &msg,
8766 .callback_ops = &nfs4_layoutcommit_ops,
8767 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008768 };
8769 struct rpc_task *task;
8770 int status = 0;
8771
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008772 dprintk("NFS: initiating layoutcommit call. sync %d "
8773 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008774 data->args.lastbytewritten,
8775 data->args.inode->i_ino);
8776
Trond Myklebust472e2592015-02-05 16:50:30 -05008777 if (!sync) {
8778 data->inode = nfs_igrab_and_active(data->args.inode);
8779 if (data->inode == NULL) {
8780 nfs4_layoutcommit_release(data);
8781 return -EAGAIN;
8782 }
8783 task_setup_data.flags = RPC_TASK_ASYNC;
8784 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008785 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008786 task = rpc_run_task(&task_setup_data);
8787 if (IS_ERR(task))
8788 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008789 if (sync)
8790 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008791 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008792 dprintk("%s: status %d\n", __func__, status);
8793 rpc_put_task(task);
8794 return status;
8795}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008796
Andy Adamson97431202013-08-08 10:57:56 -04008797/**
8798 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8799 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8800 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008801static int
8802_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008803 struct nfs_fsinfo *info,
8804 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008805{
8806 struct nfs41_secinfo_no_name_args args = {
8807 .style = SECINFO_STYLE_CURRENT_FH,
8808 };
8809 struct nfs4_secinfo_res res = {
8810 .flavors = flavors,
8811 };
8812 struct rpc_message msg = {
8813 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8814 .rpc_argp = &args,
8815 .rpc_resp = &res,
8816 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008817 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008818 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008819 int status;
8820
8821 if (use_integrity) {
8822 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008823 cred = nfs4_get_clid_cred(server->nfs_client);
8824 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008825 }
8826
8827 dprintk("--> %s\n", __func__);
8828 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8829 &res.seq_res, 0);
8830 dprintk("<-- %s status=%d\n", __func__, status);
8831
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008832 if (cred)
8833 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008834
8835 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008836}
8837
8838static int
8839nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8840 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8841{
8842 struct nfs4_exception exception = { };
8843 int err;
8844 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008845 /* first try using integrity protection */
8846 err = -NFS4ERR_WRONGSEC;
8847
8848 /* try to use integrity protection with machine cred */
8849 if (_nfs4_is_integrity_protected(server->nfs_client))
8850 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8851 flavors, true);
8852
8853 /*
8854 * if unable to use integrity protection, or SECINFO with
8855 * integrity protection returns NFS4ERR_WRONGSEC (which is
8856 * disallowed by spec, but exists in deployed servers) use
8857 * the current filesystem's rpc_client and the user cred.
8858 */
8859 if (err == -NFS4ERR_WRONGSEC)
8860 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8861 flavors, false);
8862
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008863 switch (err) {
8864 case 0:
8865 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008866 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008867 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008868 default:
8869 err = nfs4_handle_exception(server, err, &exception);
8870 }
8871 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008872out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008873 return err;
8874}
8875
8876static int
8877nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8878 struct nfs_fsinfo *info)
8879{
8880 int err;
8881 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008882 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008883 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008884 struct nfs4_secinfo4 *secinfo;
8885 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008886
8887 page = alloc_page(GFP_KERNEL);
8888 if (!page) {
8889 err = -ENOMEM;
8890 goto out;
8891 }
8892
8893 flavors = page_address(page);
8894 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8895
8896 /*
8897 * Fall back on "guess and check" method if
8898 * the server doesn't support SECINFO_NO_NAME
8899 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008900 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008901 err = nfs4_find_root_sec(server, fhandle, info);
8902 goto out_freepage;
8903 }
8904 if (err)
8905 goto out_freepage;
8906
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008907 for (i = 0; i < flavors->num_flavors; i++) {
8908 secinfo = &flavors->flavors[i];
8909
8910 switch (secinfo->flavor) {
8911 case RPC_AUTH_NULL:
8912 case RPC_AUTH_UNIX:
8913 case RPC_AUTH_GSS:
8914 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8915 &secinfo->flavor_info);
8916 break;
8917 default:
8918 flavor = RPC_AUTH_MAXFLAVOR;
8919 break;
8920 }
8921
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008922 if (!nfs_auth_info_match(&server->auth_info, flavor))
8923 flavor = RPC_AUTH_MAXFLAVOR;
8924
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008925 if (flavor != RPC_AUTH_MAXFLAVOR) {
8926 err = nfs4_lookup_root_sec(server, fhandle,
8927 info, flavor);
8928 if (!err)
8929 break;
8930 }
8931 }
8932
8933 if (flavor == RPC_AUTH_MAXFLAVOR)
8934 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008935
8936out_freepage:
8937 put_page(page);
8938 if (err == -EACCES)
8939 return -EPERM;
8940out:
8941 return err;
8942}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008943
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008944static int _nfs41_test_stateid(struct nfs_server *server,
8945 nfs4_stateid *stateid,
8946 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008947{
8948 int status;
8949 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008950 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008951 };
8952 struct nfs41_test_stateid_res res;
8953 struct rpc_message msg = {
8954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8955 .rpc_argp = &args,
8956 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008957 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008958 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008959 struct rpc_clnt *rpc_client = server->client;
8960
8961 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8962 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008963
Chuck Lever38527b12012-07-11 16:30:23 -04008964 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008965 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008966 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008967 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008968 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008969 if (status != NFS_OK) {
8970 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008971 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008972 }
8973 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008974 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008975}
8976
Trond Myklebust43912bb2016-09-22 13:38:56 -04008977static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8978 int err, struct nfs4_exception *exception)
8979{
8980 exception->retry = 0;
8981 switch(err) {
8982 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008983 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008984 nfs4_handle_exception(server, err, exception);
8985 break;
8986 case -NFS4ERR_BADSESSION:
8987 case -NFS4ERR_BADSLOT:
8988 case -NFS4ERR_BAD_HIGH_SLOT:
8989 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8990 case -NFS4ERR_DEADSESSION:
8991 nfs4_do_handle_exception(server, err, exception);
8992 }
8993}
8994
Chuck Lever38527b12012-07-11 16:30:23 -04008995/**
8996 * nfs41_test_stateid - perform a TEST_STATEID operation
8997 *
8998 * @server: server / transport on which to perform the operation
8999 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009000 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009001 *
9002 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9003 * Otherwise a negative NFS4ERR value is returned if the operation
9004 * failed or the state ID is not currently valid.
9005 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009006static int nfs41_test_stateid(struct nfs_server *server,
9007 nfs4_stateid *stateid,
9008 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009009{
9010 struct nfs4_exception exception = { };
9011 int err;
9012 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009013 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009014 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009015 } while (exception.retry);
9016 return err;
9017}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009018
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009019struct nfs_free_stateid_data {
9020 struct nfs_server *server;
9021 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009022 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009023};
9024
9025static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9026{
9027 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009028 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009029 &data->args.seq_args,
9030 &data->res.seq_res,
9031 task);
9032}
9033
9034static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9035{
9036 struct nfs_free_stateid_data *data = calldata;
9037
9038 nfs41_sequence_done(task, &data->res.seq_res);
9039
9040 switch (task->tk_status) {
9041 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009042 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009043 rpc_restart_call_prepare(task);
9044 }
9045}
9046
9047static void nfs41_free_stateid_release(void *calldata)
9048{
9049 kfree(calldata);
9050}
9051
Trond Myklebust17f26b12013-08-21 15:48:42 -04009052static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009053 .rpc_call_prepare = nfs41_free_stateid_prepare,
9054 .rpc_call_done = nfs41_free_stateid_done,
9055 .rpc_release = nfs41_free_stateid_release,
9056};
9057
9058static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009059 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009060 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009061 bool privileged)
9062{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009063 struct rpc_message msg = {
9064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009065 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009066 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009067 struct rpc_task_setup task_setup = {
9068 .rpc_client = server->client,
9069 .rpc_message = &msg,
9070 .callback_ops = &nfs41_free_stateid_ops,
9071 .flags = RPC_TASK_ASYNC,
9072 };
9073 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009074
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009075 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9076 &task_setup.rpc_client, &msg);
9077
Chuck Lever38527b12012-07-11 16:30:23 -04009078 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009079 data = kmalloc(sizeof(*data), GFP_NOFS);
9080 if (!data)
9081 return ERR_PTR(-ENOMEM);
9082 data->server = server;
9083 nfs4_stateid_copy(&data->args.stateid, stateid);
9084
9085 task_setup.callback_data = data;
9086
9087 msg.rpc_argp = &data->args;
9088 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009089 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009090 if (privileged)
9091 nfs4_set_sequence_privileged(&data->args.seq_args);
9092
9093 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009094}
9095
Chuck Lever38527b12012-07-11 16:30:23 -04009096/**
9097 * nfs41_free_stateid - perform a FREE_STATEID operation
9098 *
9099 * @server: server / transport on which to perform the operation
9100 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009101 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009102 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009103 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009104 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009105 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009106static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009107 const nfs4_stateid *stateid,
9108 struct rpc_cred *cred,
9109 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009110{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009111 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009112
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009113 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009114 if (IS_ERR(task))
9115 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009116 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009117 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009118}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009119
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009120static void
9121nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009122{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009123 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009124
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009125 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009126 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009127}
9128
Trond Myklebust36281ca2012-03-04 18:13:56 -05009129static bool nfs41_match_stateid(const nfs4_stateid *s1,
9130 const nfs4_stateid *s2)
9131{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009132 if (s1->type != s2->type)
9133 return false;
9134
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009135 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009136 return false;
9137
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009138 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009139 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009140 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009141 return true;
9142
9143 return false;
9144}
9145
Andy Adamson557134a2009-04-01 09:21:53 -04009146#endif /* CONFIG_NFS_V4_1 */
9147
Trond Myklebust36281ca2012-03-04 18:13:56 -05009148static bool nfs4_match_stateid(const nfs4_stateid *s1,
9149 const nfs4_stateid *s2)
9150{
Trond Myklebustf597c532012-03-04 18:13:56 -05009151 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009152}
9153
9154
Trond Myklebust17280172012-03-11 13:11:00 -04009155static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009156 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009157 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009158 .recover_open = nfs4_open_reclaim,
9159 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009160 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009161 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009162};
9163
Andy Adamson591d71c2009-04-01 09:22:47 -04009164#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009165static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009166 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9167 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9168 .recover_open = nfs4_open_reclaim,
9169 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009170 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009171 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009172 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009173};
9174#endif /* CONFIG_NFS_V4_1 */
9175
Trond Myklebust17280172012-03-11 13:11:00 -04009176static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009177 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009178 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009179 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009180 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009181 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009182};
9183
Andy Adamson591d71c2009-04-01 09:22:47 -04009184#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009185static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009186 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9187 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009188 .recover_open = nfs41_open_expired,
9189 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009190 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009191};
9192#endif /* CONFIG_NFS_V4_1 */
9193
Trond Myklebust17280172012-03-11 13:11:00 -04009194static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009195 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009196 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009197 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009198};
9199
9200#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009201static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009202 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009203 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009204 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009205};
9206#endif
9207
Chuck Leverec011fe2013-10-17 14:12:39 -04009208static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009209 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009210 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009211};
9212
9213#if defined(CONFIG_NFS_V4_1)
9214static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009215 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009216 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009217};
9218#endif /* CONFIG_NFS_V4_1 */
9219
Trond Myklebust97dc1352010-06-16 09:52:26 -04009220static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9221 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009222 .init_caps = NFS_CAP_READDIRPLUS
9223 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009224 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009225 .init_client = nfs40_init_client,
9226 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009227 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009228 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009229 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009230 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009231 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009232 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009233 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9234 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9235 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009236 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009237};
9238
9239#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009240static struct nfs_seqid *
9241nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9242{
9243 return NULL;
9244}
9245
Trond Myklebust97dc1352010-06-16 09:52:26 -04009246static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9247 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009248 .init_caps = NFS_CAP_READDIRPLUS
9249 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009250 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009251 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009252 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009253 .init_client = nfs41_init_client,
9254 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009255 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009256 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009257 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009258 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009259 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009260 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009261 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009262 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9263 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9264 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009265 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009266};
9267#endif
9268
Steve Dickson42c2c422013-05-22 12:50:38 -04009269#if defined(CONFIG_NFS_V4_2)
9270static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9271 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009272 .init_caps = NFS_CAP_READDIRPLUS
9273 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009274 | NFS_CAP_POSIX_LOCK
9275 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009276 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009277 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009278 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009279 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009280 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009281 | NFS_CAP_LAYOUTSTATS
9282 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009283 .init_client = nfs41_init_client,
9284 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009285 .match_stateid = nfs41_match_stateid,
9286 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009287 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009288 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009289 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009290 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009291 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009292 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9293 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9294 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009295 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009296};
9297#endif
9298
Trond Myklebust97dc1352010-06-16 09:52:26 -04009299const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9300 [0] = &nfs_v4_0_minor_ops,
9301#if defined(CONFIG_NFS_V4_1)
9302 [1] = &nfs_v4_1_minor_ops,
9303#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009304#if defined(CONFIG_NFS_V4_2)
9305 [2] = &nfs_v4_2_minor_ops,
9306#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009307};
9308
Trond Myklebust13997822016-07-24 17:10:52 -04009309static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009310{
9311 ssize_t error, error2;
9312
9313 error = generic_listxattr(dentry, list, size);
9314 if (error < 0)
9315 return error;
9316 if (list) {
9317 list += error;
9318 size -= error;
9319 }
9320
9321 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9322 if (error2 < 0)
9323 return error2;
9324 return error + error2;
9325}
9326
Trond Myklebust17f26b12013-08-21 15:48:42 -04009327static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009328 .create = nfs_create,
9329 .lookup = nfs_lookup,
9330 .atomic_open = nfs_atomic_open,
9331 .link = nfs_link,
9332 .unlink = nfs_unlink,
9333 .symlink = nfs_symlink,
9334 .mkdir = nfs_mkdir,
9335 .rmdir = nfs_rmdir,
9336 .mknod = nfs_mknod,
9337 .rename = nfs_rename,
9338 .permission = nfs_permission,
9339 .getattr = nfs_getattr,
9340 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009341 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009342};
9343
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009344static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009345 .permission = nfs_permission,
9346 .getattr = nfs_getattr,
9347 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009348 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009349};
9350
David Howells509de812006-08-22 20:06:11 -04009351const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009352 .version = 4, /* protocol version */
9353 .dentry_ops = &nfs4_dentry_operations,
9354 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009355 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009356 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009357 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009358 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009359 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009360 .getattr = nfs4_proc_getattr,
9361 .setattr = nfs4_proc_setattr,
9362 .lookup = nfs4_proc_lookup,
9363 .access = nfs4_proc_access,
9364 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009365 .create = nfs4_proc_create,
9366 .remove = nfs4_proc_remove,
9367 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009368 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009369 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009370 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009371 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009372 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009373 .link = nfs4_proc_link,
9374 .symlink = nfs4_proc_symlink,
9375 .mkdir = nfs4_proc_mkdir,
9376 .rmdir = nfs4_proc_remove,
9377 .readdir = nfs4_proc_readdir,
9378 .mknod = nfs4_proc_mknod,
9379 .statfs = nfs4_proc_statfs,
9380 .fsinfo = nfs4_proc_fsinfo,
9381 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009382 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009383 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009384 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009385 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009386 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009387 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009388 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009389 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009390 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009391 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009392 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009393 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009394 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009395 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009396 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009397 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009398 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009399 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009400 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009401 .create_server = nfs4_create_server,
9402 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009403};
9404
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009405static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009406 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009407 .list = nfs4_xattr_list_nfs4_acl,
9408 .get = nfs4_xattr_get_nfs4_acl,
9409 .set = nfs4_xattr_set_nfs4_acl,
9410};
9411
9412const struct xattr_handler *nfs4_xattr_handlers[] = {
9413 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009414#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9415 &nfs4_xattr_nfs4_label_handler,
9416#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009417 NULL
9418};
9419
Linus Torvalds1da177e2005-04-16 15:20:36 -07009420/*
9421 * Local variables:
9422 * c-basic-offset: 8
9423 * End:
9424 */