blob: baf9689904752197cd5d092a01b6d87cbd5447d5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Trond Myklebust4ce79712005-06-22 17:16:21 +000058#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050060#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050061#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040062#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040063#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040064#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040065#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020076/* file attributes which can be mapped to nfs attributes */
77#define NFS4_VALID_ATTRS (ATTR_MODE \
78 | ATTR_UID \
79 | ATTR_GID \
80 | ATTR_SIZE \
81 | ATTR_ATIME \
82 | ATTR_MTIME \
83 | ATTR_CTIME \
84 | ATTR_ATIME_SET \
85 | ATTR_MTIME_SET)
86
Trond Myklebustcdd4e682006-01-03 09:55:12 +010087struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010088static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080089static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050091static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040092static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
93static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040094static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
95 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110096 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040097 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040098#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040099static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
100 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400101static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
102 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400103#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400104
105#ifdef CONFIG_NFS_V4_SECURITY_LABEL
106static inline struct nfs4_label *
107nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
108 struct iattr *sattr, struct nfs4_label *label)
109{
110 int err;
111
112 if (label == NULL)
113 return NULL;
114
115 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
116 return NULL;
117
David Quigleyaa9c2662013-05-22 12:50:44 -0400118 err = security_dentry_init_security(dentry, sattr->ia_mode,
119 &dentry->d_name, (void **)&label->label, &label->len);
120 if (err == 0)
121 return label;
122
123 return NULL;
124}
125static inline void
126nfs4_label_release_security(struct nfs4_label *label)
127{
128 if (label)
129 security_release_secctx(label->label, label->len);
130}
131static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
132{
133 if (label)
134 return server->attr_bitmask;
135
136 return server->attr_bitmask_nl;
137}
138#else
139static inline struct nfs4_label *
140nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
141 struct iattr *sattr, struct nfs4_label *l)
142{ return NULL; }
143static inline void
144nfs4_label_release_security(struct nfs4_label *label)
145{ return; }
146static inline u32 *
147nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
148{ return server->attr_bitmask; }
149#endif
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500152static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Trond Myklebust52567b02009-10-23 14:46:42 -0400154 if (err >= -1000)
155 return err;
156 switch (err) {
157 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500158 case -NFS4ERR_LAYOUTTRYLATER:
159 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400160 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000161 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400162 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000163 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800164 case -NFS4ERR_BADOWNER:
165 case -NFS4ERR_BADNAME:
166 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400167 case -NFS4ERR_SHARE_DENIED:
168 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400169 case -NFS4ERR_MINOR_VERS_MISMATCH:
170 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400171 case -NFS4ERR_FILE_OPEN:
172 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400173 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700175 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400176 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400178 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181/*
182 * This is our standard bitmap for GETATTR requests.
183 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400184const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 FATTR4_WORD0_TYPE
186 | FATTR4_WORD0_CHANGE
187 | FATTR4_WORD0_SIZE
188 | FATTR4_WORD0_FSID
189 | FATTR4_WORD0_FILEID,
190 FATTR4_WORD1_MODE
191 | FATTR4_WORD1_NUMLINKS
192 | FATTR4_WORD1_OWNER
193 | FATTR4_WORD1_OWNER_GROUP
194 | FATTR4_WORD1_RAWDEV
195 | FATTR4_WORD1_SPACE_USED
196 | FATTR4_WORD1_TIME_ACCESS
197 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400198 | FATTR4_WORD1_TIME_MODIFY
199 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400200#ifdef CONFIG_NFS_V4_SECURITY_LABEL
201 FATTR4_WORD2_SECURITY_LABEL
202#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203};
204
Trond Myklebust1549210f2012-06-05 09:16:47 -0400205static const u32 nfs4_pnfs_open_bitmap[3] = {
206 FATTR4_WORD0_TYPE
207 | FATTR4_WORD0_CHANGE
208 | FATTR4_WORD0_SIZE
209 | FATTR4_WORD0_FSID
210 | FATTR4_WORD0_FILEID,
211 FATTR4_WORD1_MODE
212 | FATTR4_WORD1_NUMLINKS
213 | FATTR4_WORD1_OWNER
214 | FATTR4_WORD1_OWNER_GROUP
215 | FATTR4_WORD1_RAWDEV
216 | FATTR4_WORD1_SPACE_USED
217 | FATTR4_WORD1_TIME_ACCESS
218 | FATTR4_WORD1_TIME_METADATA
219 | FATTR4_WORD1_TIME_MODIFY,
220 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500221#ifdef CONFIG_NFS_V4_SECURITY_LABEL
222 | FATTR4_WORD2_SECURITY_LABEL
223#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400224};
225
Andy Adamsone23008e2012-10-02 21:07:32 -0400226static const u32 nfs4_open_noattr_bitmap[3] = {
227 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400228 | FATTR4_WORD0_FILEID,
229};
230
David Quigleya09df2c2013-05-22 12:50:41 -0400231const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 FATTR4_WORD0_FILES_AVAIL
233 | FATTR4_WORD0_FILES_FREE
234 | FATTR4_WORD0_FILES_TOTAL,
235 FATTR4_WORD1_SPACE_AVAIL
236 | FATTR4_WORD1_SPACE_FREE
237 | FATTR4_WORD1_SPACE_TOTAL
238};
239
David Quigleya09df2c2013-05-22 12:50:41 -0400240const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 FATTR4_WORD0_MAXLINK
242 | FATTR4_WORD0_MAXNAME,
243 0
244};
245
Fred Isamandae100c2011-07-30 20:52:37 -0400246const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 | FATTR4_WORD0_MAXREAD
248 | FATTR4_WORD0_MAXWRITE
249 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700250 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400251 | FATTR4_WORD1_FS_LAYOUT_TYPES,
252 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800253 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254};
255
David Quigleya09df2c2013-05-22 12:50:41 -0400256const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400257 FATTR4_WORD0_TYPE
258 | FATTR4_WORD0_CHANGE
259 | FATTR4_WORD0_SIZE
260 | FATTR4_WORD0_FSID
261 | FATTR4_WORD0_FILEID
262 | FATTR4_WORD0_FS_LOCATIONS,
263 FATTR4_WORD1_MODE
264 | FATTR4_WORD1_NUMLINKS
265 | FATTR4_WORD1_OWNER
266 | FATTR4_WORD1_OWNER_GROUP
267 | FATTR4_WORD1_RAWDEV
268 | FATTR4_WORD1_SPACE_USED
269 | FATTR4_WORD1_TIME_ACCESS
270 | FATTR4_WORD1_TIME_METADATA
271 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400272 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400273};
274
Al Virobc4785c2006-10-19 23:28:51 -0700275static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 struct nfs4_readdir_arg *readdir)
277{
Al Viro0dbb4c62006-10-19 23:28:49 -0700278 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000281 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
283 return;
284 }
285
286 readdir->cookie = 0;
287 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
288 if (cookie == 2)
289 return;
290
291 /*
292 * NFSv4 servers do not return entries for '.' and '..'
293 * Therefore, we fake these entries here. We let '.'
294 * have cookie 0 and '..' have cookie 1. Note that
295 * when talking to the server, we always send cookie 0
296 * instead of 1 or 2.
297 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800298 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 if (cookie == 0) {
301 *p++ = xdr_one; /* next */
302 *p++ = xdr_zero; /* cookie, first word */
303 *p++ = xdr_one; /* cookie, second word */
304 *p++ = xdr_one; /* entry len */
305 memcpy(p, ".\0\0\0", 4); /* entry */
306 p++;
307 *p++ = xdr_one; /* bitmap length */
308 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
309 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000310 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312
313 *p++ = xdr_one; /* next */
314 *p++ = xdr_zero; /* cookie, first word */
315 *p++ = xdr_two; /* cookie, second word */
316 *p++ = xdr_two; /* entry len */
317 memcpy(p, "..\0\0", 4); /* entry */
318 p++;
319 *p++ = xdr_one; /* bitmap length */
320 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
321 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000322 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 readdir->pgbase = (char *)p - (char *)start;
325 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800326 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Trond Myklebust26d36302016-09-22 13:39:05 -0400329static void nfs4_test_and_free_stateid(struct nfs_server *server,
330 nfs4_stateid *stateid,
331 struct rpc_cred *cred)
332{
333 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
334
335 ops->test_and_free_expired(server, stateid, cred);
336}
337
338static void __nfs4_free_revoked_stateid(struct nfs_server *server,
339 nfs4_stateid *stateid,
340 struct rpc_cred *cred)
341{
342 stateid->type = NFS4_REVOKED_STATEID_TYPE;
343 nfs4_test_and_free_stateid(server, stateid, cred);
344}
345
346static void nfs4_free_revoked_stateid(struct nfs_server *server,
347 const nfs4_stateid *stateid,
348 struct rpc_cred *cred)
349{
350 nfs4_stateid tmp;
351
352 nfs4_stateid_copy(&tmp, stateid);
353 __nfs4_free_revoked_stateid(server, &tmp, cred);
354}
355
NeilBrown8478eaa2014-09-18 16:09:27 +1000356static long nfs4_update_delay(long *timeout)
357{
358 long ret;
359 if (!timeout)
360 return NFS4_POLL_RETRY_MAX;
361 if (*timeout <= 0)
362 *timeout = NFS4_POLL_RETRY_MIN;
363 if (*timeout > NFS4_POLL_RETRY_MAX)
364 *timeout = NFS4_POLL_RETRY_MAX;
365 ret = *timeout;
366 *timeout <<= 1;
367 return ret;
368}
369
Trond Myklebust65de8722008-12-23 15:21:44 -0500370static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
371{
372 int res = 0;
373
374 might_sleep();
375
NeilBrown8478eaa2014-09-18 16:09:27 +1000376 freezable_schedule_timeout_killable_unsafe(
377 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500378 if (fatal_signal_pending(current))
379 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500380 return res;
381}
382
383/* This is the error handling routine for processes that are allowed
384 * to sleep.
385 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400386static int nfs4_do_handle_exception(struct nfs_server *server,
387 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500388{
389 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500390 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400391 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500392 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500393 int ret = errorcode;
394
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400395 exception->delay = 0;
396 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500397 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400398
399 if (stateid == NULL && state != NULL)
400 stateid = &state->stateid;
401
Trond Myklebust65de8722008-12-23 15:21:44 -0500402 switch(errorcode) {
403 case 0:
404 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400405 case -NFS4ERR_DELEG_REVOKED:
406 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400407 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400408 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400409 if (inode != NULL && stateid != NULL) {
410 nfs_inode_find_state_and_recover(inode,
411 stateid);
412 goto wait_on_recovery;
413 }
414 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400415 if (inode) {
416 int err;
417
418 err = nfs_async_inode_return_delegation(inode,
419 stateid);
420 if (err == 0)
421 goto wait_on_recovery;
422 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
423 exception->retry = 1;
424 break;
425 }
426 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500427 if (state == NULL)
428 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400429 ret = nfs4_schedule_stateid_recovery(server, state);
430 if (ret < 0)
431 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500432 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500433 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500434 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500435 nfs4_schedule_lease_recovery(clp);
436 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400437 case -NFS4ERR_MOVED:
438 ret = nfs4_schedule_migration_recovery(server);
439 if (ret < 0)
440 break;
441 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400442 case -NFS4ERR_LEASE_MOVED:
443 nfs4_schedule_lease_moved_recovery(clp);
444 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500445#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400446 case -NFS4ERR_BADSESSION:
447 case -NFS4ERR_BADSLOT:
448 case -NFS4ERR_BAD_HIGH_SLOT:
449 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
450 case -NFS4ERR_DEADSESSION:
451 case -NFS4ERR_SEQ_FALSE_RETRY:
452 case -NFS4ERR_SEQ_MISORDERED:
453 dprintk("%s ERROR: %d Reset session\n", __func__,
454 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400455 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400456 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500457#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500458 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500459 if (exception->timeout > HZ) {
460 /* We have retried a decent amount, time to
461 * fail
462 */
463 ret = -EBUSY;
464 break;
465 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500466 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400467 nfs_inc_server_stats(server, NFSIOS_DELAY);
468 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400469 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400470 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400471 exception->delay = 1;
472 return 0;
473
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400474 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500475 case -NFS4ERR_OLD_STATEID:
476 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800477 break;
478 case -NFS4ERR_BADOWNER:
479 /* The following works around a Linux server bug! */
480 case -NFS4ERR_BADNAME:
481 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
482 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
483 exception->retry = 1;
484 printk(KERN_WARNING "NFS: v4 server %s "
485 "does not accept raw "
486 "uid/gids. "
487 "Reenabling the idmapper.\n",
488 server->nfs_client->cl_hostname);
489 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500490 }
491 /* We failed to handle the error */
492 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500493wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400494 exception->recovering = 1;
495 return 0;
496}
497
498/* This is the error handling routine for processes that are allowed
499 * to sleep.
500 */
501int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
502{
503 struct nfs_client *clp = server->nfs_client;
504 int ret;
505
506 ret = nfs4_do_handle_exception(server, errorcode, exception);
507 if (exception->delay) {
508 ret = nfs4_delay(server->client, &exception->timeout);
509 goto out_retry;
510 }
511 if (exception->recovering) {
512 ret = nfs4_wait_clnt_recover(clp);
513 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
514 return -EIO;
515 goto out_retry;
516 }
517 return ret;
518out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500519 if (ret == 0)
520 exception->retry = 1;
521 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500522}
523
Trond Myklebust037fc982015-09-20 15:51:00 -0400524static int
525nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
526 int errorcode, struct nfs4_exception *exception)
527{
528 struct nfs_client *clp = server->nfs_client;
529 int ret;
530
531 ret = nfs4_do_handle_exception(server, errorcode, exception);
532 if (exception->delay) {
533 rpc_delay(task, nfs4_update_delay(&exception->timeout));
534 goto out_retry;
535 }
536 if (exception->recovering) {
537 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
538 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
539 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
540 goto out_retry;
541 }
542 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
543 ret = -EIO;
544 return ret;
545out_retry:
546 if (ret == 0)
547 exception->retry = 1;
548 return ret;
549}
550
551static int
552nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
553 struct nfs4_state *state, long *timeout)
554{
555 struct nfs4_exception exception = {
556 .state = state,
557 };
558
559 if (task->tk_status >= 0)
560 return 0;
561 if (timeout)
562 exception.timeout = *timeout;
563 task->tk_status = nfs4_async_handle_exception(task, server,
564 task->tk_status,
565 &exception);
566 if (exception.delay && timeout)
567 *timeout = exception.timeout;
568 if (exception.retry)
569 return -EAGAIN;
570 return 0;
571}
572
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400573/*
574 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
575 * or 'false' otherwise.
576 */
577static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
578{
579 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500580 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400581}
Trond Myklebust65de8722008-12-23 15:21:44 -0500582
Trond Myklebust452e9352010-07-31 14:29:06 -0400583static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 spin_lock(&clp->cl_lock);
586 if (time_before(clp->cl_last_renewal,timestamp))
587 clp->cl_last_renewal = timestamp;
588 spin_unlock(&clp->cl_lock);
589}
590
Trond Myklebust452e9352010-07-31 14:29:06 -0400591static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
592{
Trond Myklebustbe824162015-07-05 14:50:46 -0400593 struct nfs_client *clp = server->nfs_client;
594
595 if (!nfs4_has_session(clp))
596 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400597}
598
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400599struct nfs4_call_sync_data {
600 const struct nfs_server *seq_server;
601 struct nfs4_sequence_args *seq_args;
602 struct nfs4_sequence_res *seq_res;
603};
604
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800605void nfs4_init_sequence(struct nfs4_sequence_args *args,
606 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400607{
608 args->sa_slot = NULL;
609 args->sa_cache_this = cache_reply;
610 args->sa_privileged = 0;
611
612 res->sr_slot = NULL;
613}
614
615static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
616{
617 args->sa_privileged = 1;
618}
619
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400620static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400621{
622 struct nfs4_slot *slot = res->sr_slot;
623 struct nfs4_slot_table *tbl;
624
Chuck Lever3bd23842013-08-09 12:49:19 -0400625 tbl = slot->table;
626 spin_lock(&tbl->slot_tbl_lock);
627 if (!nfs41_wake_and_assign_slot(tbl, slot))
628 nfs4_free_slot(tbl, slot);
629 spin_unlock(&tbl->slot_tbl_lock);
630
631 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400632}
633
634static int nfs40_sequence_done(struct rpc_task *task,
635 struct nfs4_sequence_res *res)
636{
637 if (res->sr_slot != NULL)
638 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400639 return 1;
640}
641
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400642#if defined(CONFIG_NFS_V4_1)
643
Trond Myklebustd185a332010-06-16 09:52:25 -0400644static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400645{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500646 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400647 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500648 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500649 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400650
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500651 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500652 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500653
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400654 /* Bump the slot sequence number */
655 if (slot->seq_done)
656 slot->seq_nr++;
657 slot->seq_done = 0;
658
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500659 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500660 /* Be nice to the server: try to ensure that the last transmitted
661 * value for highest_user_slotid <= target_highest_slotid
662 */
663 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
664 send_new_highest_used_slotid = true;
665
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500666 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500667 send_new_highest_used_slotid = false;
668 goto out_unlock;
669 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500670 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500671
672 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
673 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500674out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500675 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400676 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500677 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400678 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400679 if (waitqueue_active(&tbl->slot_waitq))
680 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400681}
682
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400683static int nfs41_sequence_process(struct rpc_task *task,
684 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400685{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500686 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500687 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400688 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500689 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500690 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400691
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500692 if (slot == NULL)
693 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400694 /* don't increment the sequence number if the task wasn't sent */
695 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400696 goto out;
697
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500698 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500699
Trond Myklebustac20d162012-12-15 15:36:07 -0500700 if (slot->interrupted) {
701 slot->interrupted = 0;
702 interrupted = true;
703 }
704
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400705 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500706 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400707 switch (res->sr_status) {
708 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400709 /* If previous op on slot was interrupted and we reused
710 * the seq# and got a reply from the cache, then retry
711 */
712 if (task->tk_status == -EREMOTEIO && interrupted) {
713 ++slot->seq_nr;
714 goto retry_nowait;
715 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400716 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400717 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500718 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500719 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500720 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400721 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
722 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500723 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400724 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500725 case 1:
726 /*
727 * sr_status remains 1 if an RPC level error occurred.
728 * The server may or may not have processed the sequence
729 * operation..
730 * Mark the slot as having hosted an interrupted RPC call.
731 */
732 slot->interrupted = 1;
733 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400734 case -NFS4ERR_DELAY:
735 /* The server detected a resend of the RPC call and
736 * returned NFS4ERR_DELAY as per Section 2.10.6.2
737 * of RFC5661.
738 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500739 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400740 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500741 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500742 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400743 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500744 case -NFS4ERR_BADSLOT:
745 /*
746 * The slot id we used was probably retired. Try again
747 * using a different slot id.
748 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500749 goto retry_nowait;
750 case -NFS4ERR_SEQ_MISORDERED:
751 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500752 * Was the last operation on this sequence interrupted?
753 * If so, retry after bumping the sequence number.
754 */
755 if (interrupted) {
756 ++slot->seq_nr;
757 goto retry_nowait;
758 }
759 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500760 * Could this slot have been previously retired?
761 * If so, then the server may be expecting seq_nr = 1!
762 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500763 if (slot->seq_nr != 1) {
764 slot->seq_nr = 1;
765 goto retry_nowait;
766 }
767 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500768 case -NFS4ERR_SEQ_FALSE_RETRY:
769 ++slot->seq_nr;
770 goto retry_nowait;
Trond Myklebust2cf10cd2016-12-04 19:26:40 -0500771 case -NFS4ERR_DEADSESSION:
772 case -NFS4ERR_BADSESSION:
773 nfs4_schedule_session_recovery(session, res->sr_status);
774 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400775 default:
776 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400777 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400778 }
779out:
780 /* The session may be reset by one of the error handlers. */
781 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500782out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500783 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500784retry_nowait:
785 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400786 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500787 task->tk_status = 0;
788 ret = 0;
789 }
790 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400791out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400792 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400793 goto out;
794 rpc_delay(task, NFS4_POLL_RETRY_MAX);
795 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400796}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400797
798int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
799{
800 if (!nfs41_sequence_process(task, res))
801 return 0;
802 if (res->sr_slot != NULL)
803 nfs41_sequence_free_slot(res);
804 return 1;
805
806}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500807EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400808
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400809static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
810{
811 if (res->sr_slot == NULL)
812 return 1;
813 if (res->sr_slot->table->session != NULL)
814 return nfs41_sequence_process(task, res);
815 return nfs40_sequence_done(task, res);
816}
817
818static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
819{
820 if (res->sr_slot != NULL) {
821 if (res->sr_slot->table->session != NULL)
822 nfs41_sequence_free_slot(res);
823 else
824 nfs40_sequence_free_slot(res);
825 }
826}
827
Peng Tao2c4b1312014-06-11 05:24:16 +0800828int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400829{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500830 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400831 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400832 if (!res->sr_slot->table->session)
833 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400834 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400835}
Peng Tao2c4b1312014-06-11 05:24:16 +0800836EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400837
Andy Adamsonce5039c2009-04-01 09:22:13 -0400838static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
839{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400840 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400841
Trond Myklebust035168ab2010-06-16 09:52:26 -0400842 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
843
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500844 nfs4_setup_sequence(data->seq_server->nfs_client,
845 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400846}
847
Andy Adamson69ab40c2009-04-01 09:22:19 -0400848static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
849{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400850 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400851
Trond Myklebust14516c32010-07-31 14:29:06 -0400852 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400853}
854
Trond Myklebust17280172012-03-11 13:11:00 -0400855static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400856 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400857 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400858};
859
Chuck Lever3bd23842013-08-09 12:49:19 -0400860#else /* !CONFIG_NFS_V4_1 */
861
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400862static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
863{
864 return nfs40_sequence_done(task, res);
865}
866
867static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
868{
869 if (res->sr_slot != NULL)
870 nfs40_sequence_free_slot(res);
871}
872
Peng Tao2c4b1312014-06-11 05:24:16 +0800873int nfs4_sequence_done(struct rpc_task *task,
874 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400875{
Chuck Lever3bd23842013-08-09 12:49:19 -0400876 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400877}
Peng Tao2c4b1312014-06-11 05:24:16 +0800878EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400879
880#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400881
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500882int nfs4_setup_sequence(const struct nfs_client *client,
883 struct nfs4_sequence_args *args,
884 struct nfs4_sequence_res *res,
885 struct rpc_task *task)
886{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500887 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500888 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500889 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500890
Anna Schumaker9dd91072017-01-10 12:01:46 -0500891 /* slot already allocated? */
892 if (res->sr_slot != NULL)
893 goto out_start;
894
Anna Schumaker76ee0352017-01-10 16:49:31 -0500895 if (session) {
896 tbl = &session->fc_slot_table;
897 task->tk_timeout = 0;
898 }
899
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500900 spin_lock(&tbl->slot_tbl_lock);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500901 /* The state manager will wait until the slot table is empty */
902 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
903 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500904
Anna Schumaker3d358082017-01-11 10:54:04 -0500905 slot = nfs4_alloc_slot(tbl);
906 if (IS_ERR(slot)) {
907 /* Try again in 1/4 second */
908 if (slot == ERR_PTR(-ENOMEM))
909 task->tk_timeout = HZ >> 2;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500910 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -0500911 }
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500912 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500913
Anna Schumaker3d358082017-01-11 10:54:04 -0500914 slot->privileged = args->sa_privileged ? 1 : 0;
915 args->sa_slot = slot;
916
917 res->sr_slot = slot;
918 if (session) {
919 res->sr_timestamp = jiffies;
920 res->sr_status_flags = 0;
921 res->sr_status = 1;
Anna Schumaker3d358082017-01-11 10:54:04 -0500922 }
923
Anna Schumakerad05cc02017-01-11 13:37:06 -0500924 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd91072017-01-10 12:01:46 -0500925out_start:
926 rpc_call_start(task);
927 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500928
929out_sleep:
930 if (args->sa_privileged)
931 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
932 NULL, RPC_PRIORITY_PRIVILEGED);
933 else
934 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
935 spin_unlock(&tbl->slot_tbl_lock);
936 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500937}
938EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
939
Chuck Lever9915ea72013-08-09 12:48:27 -0400940static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
941{
942 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500943 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -0400944 data->seq_args, data->seq_res, task);
945}
946
947static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
948{
949 struct nfs4_call_sync_data *data = calldata;
950 nfs4_sequence_done(task, data->seq_res);
951}
952
953static const struct rpc_call_ops nfs40_call_sync_ops = {
954 .rpc_call_prepare = nfs40_call_sync_prepare,
955 .rpc_call_done = nfs40_call_sync_done,
956};
957
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400958static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400959 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500960 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100961 struct nfs4_sequence_args *args,
962 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400963{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100964 int ret;
965 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400966 struct nfs_client *clp = server->nfs_client;
967 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100968 .seq_server = server,
969 .seq_args = args,
970 .seq_res = res,
971 };
972 struct rpc_task_setup task_setup = {
973 .rpc_client = clnt,
974 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400975 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100976 .callback_data = &data
977 };
978
979 task = rpc_run_task(&task_setup);
980 if (IS_ERR(task))
981 ret = PTR_ERR(task);
982 else {
983 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400984 rpc_put_task(task);
985 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100986 return ret;
987}
988
Bryan Schumaker7c513052011-03-24 17:12:24 +0000989int nfs4_call_sync(struct rpc_clnt *clnt,
990 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000991 struct rpc_message *msg,
992 struct nfs4_sequence_args *args,
993 struct nfs4_sequence_res *res,
994 int cache_reply)
995{
Chuck Levera9c92d62013-08-09 12:48:18 -0400996 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400997 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000998}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001000static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1001 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 struct nfs_inode *nfsi = NFS_I(dir);
1004
1005 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001006 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001007 if (cinfo->atomic && cinfo->before == dir->i_version) {
1008 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1009 nfsi->attrtimeo_timestamp = jiffies;
1010 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 nfs_force_lookup_revalidate(dir);
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001012 if (cinfo->before != dir->i_version)
1013 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1014 NFS_INO_INVALID_ACL;
1015 }
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001016 dir->i_version = cinfo->after;
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001017 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001018 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001019 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 spin_unlock(&dir->i_lock);
1021}
1022
1023struct nfs4_opendata {
1024 struct kref kref;
1025 struct nfs_openargs o_arg;
1026 struct nfs_openres o_res;
1027 struct nfs_open_confirmargs c_arg;
1028 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001029 struct nfs4_string owner_name;
1030 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001031 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001033 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001035 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 struct nfs4_state_owner *owner;
1037 struct nfs4_state *state;
1038 struct iattr attrs;
1039 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001040 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001041 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001042 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001043 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 int cancelled;
1045};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001046
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001047static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1048 int err, struct nfs4_exception *exception)
1049{
1050 if (err != -EINVAL)
1051 return false;
1052 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1053 return false;
1054 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1055 exception->retry = 1;
1056 return true;
1057}
1058
Trond Myklebust6ae37332015-01-30 14:21:14 -05001059static u32
1060nfs4_map_atomic_open_share(struct nfs_server *server,
1061 fmode_t fmode, int openflags)
1062{
1063 u32 res = 0;
1064
1065 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1066 case FMODE_READ:
1067 res = NFS4_SHARE_ACCESS_READ;
1068 break;
1069 case FMODE_WRITE:
1070 res = NFS4_SHARE_ACCESS_WRITE;
1071 break;
1072 case FMODE_READ|FMODE_WRITE:
1073 res = NFS4_SHARE_ACCESS_BOTH;
1074 }
1075 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1076 goto out;
1077 /* Want no delegation if we're using O_DIRECT */
1078 if (openflags & O_DIRECT)
1079 res |= NFS4_SHARE_WANT_NO_DELEG;
1080out:
1081 return res;
1082}
1083
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001084static enum open_claim_type4
1085nfs4_map_atomic_open_claim(struct nfs_server *server,
1086 enum open_claim_type4 claim)
1087{
1088 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1089 return claim;
1090 switch (claim) {
1091 default:
1092 return claim;
1093 case NFS4_OPEN_CLAIM_FH:
1094 return NFS4_OPEN_CLAIM_NULL;
1095 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1096 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1097 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1098 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1099 }
1100}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001103{
1104 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001105 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001106 p->o_res.seqid = p->o_arg.seqid;
1107 p->c_res.seqid = p->c_arg.seqid;
1108 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001109 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001110 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001111 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001112}
1113
Al Viro82a2c1b2011-06-22 18:30:55 -04001114static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001115 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001116 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001117 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001118 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001119 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001120{
Al Viro82a2c1b2011-06-22 18:30:55 -04001121 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001122 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001123 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001124 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001125 struct nfs4_opendata *p;
1126
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001127 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001128 if (p == NULL)
1129 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001130
1131 p->f_label = nfs4_label_alloc(server, gfp_mask);
1132 if (IS_ERR(p->f_label))
1133 goto err_free_p;
1134
Kinglong Meea49c2692015-07-27 15:31:38 +08001135 p->a_label = nfs4_label_alloc(server, gfp_mask);
1136 if (IS_ERR(p->a_label))
1137 goto err_free_f;
1138
Trond Myklebust63f5f792015-01-23 19:19:25 -05001139 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1140 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001141 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001142 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001143 nfs_sb_active(dentry->d_sb);
1144 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001145 p->dir = parent;
1146 p->owner = sp;
1147 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001148 p->o_arg.open_flags = flags;
1149 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001150 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001151 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001152 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1153 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001154 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1155 * will return permission denied for all bits until close */
1156 if (!(flags & O_EXCL)) {
1157 /* ask server to check for all possible rights as results
1158 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001159 switch (p->o_arg.claim) {
1160 default:
1161 break;
1162 case NFS4_OPEN_CLAIM_NULL:
1163 case NFS4_OPEN_CLAIM_FH:
1164 p->o_arg.access = NFS4_ACCESS_READ |
1165 NFS4_ACCESS_MODIFY |
1166 NFS4_ACCESS_EXTEND |
1167 NFS4_ACCESS_EXECUTE;
1168 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001169 }
David Howells7539bba2006-08-22 20:06:09 -04001170 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001171 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1172 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001173 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001174 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001175 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001176 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001177 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001178 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001179 case NFS4_OPEN_CLAIM_NULL:
1180 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1181 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1182 p->o_arg.fh = NFS_FH(dir);
1183 break;
1184 case NFS4_OPEN_CLAIM_PREVIOUS:
1185 case NFS4_OPEN_CLAIM_FH:
1186 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1187 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001188 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001189 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001190 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001191 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001192
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001193 p->o_arg.u.attrs = &p->attrs;
1194 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001195
1196 verf[0] = jiffies;
1197 verf[1] = current->pid;
1198 memcpy(p->o_arg.u.verifier.data, verf,
1199 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001200 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001201 p->c_arg.fh = &p->o_res.fh;
1202 p->c_arg.stateid = &p->o_res.stateid;
1203 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001204 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001205 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001206 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001207
1208err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001209 nfs4_label_free(p->a_label);
1210err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001211 nfs4_label_free(p->f_label);
1212err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001213 kfree(p);
1214err:
1215 dput(parent);
1216 return NULL;
1217}
1218
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001219static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001220{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001221 struct nfs4_opendata *p = container_of(kref,
1222 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001223 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001224
1225 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001226 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001227 if (p->state != NULL)
1228 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001229 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001230
Kinglong Meea49c2692015-07-27 15:31:38 +08001231 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001232 nfs4_label_free(p->f_label);
1233
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001234 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001235 dput(p->dentry);
1236 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001237 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001238 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001239 kfree(p);
1240}
1241
1242static void nfs4_opendata_put(struct nfs4_opendata *p)
1243{
1244 if (p != NULL)
1245 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001246}
1247
Trond Myklebust24311f82015-09-20 10:50:17 -04001248static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1249 fmode_t fmode)
1250{
1251 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1252 case FMODE_READ|FMODE_WRITE:
1253 return state->n_rdwr != 0;
1254 case FMODE_WRITE:
1255 return state->n_wronly != 0;
1256 case FMODE_READ:
1257 return state->n_rdonly != 0;
1258 }
1259 WARN_ON_ONCE(1);
1260 return false;
1261}
1262
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001263static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001264{
1265 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001266
Trond Myklebust536e43d2012-01-17 22:04:26 -05001267 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001268 goto out;
1269 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001270 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001271 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1272 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001273 break;
1274 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001275 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1276 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001277 break;
1278 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001279 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1280 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001281 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001282out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001283 return ret;
1284}
1285
Trond Myklebust2a606182015-08-19 22:30:00 -05001286static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1287 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001288{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001289 if (delegation == NULL)
1290 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001291 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001292 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001293 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1294 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001295 switch (claim) {
1296 case NFS4_OPEN_CLAIM_NULL:
1297 case NFS4_OPEN_CLAIM_FH:
1298 break;
1299 case NFS4_OPEN_CLAIM_PREVIOUS:
1300 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1301 break;
1302 default:
1303 return 0;
1304 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001305 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001306 return 1;
1307}
1308
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001309static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001310{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001311 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001312 case FMODE_WRITE:
1313 state->n_wronly++;
1314 break;
1315 case FMODE_READ:
1316 state->n_rdonly++;
1317 break;
1318 case FMODE_READ|FMODE_WRITE:
1319 state->n_rdwr++;
1320 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001321 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001322}
1323
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001324#ifdef CONFIG_NFS_V4_1
1325static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1326{
1327 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1328 return true;
1329 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1330 return true;
1331 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1332 return true;
1333 return false;
1334}
1335#endif /* CONFIG_NFS_V4_1 */
1336
Trond Myklebust4f14c192014-02-12 19:15:06 -05001337static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001338{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001339 struct nfs_client *clp = state->owner->so_server->nfs_client;
1340 bool need_recover = false;
1341
1342 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1343 need_recover = true;
1344 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1345 need_recover = true;
1346 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1347 need_recover = true;
1348 if (need_recover)
1349 nfs4_state_mark_reclaim_nograce(clp, state);
1350}
1351
Trond Myklebuste999e802014-02-10 18:20:47 -05001352static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001353 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001354{
1355 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1356 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001357 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001358 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001359 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001360 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001361 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001362 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1363 return true;
1364 return false;
1365}
1366
Trond Myklebustf95549c2015-01-23 18:06:09 -05001367static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1368{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001369 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1370 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001371 if (state->n_wronly)
1372 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1373 if (state->n_rdonly)
1374 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1375 if (state->n_rdwr)
1376 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001377 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001378}
1379
Trond Myklebust226056c2014-02-11 10:41:07 -05001380static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1381 nfs4_stateid *stateid, fmode_t fmode)
1382{
1383 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1384 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1385 case FMODE_WRITE:
1386 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1387 break;
1388 case FMODE_READ:
1389 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1390 break;
1391 case 0:
1392 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1393 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1394 clear_bit(NFS_OPEN_STATE, &state->flags);
1395 }
1396 if (stateid == NULL)
1397 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001398 /* Handle OPEN+OPEN_DOWNGRADE races */
1399 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1400 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001401 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001402 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001403 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001404 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001405 nfs4_stateid_copy(&state->stateid, stateid);
1406 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001407}
1408
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001409static void nfs_clear_open_stateid(struct nfs4_state *state,
1410 nfs4_stateid *arg_stateid,
1411 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001412{
1413 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001414 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1415 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1416 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001417 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001418 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1419 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001420}
1421
Trond Myklebust1393d962016-09-22 13:39:13 -04001422static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1423 const nfs4_stateid *stateid, fmode_t fmode,
1424 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001425{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001426 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001427 case FMODE_READ:
1428 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1429 break;
1430 case FMODE_WRITE:
1431 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1432 break;
1433 case FMODE_READ|FMODE_WRITE:
1434 set_bit(NFS_O_RDWR_STATE, &state->flags);
1435 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001436 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001437 return;
1438 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1439 nfs4_stateid_copy(&state->stateid, stateid);
1440 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001441}
1442
Trond Myklebust1393d962016-09-22 13:39:13 -04001443static void __update_open_stateid(struct nfs4_state *state,
1444 const nfs4_stateid *open_stateid,
1445 const nfs4_stateid *deleg_stateid,
1446 fmode_t fmode,
1447 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001449 /*
1450 * Protect the call to nfs4_state_set_mode_locked and
1451 * serialise the stateid update
1452 */
Andrew Elble361cad32015-12-02 09:20:57 -05001453 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001454 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001455 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001456 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001457 set_bit(NFS_DELEGATED_STATE, &state->flags);
1458 }
1459 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001460 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001461 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001462 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001463 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Trond Myklebust1393d962016-09-22 13:39:13 -04001466static int update_open_stateid(struct nfs4_state *state,
1467 const nfs4_stateid *open_stateid,
1468 const nfs4_stateid *delegation,
1469 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001470{
Trond Myklebust1393d962016-09-22 13:39:13 -04001471 struct nfs_server *server = NFS_SERVER(state->inode);
1472 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001473 struct nfs_inode *nfsi = NFS_I(state->inode);
1474 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001475 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001476 int ret = 0;
1477
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001478 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001479
1480 rcu_read_lock();
1481 deleg_cur = rcu_dereference(nfsi->delegation);
1482 if (deleg_cur == NULL)
1483 goto no_delegation;
1484
1485 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001486 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001487 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001488 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001489 goto no_delegation_unlock;
1490
1491 if (delegation == NULL)
1492 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001493 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001494 goto no_delegation_unlock;
1495
Trond Myklebustb7391f42008-12-23 15:21:52 -05001496 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001497 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1498 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001499 ret = 1;
1500no_delegation_unlock:
1501 spin_unlock(&deleg_cur->lock);
1502no_delegation:
1503 rcu_read_unlock();
1504
1505 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001506 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001507 ret = 1;
1508 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001509 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001510 nfs4_schedule_state_manager(clp);
1511 if (freeme.type != 0)
1512 nfs4_test_and_free_stateid(server, &freeme,
1513 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001514
1515 return ret;
1516}
1517
Trond Myklebust39071e62015-01-24 15:07:56 -05001518static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1519 const nfs4_stateid *stateid)
1520{
1521 struct nfs4_state *state = lsp->ls_state;
1522 bool ret = false;
1523
1524 spin_lock(&state->state_lock);
1525 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1526 goto out_noupdate;
1527 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1528 goto out_noupdate;
1529 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1530 ret = true;
1531out_noupdate:
1532 spin_unlock(&state->state_lock);
1533 return ret;
1534}
Trond Myklebust34310432008-12-23 15:21:38 -05001535
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001536static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001537{
1538 struct nfs_delegation *delegation;
1539
1540 rcu_read_lock();
1541 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001542 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001543 rcu_read_unlock();
1544 return;
1545 }
1546 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001547 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001548}
1549
Trond Myklebust6ee41262007-07-08 14:11:36 -04001550static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001551{
1552 struct nfs4_state *state = opendata->state;
1553 struct nfs_inode *nfsi = NFS_I(state->inode);
1554 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001555 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001556 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001557 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001558 nfs4_stateid stateid;
1559 int ret = -EAGAIN;
1560
Trond Myklebustaac00a82007-07-05 19:02:21 -04001561 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001562 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001563 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001564 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001565 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001566 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001567 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001568 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001569 rcu_read_lock();
1570 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001571 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001572 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001573 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001574 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001575 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001576 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001577 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001578 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001579 if (!opendata->is_recover) {
1580 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1581 if (ret != 0)
1582 goto out;
1583 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001584 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001585
1586 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001587 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001588 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001589 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001590out:
1591 return ERR_PTR(ret);
1592out_return_state:
1593 atomic_inc(&state->count);
1594 return state;
1595}
1596
Andy Adamsone23008e2012-10-02 21:07:32 -04001597static void
1598nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1599{
1600 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1601 struct nfs_delegation *delegation;
1602 int delegation_flags = 0;
1603
1604 rcu_read_lock();
1605 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1606 if (delegation)
1607 delegation_flags = delegation->flags;
1608 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001609 switch (data->o_arg.claim) {
1610 default:
1611 break;
1612 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1613 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001614 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1615 "returning a delegation for "
1616 "OPEN(CLAIM_DELEGATE_CUR)\n",
1617 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001618 return;
1619 }
1620 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001621 nfs_inode_set_delegation(state->inode,
1622 data->owner->so_cred,
1623 &data->o_res);
1624 else
1625 nfs_inode_reclaim_delegation(state->inode,
1626 data->owner->so_cred,
1627 &data->o_res);
1628}
1629
1630/*
1631 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1632 * and update the nfs4_state.
1633 */
1634static struct nfs4_state *
1635_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1636{
1637 struct inode *inode = data->state->inode;
1638 struct nfs4_state *state = data->state;
1639 int ret;
1640
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001641 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001642 if (data->rpc_status)
1643 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001644 /* cached opens have already been processed */
1645 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001646 }
1647
Andy Adamsone23008e2012-10-02 21:07:32 -04001648 ret = nfs_refresh_inode(inode, &data->f_attr);
1649 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001650 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001651
1652 if (data->o_res.delegation_type != 0)
1653 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001654update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001655 update_open_stateid(state, &data->o_res.stateid, NULL,
1656 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001657 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001658
1659 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001660}
1661
1662static struct nfs4_state *
1663_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001664{
1665 struct inode *inode;
1666 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001667 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001668
Trond Myklebustaac00a82007-07-05 19:02:21 -04001669 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001670 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001671 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001672 goto out;
1673 }
1674
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001675 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001676 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001677 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001678 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001679 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001680 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001681 goto err;
1682 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001683 state = nfs4_get_open_state(inode, data->owner);
1684 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001685 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001686 if (data->o_res.delegation_type != 0)
1687 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001688 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001689 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001690 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001691out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001692 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001693 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001694err_put_inode:
1695 iput(inode);
1696err:
1697 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001698}
1699
Andy Adamsone23008e2012-10-02 21:07:32 -04001700static struct nfs4_state *
1701nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1702{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001703 struct nfs4_state *ret;
1704
Andy Adamsone23008e2012-10-02 21:07:32 -04001705 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001706 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1707 else
1708 ret = _nfs4_opendata_to_nfs4_state(data);
1709 nfs4_sequence_free_slot(&data->o_res.seq_res);
1710 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001711}
1712
Trond Myklebust864472e2006-01-03 09:55:15 +01001713static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1714{
1715 struct nfs_inode *nfsi = NFS_I(state->inode);
1716 struct nfs_open_context *ctx;
1717
1718 spin_lock(&state->inode->i_lock);
1719 list_for_each_entry(ctx, &nfsi->open_files, list) {
1720 if (ctx->state != state)
1721 continue;
1722 get_nfs_open_context(ctx);
1723 spin_unlock(&state->inode->i_lock);
1724 return ctx;
1725 }
1726 spin_unlock(&state->inode->i_lock);
1727 return ERR_PTR(-ENOENT);
1728}
1729
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001730static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1731 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001732{
1733 struct nfs4_opendata *opendata;
1734
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001735 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001736 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001737 if (opendata == NULL)
1738 return ERR_PTR(-ENOMEM);
1739 opendata->state = state;
1740 atomic_inc(&state->count);
1741 return opendata;
1742}
1743
Trond Myklebust24311f82015-09-20 10:50:17 -04001744static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1745 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001746{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001747 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001748 int ret;
1749
Trond Myklebust24311f82015-09-20 10:50:17 -04001750 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001751 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001752 opendata->o_arg.open_flags = 0;
1753 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001754 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1755 NFS_SB(opendata->dentry->d_sb),
1756 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001757 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1758 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1759 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001760 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001761 if (ret != 0)
1762 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001763 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001764 if (IS_ERR(newstate))
1765 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001766 if (newstate != opendata->state)
1767 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001768 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001769 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001770}
1771
1772static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1773{
Trond Myklebust864472e2006-01-03 09:55:15 +01001774 int ret;
1775
Trond Myklebust4f14c192014-02-12 19:15:06 -05001776 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1777 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1778 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1779 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001780 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001781 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001782 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001783 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001784 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1785 if (ret != 0)
1786 return ret;
1787 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1788 if (ret != 0)
1789 return ret;
1790 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1791 if (ret != 0)
1792 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001793 /*
1794 * We may have performed cached opens for all three recoveries.
1795 * Check if we need to update the current stateid.
1796 */
1797 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001798 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001799 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001800 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001801 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001802 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001803 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001804 return 0;
1805}
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807/*
1808 * OPEN_RECLAIM:
1809 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001811static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001813 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001814 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001815 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 int status;
1817
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001818 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1819 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001820 if (IS_ERR(opendata))
1821 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001822 rcu_read_lock();
1823 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001824 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001825 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001826 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001827 opendata->o_arg.u.delegation_type = delegation_type;
1828 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001829 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return status;
1831}
1832
Trond Myklebust539cd032007-06-05 11:46:42 -04001833static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
1835 struct nfs_server *server = NFS_SERVER(state->inode);
1836 struct nfs4_exception exception = { };
1837 int err;
1838 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001839 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001840 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001841 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1842 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001843 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001844 break;
1845 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 } while (exception.retry);
1847 return err;
1848}
1849
Trond Myklebust864472e2006-01-03 09:55:15 +01001850static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1851{
1852 struct nfs_open_context *ctx;
1853 int ret;
1854
1855 ctx = nfs4_state_find_open_context(state);
1856 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001857 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001858 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001859 put_nfs_open_context(ctx);
1860 return ret;
1861}
1862
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001863static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001865 switch (err) {
1866 default:
1867 printk(KERN_ERR "NFS: %s: unhandled error "
1868 "%d.\n", __func__, err);
1869 case 0:
1870 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001871 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001872 case -ESTALE:
1873 break;
1874 case -NFS4ERR_BADSESSION:
1875 case -NFS4ERR_BADSLOT:
1876 case -NFS4ERR_BAD_HIGH_SLOT:
1877 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1878 case -NFS4ERR_DEADSESSION:
1879 set_bit(NFS_DELEGATED_STATE, &state->flags);
1880 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1881 return -EAGAIN;
1882 case -NFS4ERR_STALE_CLIENTID:
1883 case -NFS4ERR_STALE_STATEID:
1884 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001885 /* Don't recall a delegation if it was lost */
1886 nfs4_schedule_lease_recovery(server->nfs_client);
1887 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001888 case -NFS4ERR_MOVED:
1889 nfs4_schedule_migration_recovery(server);
1890 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001891 case -NFS4ERR_LEASE_MOVED:
1892 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1893 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001894 case -NFS4ERR_DELEG_REVOKED:
1895 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001896 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001897 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001898 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001899 nfs_inode_find_state_and_recover(state->inode,
1900 stateid);
1901 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001902 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001903 case -NFS4ERR_DELAY:
1904 case -NFS4ERR_GRACE:
1905 set_bit(NFS_DELEGATED_STATE, &state->flags);
1906 ssleep(1);
1907 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001908 case -ENOMEM:
1909 case -NFS4ERR_DENIED:
1910 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1911 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return err;
1914}
1915
Trond Myklebust24311f82015-09-20 10:50:17 -04001916int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1917 struct nfs4_state *state, const nfs4_stateid *stateid,
1918 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001919{
1920 struct nfs_server *server = NFS_SERVER(state->inode);
1921 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001922 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001923
1924 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1925 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1926 if (IS_ERR(opendata))
1927 return PTR_ERR(opendata);
1928 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001929 write_seqlock(&state->seqlock);
1930 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1931 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001932 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1933 switch (type & (FMODE_READ|FMODE_WRITE)) {
1934 case FMODE_READ|FMODE_WRITE:
1935 case FMODE_WRITE:
1936 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1937 if (err)
1938 break;
1939 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1940 if (err)
1941 break;
1942 case FMODE_READ:
1943 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1944 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001945 nfs4_opendata_put(opendata);
1946 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1947}
1948
Chuck Leverbe05c862013-08-09 12:49:47 -04001949static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1950{
1951 struct nfs4_opendata *data = calldata;
1952
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001953 nfs4_setup_sequence(data->o_arg.server->nfs_client,
1954 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001955}
1956
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001957static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1958{
1959 struct nfs4_opendata *data = calldata;
1960
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001961 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001962
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001963 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001964 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001965 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001966 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001967 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001968 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001969 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001970}
1971
1972static void nfs4_open_confirm_release(void *calldata)
1973{
1974 struct nfs4_opendata *data = calldata;
1975 struct nfs4_state *state = NULL;
1976
1977 /* If this request hasn't been cancelled, do nothing */
1978 if (data->cancelled == 0)
1979 goto out_free;
1980 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001981 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001982 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001983 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001984 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001985 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001986out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001987 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001988}
1989
1990static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001991 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001992 .rpc_call_done = nfs4_open_confirm_done,
1993 .rpc_release = nfs4_open_confirm_release,
1994};
1995
1996/*
1997 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1998 */
1999static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2000{
David Howells2b0143b2015-03-17 22:25:59 +00002001 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002002 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002003 struct rpc_message msg = {
2004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2005 .rpc_argp = &data->c_arg,
2006 .rpc_resp = &data->c_res,
2007 .rpc_cred = data->owner->so_cred,
2008 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002009 struct rpc_task_setup task_setup_data = {
2010 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002011 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002012 .callback_ops = &nfs4_open_confirm_ops,
2013 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002014 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002015 .flags = RPC_TASK_ASYNC,
2016 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 int status;
2018
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002019 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002020 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002021 data->rpc_done = 0;
2022 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002023 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002024 if (data->is_recover)
2025 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002026 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002027 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002028 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002029 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002030 if (status != 0) {
2031 data->cancelled = 1;
2032 smp_wmb();
2033 } else
2034 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002035 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return status;
2037}
2038
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002039static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002041 struct nfs4_opendata *data = calldata;
2042 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002043 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002044 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002045
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002046 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002047 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002048 /*
2049 * Check if we still need to send an OPEN call, or if we can use
2050 * a delegation instead.
2051 */
2052 if (data->state != NULL) {
2053 struct nfs_delegation *delegation;
2054
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002055 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002056 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002057 rcu_read_lock();
2058 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002059 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002060 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002061 rcu_read_unlock();
2062 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002063 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002064 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002065 switch (claim) {
2066 default:
2067 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002068 case NFS4_OPEN_CLAIM_PREVIOUS:
2069 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2070 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002071 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002072 case NFS4_OPEN_CLAIM_FH:
2073 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002074 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2075 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002076 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002077 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002078 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002079 &data->o_res.seq_res,
2080 task) != 0)
2081 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002082
2083 /* Set the create mode (note dependency on the session type) */
2084 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2085 if (data->o_arg.open_flags & O_EXCL) {
2086 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2087 if (nfs4_has_persistent_session(clp))
2088 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2089 else if (clp->cl_mvops->minor_version > 0)
2090 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2091 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002092 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002093unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002094 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002095 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002096out_no_action:
2097 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002098out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002099 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002100}
2101
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002102static void nfs4_open_done(struct rpc_task *task, void *calldata)
2103{
2104 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002106 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002107
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002108 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002109 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002110
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002111 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002112 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2113 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002114 case S_IFREG:
2115 break;
2116 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002117 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002118 break;
2119 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002120 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002121 break;
2122 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002123 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002124 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002125 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002126 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002127 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2128 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002129 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002130 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002131}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002132
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002133static void nfs4_open_release(void *calldata)
2134{
2135 struct nfs4_opendata *data = calldata;
2136 struct nfs4_state *state = NULL;
2137
2138 /* If this request hasn't been cancelled, do nothing */
2139 if (data->cancelled == 0)
2140 goto out_free;
2141 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002142 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002143 goto out_free;
2144 /* In case we need an open_confirm, no cleanup! */
2145 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2146 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002147 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002148 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002149 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002150out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002151 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002152}
2153
2154static const struct rpc_call_ops nfs4_open_ops = {
2155 .rpc_call_prepare = nfs4_open_prepare,
2156 .rpc_call_done = nfs4_open_done,
2157 .rpc_release = nfs4_open_release,
2158};
2159
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002160static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002161{
David Howells2b0143b2015-03-17 22:25:59 +00002162 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002163 struct nfs_server *server = NFS_SERVER(dir);
2164 struct nfs_openargs *o_arg = &data->o_arg;
2165 struct nfs_openres *o_res = &data->o_res;
2166 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002167 struct rpc_message msg = {
2168 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2169 .rpc_argp = o_arg,
2170 .rpc_resp = o_res,
2171 .rpc_cred = data->owner->so_cred,
2172 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002173 struct rpc_task_setup task_setup_data = {
2174 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002175 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002176 .callback_ops = &nfs4_open_ops,
2177 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002178 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002179 .flags = RPC_TASK_ASYNC,
2180 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002181 int status;
2182
Chuck Levera9c92d62013-08-09 12:48:18 -04002183 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002184 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002185 data->rpc_done = 0;
2186 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002187 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002188 data->is_recover = 0;
2189 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002190 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002191 data->is_recover = 1;
2192 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002193 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002194 if (IS_ERR(task))
2195 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002196 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002197 if (status != 0) {
2198 data->cancelled = 1;
2199 smp_wmb();
2200 } else
2201 status = data->rpc_status;
2202 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002203
2204 return status;
2205}
2206
2207static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2208{
David Howells2b0143b2015-03-17 22:25:59 +00002209 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002210 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002211 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002212
2213 status = nfs4_run_open_task(data, 1);
2214 if (status != 0 || !data->rpc_done)
2215 return status;
2216
Trond Myklebust6926afd2012-01-07 13:22:46 -05002217 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2218
Anna Schumakerd7e98252017-01-11 16:13:29 -05002219 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002220 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002221
2222 return status;
2223}
2224
Trond Myklebustf3792d62014-07-10 08:54:32 -04002225/*
2226 * Additional permission checks in order to distinguish between an
2227 * open for read, and an open for execute. This works around the
2228 * fact that NFSv4 OPEN treats read and execute permissions as being
2229 * the same.
2230 * Note that in the non-execute case, we want to turn off permission
2231 * checking if we just created a new file (POSIX open() semantics).
2232 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002233static int nfs4_opendata_access(struct rpc_cred *cred,
2234 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002235 struct nfs4_state *state, fmode_t fmode,
2236 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002237{
2238 struct nfs_access_entry cache;
2239 u32 mask;
2240
2241 /* access call failed or for some reason the server doesn't
2242 * support any access modes -- defer access call until later */
2243 if (opendata->o_res.access_supported == 0)
2244 return 0;
2245
2246 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002247 /*
2248 * Use openflags to check for exec, because fmode won't
2249 * always have FMODE_EXEC set when file open for exec.
2250 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002251 if (openflags & __FMODE_EXEC) {
2252 /* ONLY check for exec rights */
2253 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002254 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002255 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002256
2257 cache.cred = cred;
2258 cache.jiffies = jiffies;
2259 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2260 nfs_access_add_cache(state->inode, &cache);
2261
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002262 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002263 return 0;
2264
2265 /* even though OPEN succeeded, access is denied. Close the file */
2266 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002267 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002268}
2269
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002270/*
2271 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2272 */
2273static int _nfs4_proc_open(struct nfs4_opendata *data)
2274{
David Howells2b0143b2015-03-17 22:25:59 +00002275 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002276 struct nfs_server *server = NFS_SERVER(dir);
2277 struct nfs_openargs *o_arg = &data->o_arg;
2278 struct nfs_openres *o_res = &data->o_res;
2279 int status;
2280
2281 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002282 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002283 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002284 if (status != 0) {
2285 if (status == -NFS4ERR_BADNAME &&
2286 !(o_arg->open_flags & O_CREAT))
2287 return -ENOENT;
2288 return status;
2289 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002290
Trond Myklebust6926afd2012-01-07 13:22:46 -05002291 nfs_fattr_map_and_free_names(server, &data->f_attr);
2292
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002293 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002294 if (o_arg->open_flags & O_EXCL)
2295 data->file_created = 1;
2296 else if (o_res->cinfo.before != o_res->cinfo.after)
2297 data->file_created = 1;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002298 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002299 update_changeattr(dir, &o_res->cinfo,
2300 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002301 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002302 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2303 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002305 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002307 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
2309 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002310 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002311 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314/*
2315 * OPEN_EXPIRED:
2316 * reclaim state on the server after a network partition.
2317 * Assumes caller holds the appropriate lock
2318 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002319static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002321 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002322 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002324 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002325 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002326 if (IS_ERR(opendata))
2327 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002328 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002329 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002330 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002331 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002332 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333}
2334
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002335static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002336{
Trond Myklebust539cd032007-06-05 11:46:42 -04002337 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002338 struct nfs4_exception exception = { };
2339 int err;
2340
2341 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002342 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002343 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002344 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2345 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002346 switch (err) {
2347 default:
2348 goto out;
2349 case -NFS4ERR_GRACE:
2350 case -NFS4ERR_DELAY:
2351 nfs4_handle_exception(server, err, &exception);
2352 err = 0;
2353 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002354 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002355out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002356 return err;
2357}
2358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2360{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002362 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Trond Myklebust864472e2006-01-03 09:55:15 +01002364 ctx = nfs4_state_find_open_context(state);
2365 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002366 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002367 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002368 put_nfs_open_context(ctx);
2369 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370}
2371
Trond Myklebust41020b62016-09-22 13:38:58 -04002372static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2373 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002374{
Trond Myklebust41020b62016-09-22 13:38:58 -04002375 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002376 write_seqlock(&state->seqlock);
2377 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2378 write_sequnlock(&state->seqlock);
2379 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2380}
2381
2382static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2383{
2384 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002385 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002386}
2387
2388static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2389{
2390 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2391 nfs40_clear_delegation_stateid(state);
2392 return nfs4_open_expired(sp, state);
2393}
2394
Trond Myklebust45870d62016-09-22 13:38:59 -04002395static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2396 nfs4_stateid *stateid,
2397 struct rpc_cred *cred)
2398{
2399 return -NFS4ERR_BAD_STATEID;
2400}
2401
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002402#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002403static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2404 nfs4_stateid *stateid,
2405 struct rpc_cred *cred)
2406{
2407 int status;
2408
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002409 switch (stateid->type) {
2410 default:
2411 break;
2412 case NFS4_INVALID_STATEID_TYPE:
2413 case NFS4_SPECIAL_STATEID_TYPE:
2414 return -NFS4ERR_BAD_STATEID;
2415 case NFS4_REVOKED_STATEID_TYPE:
2416 goto out_free;
2417 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002418
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002419 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002420 switch (status) {
2421 case -NFS4ERR_EXPIRED:
2422 case -NFS4ERR_ADMIN_REVOKED:
2423 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002424 break;
2425 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002426 return status;
2427 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002428out_free:
2429 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002430 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002431 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002432}
2433
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002434static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002435{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002436 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002437 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002438 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002439 struct rpc_cred *cred;
2440 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002441
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002442 /* Get the delegation credential for use by test/free_stateid */
2443 rcu_read_lock();
2444 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002445 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002446 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002447 return;
2448 }
2449
2450 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002451 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2452 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002453 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002454 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002455 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002456
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002457 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2458 rcu_read_unlock();
2459 return;
2460 }
2461
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002462 cred = get_rpccred(delegation->cred);
2463 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002464 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002465 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002466 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002467 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002468
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002469 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002470}
2471
2472/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002473 * nfs41_check_expired_locks - possibly free a lock stateid
2474 *
2475 * @state: NFSv4 state for an inode
2476 *
2477 * Returns NFS_OK if recovery for this stateid is now finished.
2478 * Otherwise a negative NFS4ERR value is returned.
2479 */
2480static int nfs41_check_expired_locks(struct nfs4_state *state)
2481{
2482 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002483 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002484 struct nfs_server *server = NFS_SERVER(state->inode);
2485
2486 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2487 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002488
2489 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002490 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2491 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2492 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2493
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002494 atomic_inc(&lsp->ls_count);
2495 spin_unlock(&state->state_lock);
2496
2497 nfs4_put_lock_state(prev);
2498 prev = lsp;
2499
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002500 status = nfs41_test_and_free_expired_stateid(server,
2501 &lsp->ls_stateid,
2502 cred);
2503 trace_nfs4_test_lock_stateid(state, lsp, status);
2504 if (status == -NFS4ERR_EXPIRED ||
2505 status == -NFS4ERR_BAD_STATEID) {
2506 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002507 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002508 if (!recover_lost_locks)
2509 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2510 } else if (status != NFS_OK) {
2511 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002512 nfs4_put_lock_state(prev);
2513 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002514 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002515 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002516 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002517 }
2518 spin_unlock(&state->state_lock);
2519 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002520out:
2521 return ret;
2522}
2523
2524/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002525 * nfs41_check_open_stateid - possibly free an open stateid
2526 *
2527 * @state: NFSv4 state for an inode
2528 *
2529 * Returns NFS_OK if recovery for this stateid is now finished.
2530 * Otherwise a negative NFS4ERR value is returned.
2531 */
2532static int nfs41_check_open_stateid(struct nfs4_state *state)
2533{
2534 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002535 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002536 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002537 int status;
2538
Trond Myklebustb134fc42016-09-22 13:39:16 -04002539 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002540 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2541 if (nfs4_have_delegation(state->inode, state->state))
2542 return NFS_OK;
2543 return -NFS4ERR_OPENMODE;
2544 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002545 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002546 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002547 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002548 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002549 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002550 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2551 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2552 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002553 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002554 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002555 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002556 if (status != NFS_OK)
2557 return status;
2558 if (nfs_open_stateid_recover_openmode(state))
2559 return -NFS4ERR_OPENMODE;
2560 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002561}
2562
2563static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2564{
Chuck Levereb64cf92012-07-11 16:30:05 -04002565 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002566
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002567 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002568 status = nfs41_check_expired_locks(state);
2569 if (status != NFS_OK)
2570 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002571 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002572 if (status != NFS_OK)
2573 status = nfs4_open_expired(sp, state);
2574 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002575}
2576#endif
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002579 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2580 * fields corresponding to attributes that were used to store the verifier.
2581 * Make sure we clobber those fields in the later setattr call
2582 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002583static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2584 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002585{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002586 const u32 *attrset = opendata->o_res.attrset;
2587
2588 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002589 !(sattr->ia_valid & ATTR_ATIME_SET))
2590 sattr->ia_valid |= ATTR_ATIME;
2591
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002592 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002593 !(sattr->ia_valid & ATTR_MTIME_SET))
2594 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002595
2596 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002597 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2598 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002599 sattr->ia_valid &= ~ATTR_MODE;
2600
2601 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2602 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002603}
2604
Trond Myklebustc21443c2013-02-07 14:26:21 -05002605static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2606 fmode_t fmode,
2607 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002608 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002609{
2610 struct nfs4_state_owner *sp = opendata->owner;
2611 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002612 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002613 struct nfs4_state *state;
2614 unsigned int seq;
2615 int ret;
2616
2617 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2618
2619 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002620 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002621 goto out;
2622
2623 state = nfs4_opendata_to_nfs4_state(opendata);
2624 ret = PTR_ERR(state);
2625 if (IS_ERR(state))
2626 goto out;
2627 if (server->caps & NFS_CAP_POSIX_LOCK)
2628 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002629 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2630 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002631
Trond Myklebust275bb302013-05-29 13:11:28 -04002632 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002633 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002634 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002635 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002636 alias = d_exact_alias(dentry, state->inode);
2637 if (!alias)
2638 alias = d_splice_alias(igrab(state->inode), dentry);
2639 /* d_splice_alias() can't fail here - it's a non-directory */
2640 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002641 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002642 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002643 }
2644 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002645 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002646 }
2647
Trond Myklebustc21443c2013-02-07 14:26:21 -05002648 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2649 if (ret != 0)
2650 goto out;
2651
Trond Myklebust3efb9722013-05-29 13:17:04 -04002652 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002653 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002654 nfs_inode_attach_open_context(ctx);
2655 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2656 nfs4_schedule_stateid_recovery(server, state);
2657 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002658out:
2659 return ret;
2660}
2661
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002662/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002663 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 */
Andy Adamson82be4172012-05-23 05:02:35 -04002665static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002666 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002667 int flags,
2668 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002669 struct nfs4_label *label,
2670 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671{
2672 struct nfs4_state_owner *sp;
2673 struct nfs4_state *state = NULL;
2674 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002675 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002676 struct dentry *dentry = ctx->dentry;
2677 struct rpc_cred *cred = ctx->cred;
2678 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2679 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002680 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002681 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002682 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
2684 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002686 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2687 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2689 goto out_err;
2690 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002691 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002692 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002693 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002694 if (d_really_is_positive(dentry))
2695 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002696 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002697 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002698 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002699 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002700 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002701 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002702 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
David Quigley14c43f72013-05-22 12:50:43 -04002704 if (label) {
2705 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2706 if (IS_ERR(olabel)) {
2707 status = PTR_ERR(olabel);
2708 goto err_opendata_put;
2709 }
2710 }
2711
Trond Myklebuste911b812014-03-26 13:24:37 -07002712 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2713 if (!opendata->f_attr.mdsthreshold) {
2714 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2715 if (!opendata->f_attr.mdsthreshold)
2716 goto err_free_label;
2717 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002718 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002719 }
David Howells2b0143b2015-03-17 22:25:59 +00002720 if (d_really_is_positive(dentry))
2721 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002722
Trond Myklebust3efb9722013-05-29 13:17:04 -04002723 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002724 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002725 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002726 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002727
NeilBrownefcbc042015-07-30 13:00:56 +10002728 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002729 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002730 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002731 /*
2732 * send create attributes which was not set by open
2733 * with an extra setattr.
2734 */
2735 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2736 nfs_fattr_init(opendata->o_res.f_attr);
2737 status = nfs4_do_setattr(state->inode, cred,
2738 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002739 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002740 if (status == 0) {
2741 nfs_setattr_update_inode(state->inode, sattr,
2742 opendata->o_res.f_attr);
2743 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2744 }
David Quigley1775fd32013-05-22 12:50:42 -04002745 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002746 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002747 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002748 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002749
Trond Myklebuste911b812014-03-26 13:24:37 -07002750 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002751 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002752 opendata->f_attr.mdsthreshold = NULL;
2753 }
Andy Adamson82be4172012-05-23 05:02:35 -04002754
David Quigley14c43f72013-05-22 12:50:43 -04002755 nfs4_label_free(olabel);
2756
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002757 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002760err_free_label:
2761 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002762err_opendata_put:
2763 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002764err_put_state_owner:
2765 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 return status;
2768}
2769
2770
Andy Adamson82be4172012-05-23 05:02:35 -04002771static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002772 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002773 int flags,
2774 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002775 struct nfs4_label *label,
2776 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002778 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 struct nfs4_exception exception = { };
2780 struct nfs4_state *res;
2781 int status;
2782
2783 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002784 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002785 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002786 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 if (status == 0)
2788 break;
2789 /* NOTE: BAD_SEQID means the server and client disagree about the
2790 * book-keeping w.r.t. state-changing operations
2791 * (OPEN/CLOSE/LOCK/LOCKU...)
2792 * It is actually a sign of a bug on the client or on the server.
2793 *
2794 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002795 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 * have unhashed the old state_owner for us, and that we can
2797 * therefore safely retry using a new one. We should still warn
2798 * the user though...
2799 */
2800 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002801 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002802 " returned a bad sequence-id error!\n",
2803 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 exception.retry = 1;
2805 continue;
2806 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002807 /*
2808 * BAD_STATEID on OPEN means that the server cancelled our
2809 * state before it received the OPEN_CONFIRM.
2810 * Recover by retrying the request as per the discussion
2811 * on Page 181 of RFC3530.
2812 */
2813 if (status == -NFS4ERR_BAD_STATEID) {
2814 exception.retry = 1;
2815 continue;
2816 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002817 if (status == -EAGAIN) {
2818 /* We must have found a delegation */
2819 exception.retry = 1;
2820 continue;
2821 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002822 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2823 continue;
2824 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 status, &exception));
2826 } while (exception.retry);
2827 return res;
2828}
2829
Trond Myklebust8487c472016-06-26 08:44:35 -04002830static int _nfs4_do_setattr(struct inode *inode,
2831 struct nfs_setattrargs *arg,
2832 struct nfs_setattrres *res,
2833 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002834 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002835{
2836 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002837 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04002838 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2839 .rpc_argp = arg,
2840 .rpc_resp = res,
2841 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002842 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002843 struct rpc_cred *delegation_cred = NULL;
2844 unsigned long timestamp = jiffies;
2845 fmode_t fmode;
2846 bool truncate;
2847 int status;
2848
2849 nfs_fattr_init(res->fattr);
2850
2851 /* Servers should only apply open mode checks for file size changes */
2852 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2853 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2854
2855 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2856 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002857 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002858 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002859 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002860 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002861 l_ctx = nfs_get_lock_context(ctx);
2862 if (IS_ERR(l_ctx))
2863 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002864 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2865 &arg->stateid, &delegation_cred);
2866 nfs_put_lock_context(l_ctx);
2867 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002868 return -EBADF;
2869 } else
2870 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2871 if (delegation_cred)
2872 msg.rpc_cred = delegation_cred;
2873
2874 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2875
2876 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002877 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002878 renew_lease(server, timestamp);
2879 trace_nfs4_setattr(inode, &arg->stateid, status);
2880 return status;
2881}
2882
2883static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2884 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002885 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002886 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002888 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002889 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002890 struct nfs_setattrargs arg = {
2891 .fh = NFS_FH(inode),
2892 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 .server = server,
2894 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002895 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002896 };
2897 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002899 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002901 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002902 struct nfs4_exception exception = {
2903 .state = state,
2904 .inode = inode,
2905 .stateid = &arg.stateid,
2906 };
2907 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
David Quigleyaa9c2662013-05-22 12:50:44 -04002909 arg.bitmask = nfs4_bitmask(server, ilabel);
2910 if (ilabel)
2911 arg.bitmask = nfs4_bitmask(server, olabel);
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 do {
NeilBrown29b59f92016-10-13 15:26:47 +11002914 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04002915 switch (err) {
2916 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002917 if (!(sattr->ia_valid & ATTR_SIZE)) {
2918 pr_warn_once("NFSv4: server %s is incorrectly "
2919 "applying open mode checks to "
2920 "a SETATTR that is not "
2921 "changing file size.\n",
2922 server->nfs_client->cl_hostname);
2923 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002924 if (state && !(state->state & FMODE_WRITE)) {
2925 err = -EBADF;
2926 if (sattr->ia_valid & ATTR_OPEN)
2927 err = -EACCES;
2928 goto out;
2929 }
2930 }
2931 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002933out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 return err;
2935}
2936
Peng Tao500d7012015-09-22 11:35:22 +08002937static bool
2938nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2939{
2940 if (inode == NULL || !nfs_have_layout(inode))
2941 return false;
2942
2943 return pnfs_wait_on_layoutreturn(inode, task);
2944}
2945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946struct nfs4_closedata {
2947 struct inode *inode;
2948 struct nfs4_state *state;
2949 struct nfs_closeargs arg;
2950 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05002951 struct {
2952 struct nfs4_layoutreturn_args arg;
2953 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04002954 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05002955 u32 roc_barrier;
2956 bool roc;
2957 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002958 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002959 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960};
2961
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002962static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002963{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002964 struct nfs4_closedata *calldata = data;
2965 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002966 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002967
Trond Myklebustcf805162016-11-15 14:56:07 -05002968 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05002969 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
2970 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07002971 nfs4_put_open_state(calldata->state);
2972 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002973 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002974 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002975 kfree(calldata);
2976}
2977
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002978static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002980 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002983 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Chuck Levera3ca5652012-03-01 17:00:40 -05002985 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002986 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2987 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002988 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05002989
2990 /* Handle Layoutreturn errors */
2991 if (calldata->arg.lr_args && task->tk_status != 0) {
2992 switch (calldata->res.lr_ret) {
2993 default:
2994 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
2995 break;
2996 case 0:
2997 calldata->arg.lr_args = NULL;
2998 calldata->res.lr_res = NULL;
2999 break;
3000 case -NFS4ERR_ADMIN_REVOKED:
3001 case -NFS4ERR_DELEG_REVOKED:
3002 case -NFS4ERR_EXPIRED:
3003 case -NFS4ERR_BAD_STATEID:
3004 case -NFS4ERR_OLD_STATEID:
3005 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3006 case -NFS4ERR_WRONG_CRED:
3007 calldata->arg.lr_args = NULL;
3008 calldata->res.lr_res = NULL;
3009 calldata->res.lr_ret = 0;
3010 rpc_restart_call_prepare(task);
3011 return;
3012 }
3013 }
3014
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003015 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 * the state_owner. we keep this around to process errors
3017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 switch (task->tk_status) {
3019 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003020 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003021 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003022 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003023 case -NFS4ERR_ACCESS:
3024 if (calldata->arg.bitmask != NULL) {
3025 calldata->arg.bitmask = NULL;
3026 calldata->res.fattr = NULL;
3027 task->tk_status = 0;
3028 rpc_restart_call_prepare(task);
3029 goto out_release;
3030
3031 }
3032 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003033 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003035 case -NFS4ERR_EXPIRED:
3036 nfs4_free_revoked_stateid(server,
3037 &calldata->arg.stateid,
3038 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003039 case -NFS4ERR_OLD_STATEID:
3040 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003041 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003042 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003043 rpc_restart_call_prepare(task);
3044 goto out_release;
3045 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003046 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003047 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003049 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003050 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003051 goto out_release;
3052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003054 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3055 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003056out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003057 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003058 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003059 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060}
3061
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003062static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003064 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003065 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003066 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003067 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003068 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003069
Chuck Levera3ca5652012-03-01 17:00:40 -05003070 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003071 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003072 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003073
Trond Myklebust88069f72009-12-08 08:33:16 -05003074 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003075 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003076 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3077 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3078 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003079 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003080 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003081 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003082 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003083 if (state->n_rdonly == 0)
3084 call_close |= is_rdonly;
3085 else if (is_rdonly)
3086 calldata->arg.fmode |= FMODE_READ;
3087 if (state->n_wronly == 0)
3088 call_close |= is_wronly;
3089 else if (is_wronly)
3090 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003091 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3092 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003093 } else if (is_rdwr)
3094 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3095
Trond Myklebust5cc78612016-11-14 11:19:56 -05003096 if (!nfs4_valid_open_stateid(state) ||
3097 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003098 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003099 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003100
3101 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003102 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003103 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003104 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003105
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003106 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003107 nfs_release_seqid(calldata->arg.seqid);
3108 goto out_wait;
3109 }
3110
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003111 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003112 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003113
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003114 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003115 /* Close-to-open cache consistency revalidation */
3116 if (!nfs4_have_delegation(inode, FMODE_READ))
3117 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3118 else
3119 calldata->arg.bitmask = NULL;
3120 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003121
Trond Myklebust6ae37332015-01-30 14:21:14 -05003122 calldata->arg.share_access =
3123 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3124 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003125
Trond Myklebustd8d84982016-12-19 12:14:44 -05003126 if (calldata->res.fattr == NULL)
3127 calldata->arg.bitmask = NULL;
3128 else if (calldata->arg.bitmask == NULL)
3129 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003130 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003131 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003132 &calldata->arg.seq_args,
3133 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003134 task) != 0)
3135 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003136 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003137 return;
3138out_no_action:
3139 task->tk_action = NULL;
3140out_wait:
3141 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142}
3143
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003144static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003145 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003146 .rpc_call_done = nfs4_close_done,
3147 .rpc_release = nfs4_free_closedata,
3148};
3149
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150/*
3151 * It is possible for data to be read/written from a mem-mapped file
3152 * after the sys_close call (which hits the vfs layer as a flush).
3153 * This means that we can't safely call nfsv4 close on a file until
3154 * the inode is cleared. This in turn means that we are not good
3155 * NFSv4 citizens - we do not indicate to the server to update the file's
3156 * share state even when we are done with one of the three share
3157 * stateid's in the inode.
3158 *
3159 * NOTE: Caller must be holding the sp->so_owner semaphore!
3160 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003161int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003163 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003164 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003166 struct nfs4_state_owner *sp = state->owner;
3167 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003168 struct rpc_message msg = {
3169 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3170 .rpc_cred = state->owner->so_cred,
3171 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003172 struct rpc_task_setup task_setup_data = {
3173 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003174 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003175 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003176 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003177 .flags = RPC_TASK_ASYNC,
3178 };
Trond Myklebust95121352005-10-18 14:20:12 -07003179 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003181 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3182 &task_setup_data.rpc_client, &msg);
3183
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003184 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003186 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003187 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003188 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003190 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003192 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3193 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003194 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003195 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003196 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003197 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003198 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003199 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003200 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003201 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003202 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003203 calldata->lr.roc = pnfs_roc(state->inode,
3204 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3205 if (calldata->lr.roc) {
3206 calldata->arg.lr_args = &calldata->lr.arg;
3207 calldata->res.lr_res = &calldata->lr.res;
3208 }
Al Viro643168c2011-06-22 18:20:23 -04003209 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003210
Trond Myklebust1174dd12010-12-21 10:52:24 -05003211 msg.rpc_argp = &calldata->arg;
3212 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003213 task_setup_data.callback_data = calldata;
3214 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003215 if (IS_ERR(task))
3216 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003217 status = 0;
3218 if (wait)
3219 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003220 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003221 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003222out_free_calldata:
3223 kfree(calldata);
3224out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003225 nfs4_put_open_state(state);
3226 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003227 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228}
3229
Trond Myklebust2b484292010-09-17 10:56:51 -04003230static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003231nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3232 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003235 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3236
3237 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Trond Myklebust565277f2007-10-15 18:17:53 -04003239 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003240 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003241
3242 nfs4_label_release_security(label);
3243
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003244 if (IS_ERR(state))
3245 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003246 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247}
3248
Trond Myklebust1185a552009-12-03 15:54:02 -05003249static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003250{
3251 if (ctx->state == NULL)
3252 return;
3253 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003254 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003255 else
Al Viro643168c2011-06-22 18:20:23 -04003256 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003257}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
Trond Myklebustb944dba2013-11-04 15:20:20 -05003259#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3260#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003261#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003262
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3264{
Kinglong Mee8c612822015-08-26 21:12:58 +08003265 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003266 struct nfs4_server_caps_arg args = {
3267 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003268 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003269 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 struct nfs4_server_caps_res res = {};
3271 struct rpc_message msg = {
3272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003273 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 .rpc_resp = &res,
3275 };
3276 int status;
3277
Kinglong Mee8c612822015-08-26 21:12:58 +08003278 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3279 FATTR4_WORD0_FH_EXPIRE_TYPE |
3280 FATTR4_WORD0_LINK_SUPPORT |
3281 FATTR4_WORD0_SYMLINK_SUPPORT |
3282 FATTR4_WORD0_ACLSUPPORT;
3283 if (minorversion)
3284 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3285
Bryan Schumaker7c513052011-03-24 17:12:24 +00003286 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003288 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003289 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003290 case 0:
3291 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3292 res.attr_bitmask[2] = 0;
3293 break;
3294 case 1:
3295 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3296 break;
3297 case 2:
3298 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003301 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3302 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3303 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3304 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003305 NFS_CAP_CTIME|NFS_CAP_MTIME|
3306 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003307 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3308 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 server->caps |= NFS_CAP_ACLS;
3310 if (res.has_links != 0)
3311 server->caps |= NFS_CAP_HARDLINKS;
3312 if (res.has_symlinks != 0)
3313 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003314 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3315 server->caps |= NFS_CAP_FILEID;
3316 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3317 server->caps |= NFS_CAP_MODE;
3318 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3319 server->caps |= NFS_CAP_NLINK;
3320 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3321 server->caps |= NFS_CAP_OWNER;
3322 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3323 server->caps |= NFS_CAP_OWNER_GROUP;
3324 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3325 server->caps |= NFS_CAP_ATIME;
3326 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3327 server->caps |= NFS_CAP_CTIME;
3328 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3329 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003330#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3331 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3332 server->caps |= NFS_CAP_SECURITY_LABEL;
3333#endif
3334 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3335 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003336 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003337
Trond Myklebusta65318b2009-03-11 14:10:28 -04003338 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3339 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3340 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003341 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003342 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3343 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003345 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003347
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 return status;
3349}
3350
Trond Myklebust55a97592006-06-09 09:34:19 -04003351int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352{
3353 struct nfs4_exception exception = { };
3354 int err;
3355 do {
3356 err = nfs4_handle_exception(server,
3357 _nfs4_server_capabilities(server, fhandle),
3358 &exception);
3359 } while (exception.retry);
3360 return err;
3361}
3362
3363static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3364 struct nfs_fsinfo *info)
3365{
David Quigleyaa9c2662013-05-22 12:50:44 -04003366 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003368 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 };
3370 struct nfs4_lookup_res res = {
3371 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003372 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 .fh = fhandle,
3374 };
3375 struct rpc_message msg = {
3376 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3377 .rpc_argp = &args,
3378 .rpc_resp = &res,
3379 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003380
David Quigleyaa9c2662013-05-22 12:50:44 -04003381 bitmask[0] = nfs4_fattr_bitmap[0];
3382 bitmask[1] = nfs4_fattr_bitmap[1];
3383 /*
3384 * Process the label in the upcoming getfattr
3385 */
3386 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3387
Trond Myklebust0e574af2005-10-27 22:12:38 -04003388 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003389 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390}
3391
3392static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3393 struct nfs_fsinfo *info)
3394{
3395 struct nfs4_exception exception = { };
3396 int err;
3397 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003398 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003399 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003400 switch (err) {
3401 case 0:
3402 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003403 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003404 default:
3405 err = nfs4_handle_exception(server, err, &exception);
3406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003408out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 return err;
3410}
3411
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003412static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3413 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3414{
Trond Myklebustc2190662013-08-26 19:23:04 -04003415 struct rpc_auth_create_args auth_args = {
3416 .pseudoflavor = flavor,
3417 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003418 struct rpc_auth *auth;
3419 int ret;
3420
Trond Myklebustc2190662013-08-26 19:23:04 -04003421 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003422 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003423 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003424 goto out;
3425 }
3426 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003427out:
3428 return ret;
3429}
3430
Chuck Lever9a744ba2013-03-16 15:56:02 -04003431/*
3432 * Retry pseudoroot lookup with various security flavors. We do this when:
3433 *
3434 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3435 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3436 *
3437 * Returns zero on success, or a negative NFS4ERR value, or a
3438 * negative errno value.
3439 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003440static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003441 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003443 /* Per 3530bis 15.33.5 */
3444 static const rpc_authflavor_t flav_array[] = {
3445 RPC_AUTH_GSS_KRB5P,
3446 RPC_AUTH_GSS_KRB5I,
3447 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003448 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003449 RPC_AUTH_NULL,
3450 };
3451 int status = -EPERM;
3452 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003454 if (server->auth_info.flavor_len > 0) {
3455 /* try each flavor specified by user */
3456 for (i = 0; i < server->auth_info.flavor_len; i++) {
3457 status = nfs4_lookup_root_sec(server, fhandle, info,
3458 server->auth_info.flavors[i]);
3459 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3460 continue;
3461 break;
3462 }
3463 } else {
3464 /* no flavors specified by user, try default list */
3465 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3466 status = nfs4_lookup_root_sec(server, fhandle, info,
3467 flav_array[i]);
3468 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3469 continue;
3470 break;
3471 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003472 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003473
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003474 /*
3475 * -EACCESS could mean that the user doesn't have correct permissions
3476 * to access the mount. It could also mean that we tried to mount
3477 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3478 * existing mount programs don't handle -EACCES very well so it should
3479 * be mapped to -EPERM instead.
3480 */
3481 if (status == -EACCES)
3482 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003483 return status;
3484}
3485
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003486/**
3487 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3488 * @server: initialized nfs_server handle
3489 * @fhandle: we fill in the pseudo-fs root file handle
3490 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003491 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003492 *
3493 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003494 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003495int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003496 struct nfs_fsinfo *info,
3497 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003498{
Andre Przywarac7757072015-04-23 17:17:40 +01003499 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003500
Andre Przywarac7757072015-04-23 17:17:40 +01003501 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003502 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003503
3504 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003505 status = server->nfs_client->cl_mvops->find_root_sec(server,
3506 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003507
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 if (status == 0)
3509 status = nfs4_server_capabilities(server, fhandle);
3510 if (status == 0)
3511 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003512
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003513 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514}
3515
Bryan Schumakerbae36242012-05-10 15:07:31 -04003516static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3517 struct nfs_fsinfo *info)
3518{
3519 int error;
3520 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003521 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003522
3523 error = nfs4_server_capabilities(server, mntfh);
3524 if (error < 0) {
3525 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3526 return error;
3527 }
3528
David Quigley14c43f72013-05-22 12:50:43 -04003529 label = nfs4_label_alloc(server, GFP_KERNEL);
3530 if (IS_ERR(label))
3531 return PTR_ERR(label);
3532
David Quigley1775fd32013-05-22 12:50:42 -04003533 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003534 if (error < 0) {
3535 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003536 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003537 }
3538
3539 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3540 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3541 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3542
David Quigley14c43f72013-05-22 12:50:43 -04003543err_free_label:
3544 nfs4_label_free(label);
3545
Bryan Schumakerbae36242012-05-10 15:07:31 -04003546 return error;
3547}
3548
Manoj Naik6b97fd32006-06-09 09:34:29 -04003549/*
3550 * Get locations and (maybe) other attributes of a referral.
3551 * Note that we'll actually follow the referral later when
3552 * we detect fsid mismatch in inode revalidation
3553 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003554static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3555 const struct qstr *name, struct nfs_fattr *fattr,
3556 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003557{
3558 int status = -ENOMEM;
3559 struct page *page = NULL;
3560 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003561
3562 page = alloc_page(GFP_KERNEL);
3563 if (page == NULL)
3564 goto out;
3565 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3566 if (locations == NULL)
3567 goto out;
3568
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003569 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003570 if (status != 0)
3571 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003572
3573 /*
3574 * If the fsid didn't change, this is a migration event, not a
3575 * referral. Cause us to drop into the exception handler, which
3576 * will kick off migration recovery.
3577 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003578 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003579 dprintk("%s: server did not return a different fsid for"
3580 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003581 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003582 goto out;
3583 }
Andy Adamson533eb462011-06-13 18:25:56 -04003584 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3585 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003586
Andy Adamson533eb462011-06-13 18:25:56 -04003587 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003588 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003589 memset(fhandle, 0, sizeof(struct nfs_fh));
3590out:
3591 if (page)
3592 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003593 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003594 return status;
3595}
3596
David Quigley1775fd32013-05-22 12:50:42 -04003597static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3598 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599{
3600 struct nfs4_getattr_arg args = {
3601 .fh = fhandle,
3602 .bitmask = server->attr_bitmask,
3603 };
3604 struct nfs4_getattr_res res = {
3605 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003606 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 .server = server,
3608 };
3609 struct rpc_message msg = {
3610 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3611 .rpc_argp = &args,
3612 .rpc_resp = &res,
3613 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003614
3615 args.bitmask = nfs4_bitmask(server, label);
3616
Trond Myklebust0e574af2005-10-27 22:12:38 -04003617 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003618 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619}
3620
David Quigley1775fd32013-05-22 12:50:42 -04003621static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3622 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623{
3624 struct nfs4_exception exception = { };
3625 int err;
3626 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003627 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3628 trace_nfs4_getattr(server, fhandle, fattr, err);
3629 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 &exception);
3631 } while (exception.retry);
3632 return err;
3633}
3634
3635/*
3636 * The file is not closed if it is opened due to the a request to change
3637 * the size of the file. The open call will not be needed once the
3638 * VFS layer lookup-intents are implemented.
3639 *
3640 * Close is called when the inode is destroyed.
3641 * If we haven't opened the file for O_WRONLY, we
3642 * need to in the size_change case to obtain a stateid.
3643 *
3644 * Got race?
3645 * Because OPEN is always done by name in nfsv4, it is
3646 * possible that we opened a different file by the same
3647 * name. We can recognize this race condition, but we
3648 * can't do anything about it besides returning an error.
3649 *
3650 * This will be fixed with VFS changes (lookup-intent).
3651 */
3652static int
3653nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3654 struct iattr *sattr)
3655{
David Howells2b0143b2015-03-17 22:25:59 +00003656 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003657 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003658 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003659 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 int status;
3661
Peng Tao88ac8152014-09-12 11:04:10 +08003662 if (pnfs_ld_layoutret_on_setattr(inode) &&
3663 sattr->ia_valid & ATTR_SIZE &&
3664 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003665 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003666
Trond Myklebust0e574af2005-10-27 22:12:38 -04003667 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
Andy Adamson26699402012-05-30 16:12:24 -04003669 /* Deal with open(O_TRUNC) */
3670 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003671 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003672
3673 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003674 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003675 return 0;
3676
Trond Myklebustd5308382005-11-04 15:33:38 -05003677 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003678 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003679
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003680 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003681 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003682 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003683 }
3684
David Quigley14c43f72013-05-22 12:50:43 -04003685 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3686 if (IS_ERR(label))
3687 return PTR_ERR(label);
3688
NeilBrown29b59f92016-10-13 15:26:47 +11003689 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003690 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003691 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003692 nfs_setsecurity(inode, fattr, label);
3693 }
David Quigley14c43f72013-05-22 12:50:43 -04003694 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 return status;
3696}
3697
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003698static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3699 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003700 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003701{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003702 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003703 int status;
3704 struct nfs4_lookup_arg args = {
3705 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003706 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003707 .name = name,
3708 };
3709 struct nfs4_lookup_res res = {
3710 .server = server,
3711 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003712 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003713 .fh = fhandle,
3714 };
3715 struct rpc_message msg = {
3716 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3717 .rpc_argp = &args,
3718 .rpc_resp = &res,
3719 };
3720
David Quigleyaa9c2662013-05-22 12:50:44 -04003721 args.bitmask = nfs4_bitmask(server, label);
3722
David Howells2b3de442006-08-22 20:06:09 -04003723 nfs_fattr_init(fattr);
3724
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003726 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 dprintk("NFS reply lookup: %d\n", status);
3728 return status;
3729}
3730
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003731static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003732{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003733 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003734 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003735 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3736 fattr->nlink = 2;
3737}
3738
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003739static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003740 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003741 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003742{
3743 struct nfs4_exception exception = { };
3744 struct rpc_clnt *client = *clnt;
3745 int err;
3746 do {
David Quigley1775fd32013-05-22 12:50:42 -04003747 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003748 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003749 switch (err) {
3750 case -NFS4ERR_BADNAME:
3751 err = -ENOENT;
3752 goto out;
3753 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003754 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003755 if (err == -NFS4ERR_MOVED)
3756 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003757 goto out;
3758 case -NFS4ERR_WRONGSEC:
3759 err = -EPERM;
3760 if (client != *clnt)
3761 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003762 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003763 if (IS_ERR(client))
3764 return PTR_ERR(client);
3765
3766 exception.retry = 1;
3767 break;
3768 default:
3769 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3770 }
3771 } while (exception.retry);
3772
3773out:
3774 if (err == 0)
3775 *clnt = client;
3776 else if (client != *clnt)
3777 rpc_shutdown_client(client);
3778
3779 return err;
3780}
3781
Al Virobeffb8f2016-07-20 16:34:42 -04003782static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003783 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3784 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003786 int status;
3787 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003788
David Quigley1775fd32013-05-22 12:50:42 -04003789 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003790 if (client != NFS_CLIENT(dir)) {
3791 rpc_shutdown_client(client);
3792 nfs_fixup_secinfo_attributes(fattr);
3793 }
3794 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795}
3796
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003797struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003798nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003799 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3800{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003801 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003802 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003803
David Quigley1775fd32013-05-22 12:50:42 -04003804 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003805 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003806 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003807 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003808}
3809
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3811{
Trond Myklebust76b32992007-08-10 17:45:11 -04003812 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 struct nfs4_accessargs args = {
3814 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003815 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003817 struct nfs4_accessres res = {
3818 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003819 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 struct rpc_message msg = {
3821 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3822 .rpc_argp = &args,
3823 .rpc_resp = &res,
3824 .rpc_cred = entry->cred,
3825 };
3826 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003827 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
3829 /*
3830 * Determine which access bits we want to ask for...
3831 */
3832 if (mode & MAY_READ)
3833 args.access |= NFS4_ACCESS_READ;
3834 if (S_ISDIR(inode->i_mode)) {
3835 if (mode & MAY_WRITE)
3836 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3837 if (mode & MAY_EXEC)
3838 args.access |= NFS4_ACCESS_LOOKUP;
3839 } else {
3840 if (mode & MAY_WRITE)
3841 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3842 if (mode & MAY_EXEC)
3843 args.access |= NFS4_ACCESS_EXECUTE;
3844 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003845
3846 res.fattr = nfs_alloc_fattr();
3847 if (res.fattr == NULL)
3848 return -ENOMEM;
3849
Bryan Schumaker7c513052011-03-24 17:12:24 +00003850 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003852 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003853 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003855 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 return status;
3857}
3858
3859static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3860{
3861 struct nfs4_exception exception = { };
3862 int err;
3863 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003864 err = _nfs4_proc_access(inode, entry);
3865 trace_nfs4_access(inode, err);
3866 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 &exception);
3868 } while (exception.retry);
3869 return err;
3870}
3871
3872/*
3873 * TODO: For the time being, we don't try to get any attributes
3874 * along with any of the zero-copy operations READ, READDIR,
3875 * READLINK, WRITE.
3876 *
3877 * In the case of the first three, we want to put the GETATTR
3878 * after the read-type operation -- this is because it is hard
3879 * to predict the length of a GETATTR response in v4, and thus
3880 * align the READ data correctly. This means that the GETATTR
3881 * may end up partially falling into the page cache, and we should
3882 * shift it into the 'tail' of the xdr_buf before processing.
3883 * To do this efficiently, we need to know the total length
3884 * of data received, which doesn't seem to be available outside
3885 * of the RPC layer.
3886 *
3887 * In the case of WRITE, we also want to put the GETATTR after
3888 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003889 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 *
3891 * Both of these changes to the XDR layer would in fact be quite
3892 * minor, but I decided to leave them for a subsequent patch.
3893 */
3894static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3895 unsigned int pgbase, unsigned int pglen)
3896{
3897 struct nfs4_readlink args = {
3898 .fh = NFS_FH(inode),
3899 .pgbase = pgbase,
3900 .pglen = pglen,
3901 .pages = &page,
3902 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003903 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 struct rpc_message msg = {
3905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3906 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003907 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 };
3909
Bryan Schumaker7c513052011-03-24 17:12:24 +00003910 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 -07003911}
3912
3913static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3914 unsigned int pgbase, unsigned int pglen)
3915{
3916 struct nfs4_exception exception = { };
3917 int err;
3918 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003919 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3920 trace_nfs4_readlink(inode, err);
3921 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 &exception);
3923 } while (exception.retry);
3924 return err;
3925}
3926
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003928 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930static int
3931nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003932 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003934 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04003935 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003936 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 int status = 0;
3939
NeilBrown532d4de2016-10-13 15:26:47 +11003940 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003941 if (IS_ERR(ctx))
3942 return PTR_ERR(ctx);
3943
David Quigleyaa9c2662013-05-22 12:50:44 -04003944 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3945
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003946 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
3947 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003948 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 if (IS_ERR(state)) {
3950 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003951 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003954 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003955 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 return status;
3957}
3958
Al Virobeffb8f2016-07-20 16:34:42 -04003959static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960{
Trond Myklebust16e42952005-10-27 22:12:44 -04003961 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003962 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003964 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003966 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003967 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003968 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3971 .rpc_argp = &args,
3972 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05003974 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04003975 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003976
Bryan Schumaker7c513052011-03-24 17:12:24 +00003977 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003978 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05003979 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 return status;
3981}
3982
Al Virobeffb8f2016-07-20 16:34:42 -04003983static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984{
3985 struct nfs4_exception exception = { };
3986 int err;
3987 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003988 err = _nfs4_proc_remove(dir, name);
3989 trace_nfs4_remove(dir, name, err);
3990 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 &exception);
3992 } while (exception.retry);
3993 return err;
3994}
3995
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003996static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003998 struct nfs_server *server = NFS_SERVER(dir);
3999 struct nfs_removeargs *args = msg->rpc_argp;
4000 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004002 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004004 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004005
4006 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007}
4008
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004009static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4010{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004011 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004012 &data->args.seq_args,
4013 &data->res.seq_res,
4014 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015}
4016
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004017static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004019 struct nfs_unlinkdata *data = task->tk_calldata;
4020 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004021
Trond Myklebust14516c32010-07-31 14:29:06 -04004022 if (!nfs4_sequence_done(task, &res->seq_res))
4023 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004024 if (nfs4_async_handle_error(task, res->server, NULL,
4025 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004026 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004027 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004028 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004029 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030}
4031
Jeff Laytond3d41522010-09-17 17:31:57 -04004032static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4033{
4034 struct nfs_server *server = NFS_SERVER(dir);
4035 struct nfs_renameargs *arg = msg->rpc_argp;
4036 struct nfs_renameres *res = msg->rpc_resp;
4037
4038 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004039 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004040 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004041}
4042
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004043static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4044{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004045 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004046 &data->args.seq_args,
4047 &data->res.seq_res,
4048 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004049}
4050
4051static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4052 struct inode *new_dir)
4053{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004054 struct nfs_renamedata *data = task->tk_calldata;
4055 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004056
4057 if (!nfs4_sequence_done(task, &res->seq_res))
4058 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004059 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004060 return 0;
4061
Trond Myklebustc733c492017-01-11 12:32:26 -05004062 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004063 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004064 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004065 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004066 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004067 return 1;
4068}
4069
Al Virobeffb8f2016-07-20 16:34:42 -04004070static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004072 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 struct nfs4_link_arg arg = {
4074 .fh = NFS_FH(inode),
4075 .dir_fh = NFS_FH(dir),
4076 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004077 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004079 struct nfs4_link_res res = {
4080 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004081 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004082 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 struct rpc_message msg = {
4084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4085 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004086 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004088 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089
Trond Myklebust136f2622010-04-16 16:22:49 -04004090 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004091 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004092 goto out;
4093
David Quigley14c43f72013-05-22 12:50:43 -04004094 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4095 if (IS_ERR(res.label)) {
4096 status = PTR_ERR(res.label);
4097 goto out;
4098 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004099 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004100
Bryan Schumaker7c513052011-03-24 17:12:24 +00004101 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004102 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004103 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004104 status = nfs_post_op_update_inode(inode, res.fattr);
4105 if (!status)
4106 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004107 }
David Quigley14c43f72013-05-22 12:50:43 -04004108
4109
4110 nfs4_label_free(res.label);
4111
Trond Myklebust136f2622010-04-16 16:22:49 -04004112out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004113 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 return status;
4115}
4116
Al Virobeffb8f2016-07-20 16:34:42 -04004117static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118{
4119 struct nfs4_exception exception = { };
4120 int err;
4121 do {
4122 err = nfs4_handle_exception(NFS_SERVER(inode),
4123 _nfs4_proc_link(inode, dir, name),
4124 &exception);
4125 } while (exception.retry);
4126 return err;
4127}
4128
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004129struct nfs4_createdata {
4130 struct rpc_message msg;
4131 struct nfs4_create_arg arg;
4132 struct nfs4_create_res res;
4133 struct nfs_fh fh;
4134 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004135 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004136};
4137
4138static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004139 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004140{
4141 struct nfs4_createdata *data;
4142
4143 data = kzalloc(sizeof(*data), GFP_KERNEL);
4144 if (data != NULL) {
4145 struct nfs_server *server = NFS_SERVER(dir);
4146
David Quigley14c43f72013-05-22 12:50:43 -04004147 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4148 if (IS_ERR(data->label))
4149 goto out_free;
4150
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004151 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4152 data->msg.rpc_argp = &data->arg;
4153 data->msg.rpc_resp = &data->res;
4154 data->arg.dir_fh = NFS_FH(dir);
4155 data->arg.server = server;
4156 data->arg.name = name;
4157 data->arg.attrs = sattr;
4158 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004159 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004160 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004161 data->res.server = server;
4162 data->res.fh = &data->fh;
4163 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004164 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004165 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004166 }
4167 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004168out_free:
4169 kfree(data);
4170 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004171}
4172
4173static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4174{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004175 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004176 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004177 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004178 update_changeattr(dir, &data->res.dir_cinfo,
4179 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004180 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004181 }
4182 return status;
4183}
4184
4185static void nfs4_free_createdata(struct nfs4_createdata *data)
4186{
David Quigley14c43f72013-05-22 12:50:43 -04004187 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004188 kfree(data);
4189}
4190
Chuck Lever4f390c12006-08-22 20:06:22 -04004191static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004192 struct page *page, unsigned int len, struct iattr *sattr,
4193 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004195 struct nfs4_createdata *data;
4196 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197
Chuck Lever94a6d752006-08-22 20:06:23 -04004198 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004199 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004200
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004201 status = -ENOMEM;
4202 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4203 if (data == NULL)
4204 goto out;
4205
4206 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4207 data->arg.u.symlink.pages = &page;
4208 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004209 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004211 status = nfs4_do_create(dir, dentry, data);
4212
4213 nfs4_free_createdata(data);
4214out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 return status;
4216}
4217
Chuck Lever4f390c12006-08-22 20:06:22 -04004218static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004219 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220{
4221 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004222 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004224
4225 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4226
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004228 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4229 trace_nfs4_symlink(dir, &dentry->d_name, err);
4230 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 &exception);
4232 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004233
4234 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 return err;
4236}
4237
4238static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004239 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004241 struct nfs4_createdata *data;
4242 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004244 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4245 if (data == NULL)
4246 goto out;
4247
David Quigley1775fd32013-05-22 12:50:42 -04004248 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004249 status = nfs4_do_create(dir, dentry, data);
4250
4251 nfs4_free_createdata(data);
4252out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 return status;
4254}
4255
4256static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4257 struct iattr *sattr)
4258{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004259 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004261 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004263
David Quigleyaa9c2662013-05-22 12:50:44 -04004264 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4265
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004266 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4267 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004269 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4270 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4271 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 &exception);
4273 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004274 nfs4_label_release_security(label);
4275
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 return err;
4277}
4278
4279static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004280 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281{
David Howells2b0143b2015-03-17 22:25:59 +00004282 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 struct nfs4_readdir_arg args = {
4284 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004285 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 .pgbase = 0,
4287 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004288 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004289 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 };
4291 struct nfs4_readdir_res res;
4292 struct rpc_message msg = {
4293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4294 .rpc_argp = &args,
4295 .rpc_resp = &res,
4296 .rpc_cred = cred,
4297 };
4298 int status;
4299
Al Viro6de14722013-09-16 10:53:17 -04004300 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4301 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004302 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004303 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004305 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 -05004306 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004307 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004308 status += args.pgbase;
4309 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004310
4311 nfs_invalidate_atime(dir);
4312
Harvey Harrison3110ff82008-05-02 13:42:44 -07004313 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 return status;
4315}
4316
4317static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004318 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319{
4320 struct nfs4_exception exception = { };
4321 int err;
4322 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004323 err = _nfs4_proc_readdir(dentry, cred, cookie,
4324 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004325 trace_nfs4_readdir(d_inode(dentry), err);
4326 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 &exception);
4328 } while (exception.retry);
4329 return err;
4330}
4331
4332static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004333 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004335 struct nfs4_createdata *data;
4336 int mode = sattr->ia_mode;
4337 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004339 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4340 if (data == NULL)
4341 goto out;
4342
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004344 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004346 data->arg.ftype = NF4BLK;
4347 data->arg.u.device.specdata1 = MAJOR(rdev);
4348 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 }
4350 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004351 data->arg.ftype = NF4CHR;
4352 data->arg.u.device.specdata1 = MAJOR(rdev);
4353 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004354 } else if (!S_ISSOCK(mode)) {
4355 status = -EINVAL;
4356 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 }
David Quigley1775fd32013-05-22 12:50:42 -04004358
David Quigleyaa9c2662013-05-22 12:50:44 -04004359 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004360 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004361out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004362 nfs4_free_createdata(data);
4363out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 return status;
4365}
4366
4367static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4368 struct iattr *sattr, dev_t rdev)
4369{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004370 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004372 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004374
David Quigleyaa9c2662013-05-22 12:50:44 -04004375 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4376
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004377 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4378 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004380 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4381 trace_nfs4_mknod(dir, &dentry->d_name, err);
4382 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 &exception);
4384 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004385
4386 nfs4_label_release_security(label);
4387
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 return err;
4389}
4390
4391static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4392 struct nfs_fsstat *fsstat)
4393{
4394 struct nfs4_statfs_arg args = {
4395 .fh = fhandle,
4396 .bitmask = server->attr_bitmask,
4397 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004398 struct nfs4_statfs_res res = {
4399 .fsstat = fsstat,
4400 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 struct rpc_message msg = {
4402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4403 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004404 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 };
4406
Trond Myklebust0e574af2005-10-27 22:12:38 -04004407 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004408 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409}
4410
4411static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4412{
4413 struct nfs4_exception exception = { };
4414 int err;
4415 do {
4416 err = nfs4_handle_exception(server,
4417 _nfs4_proc_statfs(server, fhandle, fsstat),
4418 &exception);
4419 } while (exception.retry);
4420 return err;
4421}
4422
4423static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4424 struct nfs_fsinfo *fsinfo)
4425{
4426 struct nfs4_fsinfo_arg args = {
4427 .fh = fhandle,
4428 .bitmask = server->attr_bitmask,
4429 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004430 struct nfs4_fsinfo_res res = {
4431 .fsinfo = fsinfo,
4432 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 struct rpc_message msg = {
4434 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4435 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004436 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 };
4438
Bryan Schumaker7c513052011-03-24 17:12:24 +00004439 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440}
4441
4442static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4443{
4444 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004445 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 int err;
4447
4448 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004449 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004450 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004451 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004452 nfs4_set_lease_period(server->nfs_client,
4453 fsinfo->lease_time * HZ,
4454 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004455 break;
4456 }
4457 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 } while (exception.retry);
4459 return err;
4460}
4461
4462static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4463{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004464 int error;
4465
Trond Myklebust0e574af2005-10-27 22:12:38 -04004466 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004467 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004468 if (error == 0) {
4469 /* block layout checks this! */
4470 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004471 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004472 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004473
4474 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475}
4476
4477static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4478 struct nfs_pathconf *pathconf)
4479{
4480 struct nfs4_pathconf_arg args = {
4481 .fh = fhandle,
4482 .bitmask = server->attr_bitmask,
4483 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004484 struct nfs4_pathconf_res res = {
4485 .pathconf = pathconf,
4486 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 struct rpc_message msg = {
4488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4489 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004490 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 };
4492
4493 /* None of the pathconf attributes are mandatory to implement */
4494 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4495 memset(pathconf, 0, sizeof(*pathconf));
4496 return 0;
4497 }
4498
Trond Myklebust0e574af2005-10-27 22:12:38 -04004499 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004500 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501}
4502
4503static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4504 struct nfs_pathconf *pathconf)
4505{
4506 struct nfs4_exception exception = { };
4507 int err;
4508
4509 do {
4510 err = nfs4_handle_exception(server,
4511 _nfs4_proc_pathconf(server, fhandle, pathconf),
4512 &exception);
4513 } while (exception.retry);
4514 return err;
4515}
4516
Trond Myklebust5521abf2013-03-16 20:54:34 -04004517int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004518 const struct nfs_open_context *ctx,
4519 const struct nfs_lock_context *l_ctx,
4520 fmode_t fmode)
4521{
NeilBrown17393472016-10-13 15:26:47 +11004522 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004523}
4524EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4525
Trond Myklebust5521abf2013-03-16 20:54:34 -04004526static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4527 const struct nfs_open_context *ctx,
4528 const struct nfs_lock_context *l_ctx,
4529 fmode_t fmode)
4530{
4531 nfs4_stateid current_stateid;
4532
Trond Myklebuste1253be2014-03-05 08:44:23 -05004533 /* If the current stateid represents a lost lock, then exit */
4534 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4535 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004536 return nfs4_stateid_match(stateid, &current_stateid);
4537}
4538
4539static bool nfs4_error_stateid_expired(int err)
4540{
4541 switch (err) {
4542 case -NFS4ERR_DELEG_REVOKED:
4543 case -NFS4ERR_ADMIN_REVOKED:
4544 case -NFS4ERR_BAD_STATEID:
4545 case -NFS4ERR_STALE_STATEID:
4546 case -NFS4ERR_OLD_STATEID:
4547 case -NFS4ERR_OPENMODE:
4548 case -NFS4ERR_EXPIRED:
4549 return true;
4550 }
4551 return false;
4552}
4553
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004554static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004556 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004558 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004559 if (task->tk_status < 0) {
4560 struct nfs4_exception exception = {
4561 .inode = hdr->inode,
4562 .state = hdr->args.context->state,
4563 .stateid = &hdr->args.stateid,
4564 };
4565 task->tk_status = nfs4_async_handle_exception(task,
4566 server, task->tk_status, &exception);
4567 if (exception.retry) {
4568 rpc_restart_call_prepare(task);
4569 return -EAGAIN;
4570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004572
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004574 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576}
4577
Trond Myklebust5521abf2013-03-16 20:54:34 -04004578static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004579 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004580{
4581
4582 if (!nfs4_error_stateid_expired(task->tk_status) ||
4583 nfs4_stateid_is_current(&args->stateid,
4584 args->context,
4585 args->lock_context,
4586 FMODE_READ))
4587 return false;
4588 rpc_restart_call_prepare(task);
4589 return true;
4590}
4591
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004592static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004593{
4594
4595 dprintk("--> %s\n", __func__);
4596
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004597 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004598 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004599 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004600 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004601 if (task->tk_status > 0)
4602 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004603 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4604 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004605}
4606
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004607static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4608 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004610 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004611 if (!hdr->pgio_done_cb)
4612 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004613 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004614 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615}
4616
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004617static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4618 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004619{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004620 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004621 &hdr->args.seq_args,
4622 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004623 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004624 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004625 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4626 hdr->args.lock_context,
4627 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004628 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004629 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004630 return -EIO;
4631 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632}
4633
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004634static int nfs4_write_done_cb(struct rpc_task *task,
4635 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004637 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004638
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004639 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004640 if (task->tk_status < 0) {
4641 struct nfs4_exception exception = {
4642 .inode = hdr->inode,
4643 .state = hdr->args.context->state,
4644 .stateid = &hdr->args.stateid,
4645 };
4646 task->tk_status = nfs4_async_handle_exception(task,
4647 NFS_SERVER(inode), task->tk_status,
4648 &exception);
4649 if (exception.retry) {
4650 rpc_restart_call_prepare(task);
4651 return -EAGAIN;
4652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004654 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004655 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004656 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004657 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004658 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659}
4660
Trond Myklebust5521abf2013-03-16 20:54:34 -04004661static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004662 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004663{
4664
4665 if (!nfs4_error_stateid_expired(task->tk_status) ||
4666 nfs4_stateid_is_current(&args->stateid,
4667 args->context,
4668 args->lock_context,
4669 FMODE_WRITE))
4670 return false;
4671 rpc_restart_call_prepare(task);
4672 return true;
4673}
4674
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004675static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004676{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004677 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004678 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004679 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004680 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004681 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4682 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004683}
4684
Trond Myklebust5a37f852012-04-28 14:55:16 -04004685static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004686bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004687{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004688 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004689 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004690 return false;
4691 /* Otherwise, request attributes if and only if we don't hold
4692 * a delegation
4693 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004694 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004695}
Fred Isamana69aef12011-03-03 15:13:47 +00004696
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004697static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4698 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004700 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004701
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004702 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4703 hdr->args.bitmask = NULL;
4704 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004705 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004706 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004707
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004708 if (!hdr->pgio_done_cb)
4709 hdr->pgio_done_cb = nfs4_write_done_cb;
4710 hdr->res.server = server;
4711 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004713 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004714 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715}
4716
Fred Isaman0b7c0152012-04-20 14:47:39 -04004717static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4718{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004719 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004720 &data->args.seq_args,
4721 &data->res.seq_res,
4722 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723}
4724
Fred Isaman0b7c0152012-04-20 14:47:39 -04004725static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004728
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004729 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004730 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4731 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004732 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004733 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004735 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736}
4737
Fred Isaman0b7c0152012-04-20 14:47:39 -04004738static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004739{
4740 if (!nfs4_sequence_done(task, &data->res.seq_res))
4741 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004742 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004743}
4744
Fred Isaman0b7c0152012-04-20 14:47:39 -04004745static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004747 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004748
Fred Isaman0b7c0152012-04-20 14:47:39 -04004749 if (data->commit_done_cb == NULL)
4750 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004751 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004752 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004753 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754}
4755
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004756struct nfs4_renewdata {
4757 struct nfs_client *client;
4758 unsigned long timestamp;
4759};
4760
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761/*
4762 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4763 * standalone procedure for queueing an asynchronous RENEW.
4764 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004765static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004766{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004767 struct nfs4_renewdata *data = calldata;
4768 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004769
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004770 if (atomic_read(&clp->cl_count) > 1)
4771 nfs4_schedule_state_renewal(clp);
4772 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004773 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004774}
4775
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004776static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004778 struct nfs4_renewdata *data = calldata;
4779 struct nfs_client *clp = data->client;
4780 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004782 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004783 switch (task->tk_status) {
4784 case 0:
4785 break;
4786 case -NFS4ERR_LEASE_MOVED:
4787 nfs4_schedule_lease_moved_recovery(clp);
4788 break;
4789 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004790 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004791 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4792 return;
4793 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004794 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004795 return;
4796 }
4797 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004799 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800}
4801
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004802static const struct rpc_call_ops nfs4_renew_ops = {
4803 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004804 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004805};
4806
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004807static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808{
4809 struct rpc_message msg = {
4810 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4811 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004812 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004814 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004816 if (renew_flags == 0)
4817 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004818 if (!atomic_inc_not_zero(&clp->cl_count))
4819 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004820 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004821 if (data == NULL)
4822 return -ENOMEM;
4823 data->client = clp;
4824 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004825 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004826 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827}
4828
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004829static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830{
4831 struct rpc_message msg = {
4832 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4833 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004834 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 };
4836 unsigned long now = jiffies;
4837 int status;
4838
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004839 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 if (status < 0)
4841 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004842 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 return 0;
4844}
4845
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004846static inline int nfs4_server_supports_acls(struct nfs_server *server)
4847{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004848 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004849}
4850
Trond Myklebust21f498c2012-08-24 10:59:25 -04004851/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4852 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004853 * the stack.
4854 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004855#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004856
Neil Hormane9e3d722011-03-04 19:26:03 -05004857static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004858 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004859{
4860 struct page *newpage, **spages;
4861 int rc = 0;
4862 size_t len;
4863 spages = pages;
4864
4865 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004866 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004867 newpage = alloc_page(GFP_KERNEL);
4868
4869 if (newpage == NULL)
4870 goto unwind;
4871 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05004872 buf += len;
4873 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05004874 *pages++ = newpage;
4875 rc++;
4876 } while (buflen != 0);
4877
4878 return rc;
4879
4880unwind:
4881 for(; rc > 0; rc--)
4882 __free_page(spages[rc-1]);
4883 return -ENOMEM;
4884}
4885
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004886struct nfs4_cached_acl {
4887 int cached;
4888 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004889 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004890};
4891
4892static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004893{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004894 struct nfs_inode *nfsi = NFS_I(inode);
4895
4896 spin_lock(&inode->i_lock);
4897 kfree(nfsi->nfs4_acl);
4898 nfsi->nfs4_acl = acl;
4899 spin_unlock(&inode->i_lock);
4900}
4901
4902static void nfs4_zap_acl_attr(struct inode *inode)
4903{
4904 nfs4_set_cached_acl(inode, NULL);
4905}
4906
4907static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4908{
4909 struct nfs_inode *nfsi = NFS_I(inode);
4910 struct nfs4_cached_acl *acl;
4911 int ret = -ENOENT;
4912
4913 spin_lock(&inode->i_lock);
4914 acl = nfsi->nfs4_acl;
4915 if (acl == NULL)
4916 goto out;
4917 if (buf == NULL) /* user is just asking for length */
4918 goto out_len;
4919 if (acl->cached == 0)
4920 goto out;
4921 ret = -ERANGE; /* see getxattr(2) man page */
4922 if (acl->len > buflen)
4923 goto out;
4924 memcpy(buf, acl->data, acl->len);
4925out_len:
4926 ret = acl->len;
4927out:
4928 spin_unlock(&inode->i_lock);
4929 return ret;
4930}
4931
Sachin Prabhu5794d212012-04-17 14:36:40 +01004932static 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 +00004933{
4934 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004935 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004936
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004937 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004938 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004939 if (acl == NULL)
4940 goto out;
4941 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004942 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004943 } else {
4944 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4945 if (acl == NULL)
4946 goto out;
4947 acl->cached = 0;
4948 }
4949 acl->len = acl_len;
4950out:
4951 nfs4_set_cached_acl(inode, acl);
4952}
4953
Andy Adamsonbf118a32011-12-07 11:55:27 -05004954/*
4955 * The getxattr API returns the required buffer length when called with a
4956 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4957 * the required buf. On a NULL buf, we send a page of data to the server
4958 * guessing that the ACL request can be serviced by a page. If so, we cache
4959 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4960 * the cache. If not so, we throw away the page, and cache the required
4961 * length. The next getxattr call will then produce another round trip to
4962 * the server, this time with the input buf of the required size.
4963 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004964static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004965{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004966 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004967 struct nfs_getaclargs args = {
4968 .fh = NFS_FH(inode),
4969 .acl_pages = pages,
4970 .acl_len = buflen,
4971 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004972 struct nfs_getaclres res = {
4973 .acl_len = buflen,
4974 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004975 struct rpc_message msg = {
4976 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4977 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004978 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004979 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004980 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4981 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004982
Andy Adamsonbf118a32011-12-07 11:55:27 -05004983 /* As long as we're doing a round trip to the server anyway,
4984 * let's be prepared for a page of acl data. */
4985 if (npages == 0)
4986 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004987 if (npages > ARRAY_SIZE(pages))
4988 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004989
Andy Adamsonbf118a32011-12-07 11:55:27 -05004990 for (i = 0; i < npages; i++) {
4991 pages[i] = alloc_page(GFP_KERNEL);
4992 if (!pages[i])
4993 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004994 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004995
4996 /* for decoding across pages */
4997 res.acl_scratch = alloc_page(GFP_KERNEL);
4998 if (!res.acl_scratch)
4999 goto out_free;
5000
Andy Adamsonbf118a32011-12-07 11:55:27 -05005001 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005002
Peng Taode040be2012-01-10 22:42:47 +08005003 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005004 __func__, buf, buflen, npages, args.acl_len);
5005 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5006 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005007 if (ret)
5008 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005009
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005010 /* Handle the case where the passed-in buffer is too short */
5011 if (res.acl_flags & NFS4_ACL_TRUNC) {
5012 /* Did the user only issue a request for the acl length? */
5013 if (buf == NULL)
5014 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005015 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005016 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005017 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005018 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005019 if (buf) {
5020 if (res.acl_len > buflen) {
5021 ret = -ERANGE;
5022 goto out_free;
5023 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005024 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005025 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005026out_ok:
5027 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005028out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005029 for (i = 0; i < npages; i++)
5030 if (pages[i])
5031 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005032 if (res.acl_scratch)
5033 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005034 return ret;
5035}
5036
Trond Myklebust16b42892006-08-24 12:27:15 -04005037static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5038{
5039 struct nfs4_exception exception = { };
5040 ssize_t ret;
5041 do {
5042 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005043 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005044 if (ret >= 0)
5045 break;
5046 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5047 } while (exception.retry);
5048 return ret;
5049}
5050
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005051static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5052{
5053 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005054 int ret;
5055
5056 if (!nfs4_server_supports_acls(server))
5057 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005058 ret = nfs_revalidate_inode(server, inode);
5059 if (ret < 0)
5060 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005061 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5062 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005063 ret = nfs4_read_cached_acl(inode, buf, buflen);
5064 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005065 /* -ENOENT is returned if there is no ACL or if there is an ACL
5066 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005067 return ret;
5068 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005069}
5070
Trond Myklebust16b42892006-08-24 12:27:15 -04005071static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005072{
5073 struct nfs_server *server = NFS_SERVER(inode);
5074 struct page *pages[NFS4ACL_MAXPAGES];
5075 struct nfs_setaclargs arg = {
5076 .fh = NFS_FH(inode),
5077 .acl_pages = pages,
5078 .acl_len = buflen,
5079 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005080 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005081 struct rpc_message msg = {
5082 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5083 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005084 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005085 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005086 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005087 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005088
5089 if (!nfs4_server_supports_acls(server))
5090 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005091 if (npages > ARRAY_SIZE(pages))
5092 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005093 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005094 if (i < 0)
5095 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005096 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005097 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005098
5099 /*
5100 * Free each page after tx, so the only ref left is
5101 * held by the network stack
5102 */
5103 for (; i > 0; i--)
5104 put_page(pages[i-1]);
5105
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005106 /*
5107 * Acl update can result in inode attribute update.
5108 * so mark the attribute cache invalid.
5109 */
5110 spin_lock(&inode->i_lock);
5111 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5112 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005113 nfs_access_zap_cache(inode);
5114 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005115 return ret;
5116}
5117
Trond Myklebust16b42892006-08-24 12:27:15 -04005118static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5119{
5120 struct nfs4_exception exception = { };
5121 int err;
5122 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005123 err = __nfs4_proc_set_acl(inode, buf, buflen);
5124 trace_nfs4_set_acl(inode, err);
5125 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005126 &exception);
5127 } while (exception.retry);
5128 return err;
5129}
5130
David Quigleyaa9c2662013-05-22 12:50:44 -04005131#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5132static int _nfs4_get_security_label(struct inode *inode, void *buf,
5133 size_t buflen)
5134{
5135 struct nfs_server *server = NFS_SERVER(inode);
5136 struct nfs_fattr fattr;
5137 struct nfs4_label label = {0, 0, buflen, buf};
5138
5139 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005140 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005141 .fh = NFS_FH(inode),
5142 .bitmask = bitmask,
5143 };
5144 struct nfs4_getattr_res res = {
5145 .fattr = &fattr,
5146 .label = &label,
5147 .server = server,
5148 };
5149 struct rpc_message msg = {
5150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005151 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005152 .rpc_resp = &res,
5153 };
5154 int ret;
5155
5156 nfs_fattr_init(&fattr);
5157
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005158 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005159 if (ret)
5160 return ret;
5161 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5162 return -ENOENT;
5163 if (buflen < label.len)
5164 return -ERANGE;
5165 return 0;
5166}
5167
5168static int nfs4_get_security_label(struct inode *inode, void *buf,
5169 size_t buflen)
5170{
5171 struct nfs4_exception exception = { };
5172 int err;
5173
5174 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5175 return -EOPNOTSUPP;
5176
5177 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005178 err = _nfs4_get_security_label(inode, buf, buflen);
5179 trace_nfs4_get_security_label(inode, err);
5180 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005181 &exception);
5182 } while (exception.retry);
5183 return err;
5184}
5185
5186static int _nfs4_do_set_security_label(struct inode *inode,
5187 struct nfs4_label *ilabel,
5188 struct nfs_fattr *fattr,
5189 struct nfs4_label *olabel)
5190{
5191
5192 struct iattr sattr = {0};
5193 struct nfs_server *server = NFS_SERVER(inode);
5194 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005195 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005196 .fh = NFS_FH(inode),
5197 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005198 .server = server,
5199 .bitmask = bitmask,
5200 .label = ilabel,
5201 };
5202 struct nfs_setattrres res = {
5203 .fattr = fattr,
5204 .label = olabel,
5205 .server = server,
5206 };
5207 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005208 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5209 .rpc_argp = &arg,
5210 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005211 };
5212 int status;
5213
Jeff Layton12207f62013-11-01 10:49:32 -04005214 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005215
Jeff Layton12207f62013-11-01 10:49:32 -04005216 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005217 if (status)
5218 dprintk("%s failed: %d\n", __func__, status);
5219
5220 return status;
5221}
5222
5223static int nfs4_do_set_security_label(struct inode *inode,
5224 struct nfs4_label *ilabel,
5225 struct nfs_fattr *fattr,
5226 struct nfs4_label *olabel)
5227{
5228 struct nfs4_exception exception = { };
5229 int err;
5230
5231 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005232 err = _nfs4_do_set_security_label(inode, ilabel,
5233 fattr, olabel);
5234 trace_nfs4_set_security_label(inode, err);
5235 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005236 &exception);
5237 } while (exception.retry);
5238 return err;
5239}
5240
5241static int
Al Viro59301222016-05-27 10:19:30 -04005242nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005243{
5244 struct nfs4_label ilabel, *olabel = NULL;
5245 struct nfs_fattr fattr;
5246 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005247 int status;
5248
5249 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5250 return -EOPNOTSUPP;
5251
5252 nfs_fattr_init(&fattr);
5253
5254 ilabel.pi = 0;
5255 ilabel.lfs = 0;
5256 ilabel.label = (char *)buf;
5257 ilabel.len = buflen;
5258
5259 cred = rpc_lookup_cred();
5260 if (IS_ERR(cred))
5261 return PTR_ERR(cred);
5262
5263 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5264 if (IS_ERR(olabel)) {
5265 status = -PTR_ERR(olabel);
5266 goto out;
5267 }
5268
5269 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5270 if (status == 0)
5271 nfs_setsecurity(inode, &fattr, olabel);
5272
5273 nfs4_label_free(olabel);
5274out:
5275 put_rpccred(cred);
5276 return status;
5277}
5278#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5279
5280
Chuck Leverf0920752012-05-21 22:45:41 -04005281static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5282 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005283{
5284 __be32 verf[2];
5285
Chuck Lever2c820d92012-05-21 22:45:33 -04005286 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5287 /* An impossible timestamp guarantees this value
5288 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005289 verf[0] = cpu_to_be32(U32_MAX);
5290 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005291 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005292 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005293 u64 ns = ktime_to_ns(nn->boot_time);
5294
5295 verf[0] = cpu_to_be32(ns >> 32);
5296 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005297 }
Chuck Levercd937102012-03-02 17:14:31 -05005298 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5299}
5300
Jeff Laytona3192682015-06-09 19:43:59 -04005301static int
5302nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005303{
Jeff Laytona3192682015-06-09 19:43:59 -04005304 size_t len;
5305 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005306
Trond Myklebustceb3a162015-01-03 15:16:04 -05005307 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005308 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005309
Jeff Laytona3192682015-06-09 19:43:59 -04005310 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005311 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005312 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5313 1 +
5314 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5315 1;
5316 rcu_read_unlock();
5317
5318 if (len > NFS4_OPAQUE_LIMIT + 1)
5319 return -EINVAL;
5320
5321 /*
5322 * Since this string is allocated at mount time, and held until the
5323 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5324 * about a memory-reclaim deadlock.
5325 */
5326 str = kmalloc(len, GFP_KERNEL);
5327 if (!str)
5328 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005329
Chuck Levere984a552012-09-14 17:24:21 -04005330 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005331 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005332 clp->cl_ipaddr,
5333 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5334 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005335 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005336
Jeff Laytona3192682015-06-09 19:43:59 -04005337 clp->cl_owner_id = str;
5338 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005339}
5340
Jeff Layton873e3852015-06-09 19:44:00 -04005341static int
5342nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005343{
Jeff Layton873e3852015-06-09 19:44:00 -04005344 size_t len;
5345 char *str;
5346
5347 len = 10 + 10 + 1 + 10 + 1 +
5348 strlen(nfs4_client_id_uniquifier) + 1 +
5349 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5350
5351 if (len > NFS4_OPAQUE_LIMIT + 1)
5352 return -EINVAL;
5353
5354 /*
5355 * Since this string is allocated at mount time, and held until the
5356 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5357 * about a memory-reclaim deadlock.
5358 */
5359 str = kmalloc(len, GFP_KERNEL);
5360 if (!str)
5361 return -ENOMEM;
5362
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005363 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005364 clp->rpc_ops->version, clp->cl_minorversion,
5365 nfs4_client_id_uniquifier,
5366 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005367 clp->cl_owner_id = str;
5368 return 0;
5369}
5370
5371static int
5372nfs4_init_uniform_client_string(struct nfs_client *clp)
5373{
Jeff Layton873e3852015-06-09 19:44:00 -04005374 size_t len;
5375 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005376
5377 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005378 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005379
5380 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005381 return nfs4_init_uniquifier_client_string(clp);
5382
5383 len = 10 + 10 + 1 + 10 + 1 +
5384 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5385
5386 if (len > NFS4_OPAQUE_LIMIT + 1)
5387 return -EINVAL;
5388
5389 /*
5390 * Since this string is allocated at mount time, and held until the
5391 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5392 * about a memory-reclaim deadlock.
5393 */
5394 str = kmalloc(len, GFP_KERNEL);
5395 if (!str)
5396 return -ENOMEM;
5397
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005398 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005399 clp->rpc_ops->version, clp->cl_minorversion,
5400 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005401 clp->cl_owner_id = str;
5402 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005403}
5404
Chuck Lever706cb8d2014-03-12 12:51:47 -04005405/*
5406 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5407 * services. Advertise one based on the address family of the
5408 * clientaddr.
5409 */
5410static unsigned int
5411nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5412{
5413 if (strchr(clp->cl_ipaddr, ':') != NULL)
5414 return scnprintf(buf, len, "tcp6");
5415 else
5416 return scnprintf(buf, len, "tcp");
5417}
5418
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005419static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5420{
5421 struct nfs4_setclientid *sc = calldata;
5422
5423 if (task->tk_status == 0)
5424 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5425}
5426
5427static const struct rpc_call_ops nfs4_setclientid_ops = {
5428 .rpc_call_done = nfs4_setclientid_done,
5429};
5430
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005431/**
5432 * nfs4_proc_setclientid - Negotiate client ID
5433 * @clp: state data structure
5434 * @program: RPC program for NFSv4 callback service
5435 * @port: IP port number for NFS4 callback service
5436 * @cred: RPC credential to use for this call
5437 * @res: where to place the result
5438 *
5439 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5440 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005441int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5442 unsigned short port, struct rpc_cred *cred,
5443 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444{
5445 nfs4_verifier sc_verifier;
5446 struct nfs4_setclientid setclientid = {
5447 .sc_verifier = &sc_verifier,
5448 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005449 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 };
5451 struct rpc_message msg = {
5452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5453 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005454 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005455 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005457 struct rpc_task *task;
5458 struct rpc_task_setup task_setup_data = {
5459 .rpc_client = clp->cl_rpcclient,
5460 .rpc_message = &msg,
5461 .callback_ops = &nfs4_setclientid_ops,
5462 .callback_data = &setclientid,
5463 .flags = RPC_TASK_TIMEOUT,
5464 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005465 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466
Chuck Leverde734832012-07-11 16:30:50 -04005467 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005468 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005469
5470 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5471 status = nfs4_init_uniform_client_string(clp);
5472 else
Jeff Laytona3192682015-06-09 19:43:59 -04005473 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005474
5475 if (status)
5476 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005477
Chuck Leverde734832012-07-11 16:30:50 -04005478 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005479 setclientid.sc_netid_len =
5480 nfs4_init_callback_netid(clp,
5481 setclientid.sc_netid,
5482 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005483 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005484 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 clp->cl_ipaddr, port >> 8, port & 255);
5486
Jeff Layton3a6bb732015-06-09 19:43:57 -04005487 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005488 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005489 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005490 task = rpc_run_task(&task_setup_data);
5491 if (IS_ERR(task)) {
5492 status = PTR_ERR(task);
5493 goto out;
5494 }
5495 status = task->tk_status;
5496 if (setclientid.sc_cred) {
5497 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5498 put_rpccred(setclientid.sc_cred);
5499 }
5500 rpc_put_task(task);
5501out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005502 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005503 dprintk("NFS reply setclientid: %d\n", status);
5504 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505}
5506
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005507/**
5508 * nfs4_proc_setclientid_confirm - Confirm client ID
5509 * @clp: state data structure
5510 * @res: result of a previous SETCLIENTID
5511 * @cred: RPC credential to use for this call
5512 *
5513 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5514 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005515int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005516 struct nfs4_setclientid_res *arg,
5517 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 struct rpc_message msg = {
5520 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005521 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005522 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 int status;
5525
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005526 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5527 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5528 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005529 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005530 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005531 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 return status;
5533}
5534
Trond Myklebustfe650402006-01-03 09:55:18 +01005535struct nfs4_delegreturndata {
5536 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005537 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005538 struct nfs_fh fh;
5539 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005540 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005541 struct {
5542 struct nfs4_layoutreturn_args arg;
5543 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005544 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005545 u32 roc_barrier;
5546 bool roc;
5547 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005548 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005549 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005550 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005551};
5552
Trond Myklebustfe650402006-01-03 09:55:18 +01005553static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5554{
5555 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005556
Trond Myklebust14516c32010-07-31 14:29:06 -04005557 if (!nfs4_sequence_done(task, &data->res.seq_res))
5558 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005559
Trond Myklebustca8acf82013-08-13 10:36:56 -04005560 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005561
5562 /* Handle Layoutreturn errors */
5563 if (data->args.lr_args && task->tk_status != 0) {
5564 switch(data->res.lr_ret) {
5565 default:
5566 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5567 break;
5568 case 0:
5569 data->args.lr_args = NULL;
5570 data->res.lr_res = NULL;
5571 break;
5572 case -NFS4ERR_ADMIN_REVOKED:
5573 case -NFS4ERR_DELEG_REVOKED:
5574 case -NFS4ERR_EXPIRED:
5575 case -NFS4ERR_BAD_STATEID:
5576 case -NFS4ERR_OLD_STATEID:
5577 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5578 case -NFS4ERR_WRONG_CRED:
5579 data->args.lr_args = NULL;
5580 data->res.lr_res = NULL;
5581 data->res.lr_ret = 0;
5582 rpc_restart_call_prepare(task);
5583 return;
5584 }
5585 }
5586
Ricardo Labiaga79708862009-12-07 09:23:21 -05005587 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005588 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005589 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005590 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005591 case -NFS4ERR_ADMIN_REVOKED:
5592 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005593 case -NFS4ERR_EXPIRED:
5594 nfs4_free_revoked_stateid(data->res.server,
5595 data->args.stateid,
5596 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005597 case -NFS4ERR_BAD_STATEID:
5598 case -NFS4ERR_OLD_STATEID:
5599 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005600 task->tk_status = 0;
5601 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005602 case -NFS4ERR_ACCESS:
5603 if (data->args.bitmask) {
5604 data->args.bitmask = NULL;
5605 data->res.fattr = NULL;
5606 task->tk_status = 0;
5607 rpc_restart_call_prepare(task);
5608 return;
5609 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005610 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005611 if (nfs4_async_handle_error(task, data->res.server,
5612 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005613 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005614 return;
5615 }
5616 }
5617 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005618}
5619
5620static void nfs4_delegreturn_release(void *calldata)
5621{
Peng Tao039b7562014-07-03 13:05:02 +08005622 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005623 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005624
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005625 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005626 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005627 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5628 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005629 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005630 nfs_iput_and_deactive(inode);
5631 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005632 kfree(calldata);
5633}
5634
Andy Adamson938e1012009-04-01 09:22:28 -04005635static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5636{
5637 struct nfs4_delegreturndata *d_data;
5638
5639 d_data = (struct nfs4_delegreturndata *)data;
5640
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005641 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005642 return;
5643
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005644 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005645 &d_data->args.seq_args,
5646 &d_data->res.seq_res,
5647 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005648}
Andy Adamson938e1012009-04-01 09:22:28 -04005649
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005650static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005651 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005652 .rpc_call_done = nfs4_delegreturn_done,
5653 .rpc_release = nfs4_delegreturn_release,
5654};
5655
Trond Myklebuste6f81072008-01-24 18:14:34 -05005656static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005657{
5658 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005659 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005660 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005661 struct rpc_message msg = {
5662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5663 .rpc_cred = cred,
5664 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005665 struct rpc_task_setup task_setup_data = {
5666 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005667 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005668 .callback_ops = &nfs4_delegreturn_ops,
5669 .flags = RPC_TASK_ASYNC,
5670 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005671 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005672
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005673 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005674 if (data == NULL)
5675 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005676 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005677
5678 nfs4_state_protect(server->nfs_client,
5679 NFS_SP4_MACH_CRED_CLEANUP,
5680 &task_setup_data.rpc_client, &msg);
5681
Trond Myklebustfe650402006-01-03 09:55:18 +01005682 data->args.fhandle = &data->fh;
5683 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005684 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005685 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005686 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005687 data->res.fattr = &data->fattr;
5688 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005689 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005690 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005691 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005692 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005693 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005694 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005695 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005696 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005697 if (data->lr.roc) {
5698 data->args.lr_args = &data->lr.arg;
5699 data->res.lr_res = &data->lr.res;
5700 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005701 } else if (data->lr.roc) {
5702 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5703 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005704 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005705
Trond Myklebustc970aa82007-07-14 15:39:59 -04005706 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005707 msg.rpc_argp = &data->args;
5708 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005709 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005710 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005711 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005712 if (!issync)
5713 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005714 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005715 if (status != 0)
5716 goto out;
5717 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005718out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005719 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005720 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721}
5722
Trond Myklebuste6f81072008-01-24 18:14:34 -05005723int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724{
5725 struct nfs_server *server = NFS_SERVER(inode);
5726 struct nfs4_exception exception = { };
5727 int err;
5728 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005729 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005730 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 switch (err) {
5732 case -NFS4ERR_STALE_STATEID:
5733 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 case 0:
5735 return 0;
5736 }
5737 err = nfs4_handle_exception(server, err, &exception);
5738 } while (exception.retry);
5739 return err;
5740}
5741
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5743{
5744 struct inode *inode = state->inode;
5745 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005746 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005747 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005749 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005751 struct nfs_lockt_res res = {
5752 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 };
5754 struct rpc_message msg = {
5755 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005756 .rpc_argp = &arg,
5757 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 .rpc_cred = state->owner->so_cred,
5759 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 struct nfs4_lock_state *lsp;
5761 int status;
5762
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005763 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005764 status = nfs4_set_lock_state(state, request);
5765 if (status != 0)
5766 goto out;
5767 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005768 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005769 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005770 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005771 switch (status) {
5772 case 0:
5773 request->fl_type = F_UNLCK;
5774 break;
5775 case -NFS4ERR_DENIED:
5776 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005778 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005779 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005780out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 return status;
5782}
5783
5784static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5785{
5786 struct nfs4_exception exception = { };
5787 int err;
5788
5789 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005790 err = _nfs4_proc_getlk(state, cmd, request);
5791 trace_nfs4_get_lock(request, state, cmd, err);
5792 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 &exception);
5794 } while (exception.retry);
5795 return err;
5796}
5797
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005798struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005799 struct nfs_locku_args arg;
5800 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005801 struct nfs4_lock_state *lsp;
5802 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005803 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005804 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005805 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005806};
5807
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005808static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5809 struct nfs_open_context *ctx,
5810 struct nfs4_lock_state *lsp,
5811 struct nfs_seqid *seqid)
5812{
5813 struct nfs4_unlockdata *p;
5814 struct inode *inode = lsp->ls_state->inode;
5815
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005816 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005817 if (p == NULL)
5818 return NULL;
5819 p->arg.fh = NFS_FH(inode);
5820 p->arg.fl = &p->fl;
5821 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005822 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005823 p->lsp = lsp;
5824 atomic_inc(&lsp->ls_count);
5825 /* Ensure we don't close file until we're done freeing locks! */
5826 p->ctx = get_nfs_open_context(ctx);
5827 memcpy(&p->fl, fl, sizeof(p->fl));
5828 p->server = NFS_SERVER(inode);
5829 return p;
5830}
5831
Trond Myklebust06f814a2006-01-03 09:55:07 +01005832static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005833{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005834 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005835 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005836 nfs4_put_lock_state(calldata->lsp);
5837 put_nfs_open_context(calldata->ctx);
5838 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005839}
5840
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005841static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005842{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005843 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005844
Trond Myklebust14516c32010-07-31 14:29:06 -04005845 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5846 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005847 switch (task->tk_status) {
5848 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005849 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005850 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005851 if (nfs4_update_lock_stateid(calldata->lsp,
5852 &calldata->res.stateid))
5853 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005854 case -NFS4ERR_ADMIN_REVOKED:
5855 case -NFS4ERR_EXPIRED:
5856 nfs4_free_revoked_stateid(calldata->server,
5857 &calldata->arg.stateid,
5858 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005859 case -NFS4ERR_BAD_STATEID:
5860 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005861 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005862 if (!nfs4_stateid_match(&calldata->arg.stateid,
5863 &calldata->lsp->ls_stateid))
5864 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005865 break;
5866 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005867 if (nfs4_async_handle_error(task, calldata->server,
5868 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005869 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005870 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005871 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005872}
5873
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005874static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005875{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005876 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005878 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005879 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005880 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005881 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005882 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005883 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005884 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005885 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005886 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04005887 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005888 &calldata->res.seq_res,
5889 task) != 0)
5890 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005891 return;
5892out_no_action:
5893 task->tk_action = NULL;
5894out_wait:
5895 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896}
5897
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005898static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005899 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005900 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005901 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005902};
5903
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005904static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5905 struct nfs_open_context *ctx,
5906 struct nfs4_lock_state *lsp,
5907 struct nfs_seqid *seqid)
5908{
5909 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005910 struct rpc_message msg = {
5911 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5912 .rpc_cred = ctx->cred,
5913 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005914 struct rpc_task_setup task_setup_data = {
5915 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005916 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005917 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005918 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005919 .flags = RPC_TASK_ASYNC,
5920 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005921
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005922 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5923 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5924
Frank Filz137d6ac2007-07-09 15:32:29 -07005925 /* Ensure this is an unlock - when canceling a lock, the
5926 * canceled lock is passed in, and it won't be an unlock.
5927 */
5928 fl->fl_type = F_UNLCK;
5929
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005930 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5931 if (data == NULL) {
5932 nfs_free_seqid(seqid);
5933 return ERR_PTR(-ENOMEM);
5934 }
5935
Chuck Levera9c92d62013-08-09 12:48:18 -04005936 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005937 msg.rpc_argp = &data->arg;
5938 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005939 task_setup_data.callback_data = data;
5940 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005941}
5942
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5944{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005945 struct inode *inode = state->inode;
5946 struct nfs4_state_owner *sp = state->owner;
5947 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005948 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005949 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005950 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005951 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005952 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005953 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954
Trond Myklebust9b073572006-06-29 16:38:34 -04005955 status = nfs4_set_lock_state(state, request);
5956 /* Unlock _before_ we do the RPC call */
5957 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005958 /* Exclude nfs_delegation_claim_locks() */
5959 mutex_lock(&sp->so_delegreturn_mutex);
5960 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005961 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005962 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005963 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005964 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005965 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005966 }
5967 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005968 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005969 if (status != 0)
5970 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005971 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005972 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005973 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5974 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005975 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5976 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005977 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005978 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005979 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005980 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005981 status = PTR_ERR(task);
5982 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005983 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005984 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005985 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005986out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005987 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005988 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005989 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990}
5991
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005992struct nfs4_lockdata {
5993 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005994 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005995 struct nfs4_lock_state *lsp;
5996 struct nfs_open_context *ctx;
5997 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005998 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005999 int rpc_status;
6000 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006001 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006002};
6003
6004static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006005 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6006 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006007{
6008 struct nfs4_lockdata *p;
6009 struct inode *inode = lsp->ls_state->inode;
6010 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006011 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006012
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006013 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006014 if (p == NULL)
6015 return NULL;
6016
6017 p->arg.fh = NFS_FH(inode);
6018 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006019 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006020 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006021 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006022 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6023 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006024 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006025 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006026 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006027 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006028 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006029 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006030 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006031 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006032 atomic_inc(&lsp->ls_count);
6033 p->ctx = get_nfs_open_context(ctx);
6034 memcpy(&p->fl, fl, sizeof(p->fl));
6035 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006036out_free_seqid:
6037 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006038out_free:
6039 kfree(p);
6040 return NULL;
6041}
6042
6043static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6044{
6045 struct nfs4_lockdata *data = calldata;
6046 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047
Harvey Harrison3110ff82008-05-02 13:42:44 -07006048 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006049 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006050 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006051 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006052 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006053 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006054 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006055 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006056 nfs4_stateid_copy(&data->arg.open_stateid,
6057 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006058 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006059 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006060 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006061 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006062 nfs4_stateid_copy(&data->arg.lock_stateid,
6063 &data->lsp->ls_stateid);
6064 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006065 if (!nfs4_valid_open_stateid(state)) {
6066 data->rpc_status = -EBADF;
6067 task->tk_action = NULL;
6068 goto out_release_open_seqid;
6069 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006070 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006071 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006072 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006073 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006074 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006075 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006076out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006077 nfs_release_seqid(data->arg.open_seqid);
6078out_release_lock_seqid:
6079 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006080out_wait:
6081 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006082 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006083}
6084
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006085static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6086{
6087 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006088 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006089
Harvey Harrison3110ff82008-05-02 13:42:44 -07006090 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006091
Trond Myklebust14516c32010-07-31 14:29:06 -04006092 if (!nfs4_sequence_done(task, &data->res.seq_res))
6093 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006094
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006095 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006096 switch (task->tk_status) {
6097 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006098 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006099 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006100 if (data->arg.new_lock) {
6101 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006102 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006103 rpc_restart_call_prepare(task);
6104 break;
6105 }
6106 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006107 if (data->arg.new_lock_owner != 0) {
6108 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6109 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6110 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6111 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6112 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006113 break;
6114 case -NFS4ERR_BAD_STATEID:
6115 case -NFS4ERR_OLD_STATEID:
6116 case -NFS4ERR_STALE_STATEID:
6117 case -NFS4ERR_EXPIRED:
6118 if (data->arg.new_lock_owner != 0) {
6119 if (!nfs4_stateid_match(&data->arg.open_stateid,
6120 &lsp->ls_state->open_stateid))
6121 rpc_restart_call_prepare(task);
6122 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6123 &lsp->ls_stateid))
6124 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006125 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006126 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006127}
6128
6129static void nfs4_lock_release(void *calldata)
6130{
6131 struct nfs4_lockdata *data = calldata;
6132
Harvey Harrison3110ff82008-05-02 13:42:44 -07006133 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006134 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006135 if (data->cancelled != 0) {
6136 struct rpc_task *task;
6137 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6138 data->arg.lock_seqid);
6139 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006140 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006141 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006142 } else
6143 nfs_free_seqid(data->arg.lock_seqid);
6144 nfs4_put_lock_state(data->lsp);
6145 put_nfs_open_context(data->ctx);
6146 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006147 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006148}
6149
6150static const struct rpc_call_ops nfs4_lock_ops = {
6151 .rpc_call_prepare = nfs4_lock_prepare,
6152 .rpc_call_done = nfs4_lock_done,
6153 .rpc_release = nfs4_lock_release,
6154};
6155
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006156static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6157{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006158 switch (error) {
6159 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006160 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006161 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006162 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006163 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006164 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006165 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006166 break;
6167 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006168 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006169 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006170 };
6171}
6172
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006173static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006174{
6175 struct nfs4_lockdata *data;
6176 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006177 struct rpc_message msg = {
6178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6179 .rpc_cred = state->owner->so_cred,
6180 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006181 struct rpc_task_setup task_setup_data = {
6182 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006183 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006184 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006185 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006186 .flags = RPC_TASK_ASYNC,
6187 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006188 int ret;
6189
Harvey Harrison3110ff82008-05-02 13:42:44 -07006190 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006191 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006192 fl->fl_u.nfs4_fl.owner,
6193 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006194 if (data == NULL)
6195 return -ENOMEM;
6196 if (IS_SETLKW(cmd))
6197 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006198 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006199 msg.rpc_argp = &data->arg;
6200 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006201 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006202 if (recovery_type > NFS_LOCK_NEW) {
6203 if (recovery_type == NFS_LOCK_RECLAIM)
6204 data->arg.reclaim = NFS_LOCK_RECLAIM;
6205 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006206 } else
6207 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006208 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006209 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006210 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006211 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006212 if (ret == 0) {
6213 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006214 if (ret)
6215 nfs4_handle_setlk_error(data->server, data->lsp,
6216 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006217 } else
6218 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006219 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006220 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006221 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006222 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223}
6224
6225static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6226{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006227 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006228 struct nfs4_exception exception = {
6229 .inode = state->inode,
6230 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006231 int err;
6232
6233 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006234 /* Cache the lock if possible... */
6235 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6236 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006237 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006238 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006239 break;
6240 nfs4_handle_exception(server, err, &exception);
6241 } while (exception.retry);
6242 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243}
6244
6245static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6246{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006247 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006248 struct nfs4_exception exception = {
6249 .inode = state->inode,
6250 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006251 int err;
6252
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006253 err = nfs4_set_lock_state(state, request);
6254 if (err != 0)
6255 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006256 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006257 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6258 return 0;
6259 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006260 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006261 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6262 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006263 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006264 switch (err) {
6265 default:
6266 goto out;
6267 case -NFS4ERR_GRACE:
6268 case -NFS4ERR_DELAY:
6269 nfs4_handle_exception(server, err, &exception);
6270 err = 0;
6271 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006272 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006273out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006274 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275}
6276
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006277#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006278static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6279{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006280 struct nfs4_lock_state *lsp;
6281 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006282
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006283 status = nfs4_set_lock_state(state, request);
6284 if (status != 0)
6285 return status;
6286 lsp = request->fl_u.nfs4_fl.owner;
6287 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6288 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6289 return 0;
6290 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006291 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006292}
6293#endif
6294
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6296{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006297 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006298 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006299 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006300 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301
Trond Myklebust01c3b862006-06-29 16:38:39 -04006302 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006303 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006304 if (status < 0)
6305 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006306 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006307 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006308 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006309 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006310 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006311 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006312 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006313 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006314 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006315 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006316 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006317 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006318 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006319 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006320out:
6321 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322 return status;
6323}
6324
6325static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6326{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006327 struct nfs4_exception exception = {
6328 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006329 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006330 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331 int err;
6332
6333 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006334 err = _nfs4_proc_setlk(state, cmd, request);
6335 if (err == -NFS4ERR_DENIED)
6336 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006338 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339 } while (exception.retry);
6340 return err;
6341}
6342
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006343#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6344#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6345
6346static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006347nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6348 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006349{
6350 int status = -ERESTARTSYS;
6351 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6352
6353 while(!signalled()) {
6354 status = nfs4_proc_setlk(state, cmd, request);
6355 if ((status != -EAGAIN) || IS_SETLK(cmd))
6356 break;
6357 freezable_schedule_timeout_interruptible(timeout);
6358 timeout *= 2;
6359 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6360 status = -ERESTARTSYS;
6361 }
6362 return status;
6363}
6364
Jeff Laytona1d617d82016-09-17 18:17:39 -04006365#ifdef CONFIG_NFS_V4_1
6366struct nfs4_lock_waiter {
6367 struct task_struct *task;
6368 struct inode *inode;
6369 struct nfs_lowner *owner;
6370 bool notified;
6371};
6372
6373static int
6374nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6375{
6376 int ret;
6377 struct cb_notify_lock_args *cbnl = key;
6378 struct nfs4_lock_waiter *waiter = wait->private;
6379 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6380 *wowner = waiter->owner;
6381
6382 /* Only wake if the callback was for the same owner */
6383 if (lowner->clientid != wowner->clientid ||
6384 lowner->id != wowner->id ||
6385 lowner->s_dev != wowner->s_dev)
6386 return 0;
6387
6388 /* Make sure it's for the right inode */
6389 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6390 return 0;
6391
6392 waiter->notified = true;
6393
6394 /* override "private" so we can use default_wake_function */
6395 wait->private = waiter->task;
6396 ret = autoremove_wake_function(wait, mode, flags, key);
6397 wait->private = waiter;
6398 return ret;
6399}
6400
6401static int
6402nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6403{
6404 int status = -ERESTARTSYS;
6405 unsigned long flags;
6406 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6407 struct nfs_server *server = NFS_SERVER(state->inode);
6408 struct nfs_client *clp = server->nfs_client;
6409 wait_queue_head_t *q = &clp->cl_lock_waitq;
6410 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6411 .id = lsp->ls_seqid.owner_id,
6412 .s_dev = server->s_dev };
6413 struct nfs4_lock_waiter waiter = { .task = current,
6414 .inode = state->inode,
6415 .owner = &owner,
6416 .notified = false };
6417 wait_queue_t wait;
6418
6419 /* Don't bother with waitqueue if we don't expect a callback */
6420 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6421 return nfs4_retry_setlk_simple(state, cmd, request);
6422
6423 init_wait(&wait);
6424 wait.private = &waiter;
6425 wait.func = nfs4_wake_lock_waiter;
6426 add_wait_queue(q, &wait);
6427
6428 while(!signalled()) {
6429 status = nfs4_proc_setlk(state, cmd, request);
6430 if ((status != -EAGAIN) || IS_SETLK(cmd))
6431 break;
6432
6433 status = -ERESTARTSYS;
6434 spin_lock_irqsave(&q->lock, flags);
6435 if (waiter.notified) {
6436 spin_unlock_irqrestore(&q->lock, flags);
6437 continue;
6438 }
6439 set_current_state(TASK_INTERRUPTIBLE);
6440 spin_unlock_irqrestore(&q->lock, flags);
6441
6442 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6443 }
6444
6445 finish_wait(q, &wait);
6446 return status;
6447}
6448#else /* !CONFIG_NFS_V4_1 */
6449static inline int
6450nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6451{
6452 return nfs4_retry_setlk_simple(state, cmd, request);
6453}
6454#endif
6455
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456static int
6457nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6458{
6459 struct nfs_open_context *ctx;
6460 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461 int status;
6462
6463 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006464 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 state = ctx->state;
6466
6467 if (request->fl_start < 0 || request->fl_end < 0)
6468 return -EINVAL;
6469
Trond Myklebustd9531262009-07-21 19:22:38 -04006470 if (IS_GETLK(cmd)) {
6471 if (state != NULL)
6472 return nfs4_proc_getlk(state, F_GETLK, request);
6473 return 0;
6474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475
6476 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6477 return -EINVAL;
6478
Trond Myklebustd9531262009-07-21 19:22:38 -04006479 if (request->fl_type == F_UNLCK) {
6480 if (state != NULL)
6481 return nfs4_proc_unlck(state, cmd, request);
6482 return 0;
6483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484
Trond Myklebustd9531262009-07-21 19:22:38 -04006485 if (state == NULL)
6486 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006487
6488 if ((request->fl_flags & FL_POSIX) &&
6489 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6490 return -ENOLCK;
6491
Trond Myklebust55725512012-04-18 12:48:35 -04006492 /*
6493 * Don't rely on the VFS having checked the file open mode,
6494 * since it won't do this for flock() locks.
6495 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006496 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006497 case F_RDLCK:
6498 if (!(filp->f_mode & FMODE_READ))
6499 return -EBADF;
6500 break;
6501 case F_WRLCK:
6502 if (!(filp->f_mode & FMODE_WRITE))
6503 return -EBADF;
6504 }
6505
Jeff Layton1ea67db2016-09-17 18:17:37 -04006506 status = nfs4_set_lock_state(state, request);
6507 if (status != 0)
6508 return status;
6509
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006510 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511}
6512
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006513int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006514{
6515 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006516 int err;
6517
6518 err = nfs4_set_lock_state(state, fl);
6519 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006520 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006521 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006522 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006523}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006524
Trond Myklebustcf470c32012-03-07 13:49:12 -05006525struct nfs_release_lockowner_data {
6526 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006527 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006528 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006529 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006530 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006531};
6532
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006533static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6534{
6535 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006536 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006537 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6538 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006539 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006540 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006541}
6542
6543static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6544{
6545 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006546 struct nfs_server *server = data->server;
6547
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006548 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006549
6550 switch (task->tk_status) {
6551 case 0:
6552 renew_lease(server, data->timestamp);
6553 break;
6554 case -NFS4ERR_STALE_CLIENTID:
6555 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006556 nfs4_schedule_lease_recovery(server->nfs_client);
6557 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006558 case -NFS4ERR_LEASE_MOVED:
6559 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006560 if (nfs4_async_handle_error(task, server,
6561 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006562 rpc_restart_call_prepare(task);
6563 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006564}
6565
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006566static void nfs4_release_lockowner_release(void *calldata)
6567{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006568 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006569 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006570 kfree(calldata);
6571}
6572
Trond Myklebust17280172012-03-11 13:11:00 -04006573static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006574 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6575 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006576 .rpc_release = nfs4_release_lockowner_release,
6577};
6578
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006579static void
6580nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006581{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006582 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006583 struct rpc_message msg = {
6584 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6585 };
6586
6587 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006588 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006589
Trond Myklebustcf470c32012-03-07 13:49:12 -05006590 data = kmalloc(sizeof(*data), GFP_NOFS);
6591 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006592 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006593 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006594 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006595 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6596 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6597 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006598
Trond Myklebustcf470c32012-03-07 13:49:12 -05006599 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006600 msg.rpc_resp = &data->res;
6601 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006602 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006603}
6604
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006605#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6606
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006607static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006608 struct dentry *unused, struct inode *inode,
6609 const char *key, const void *buf,
6610 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006611{
Al Viro59301222016-05-27 10:19:30 -04006612 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006613}
6614
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006615static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006616 struct dentry *unused, struct inode *inode,
6617 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006618{
Al Virob2968212016-04-10 20:48:24 -04006619 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006620}
6621
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006622static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006623{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006624 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006625}
6626
David Quigleyc9bccef2013-05-22 12:50:45 -04006627#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006628
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006629static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006630 struct dentry *unused, struct inode *inode,
6631 const char *key, const void *buf,
6632 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006633{
6634 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006635 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006636
6637 return -EOPNOTSUPP;
6638}
6639
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006640static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006641 struct dentry *unused, struct inode *inode,
6642 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006643{
6644 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006645 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006646 return -EOPNOTSUPP;
6647}
6648
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006649static ssize_t
6650nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006651{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006652 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006653
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006654 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6655 len = security_inode_listsecurity(inode, list, list_len);
6656 if (list_len && len > list_len)
6657 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006658 }
6659 return len;
6660}
6661
6662static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6663 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006664 .get = nfs4_xattr_get_nfs4_label,
6665 .set = nfs4_xattr_set_nfs4_label,
6666};
David Quigleyc9bccef2013-05-22 12:50:45 -04006667
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006668#else
6669
6670static ssize_t
6671nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6672{
6673 return 0;
6674}
6675
6676#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006677
Andy Adamson533eb462011-06-13 18:25:56 -04006678/*
6679 * nfs_fhget will use either the mounted_on_fileid or the fileid
6680 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006681static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6682{
Andy Adamson533eb462011-06-13 18:25:56 -04006683 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6684 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6685 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006686 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006687 return;
6688
6689 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006690 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006691 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6692 fattr->nlink = 2;
6693}
6694
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006695static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6696 const struct qstr *name,
6697 struct nfs4_fs_locations *fs_locations,
6698 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006699{
6700 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006701 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006702 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006703 };
6704 struct nfs4_fs_locations_arg args = {
6705 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006706 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006707 .page = page,
6708 .bitmask = bitmask,
6709 };
Benny Halevy22958462009-04-01 09:22:02 -04006710 struct nfs4_fs_locations_res res = {
6711 .fs_locations = fs_locations,
6712 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006713 struct rpc_message msg = {
6714 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6715 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006716 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006717 };
6718 int status;
6719
Harvey Harrison3110ff82008-05-02 13:42:44 -07006720 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006721
6722 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6723 * is not supported */
6724 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6725 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6726 else
6727 bitmask[0] |= FATTR4_WORD0_FILEID;
6728
Trond Myklebustc228fd32007-01-13 02:28:11 -05006729 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006730 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006731 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006732 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006733 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006734 return status;
6735}
6736
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006737int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6738 const struct qstr *name,
6739 struct nfs4_fs_locations *fs_locations,
6740 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006741{
6742 struct nfs4_exception exception = { };
6743 int err;
6744 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006745 err = _nfs4_proc_fs_locations(client, dir, name,
6746 fs_locations, page);
6747 trace_nfs4_get_fs_locations(dir, name, err);
6748 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006749 &exception);
6750 } while (exception.retry);
6751 return err;
6752}
6753
Chuck Leverb03d7352013-10-17 14:12:50 -04006754/*
6755 * This operation also signals the server that this client is
6756 * performing migration recovery. The server can stop returning
6757 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6758 * appended to this compound to identify the client ID which is
6759 * performing recovery.
6760 */
6761static int _nfs40_proc_get_locations(struct inode *inode,
6762 struct nfs4_fs_locations *locations,
6763 struct page *page, struct rpc_cred *cred)
6764{
6765 struct nfs_server *server = NFS_SERVER(inode);
6766 struct rpc_clnt *clnt = server->client;
6767 u32 bitmask[2] = {
6768 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6769 };
6770 struct nfs4_fs_locations_arg args = {
6771 .clientid = server->nfs_client->cl_clientid,
6772 .fh = NFS_FH(inode),
6773 .page = page,
6774 .bitmask = bitmask,
6775 .migration = 1, /* skip LOOKUP */
6776 .renew = 1, /* append RENEW */
6777 };
6778 struct nfs4_fs_locations_res res = {
6779 .fs_locations = locations,
6780 .migration = 1,
6781 .renew = 1,
6782 };
6783 struct rpc_message msg = {
6784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6785 .rpc_argp = &args,
6786 .rpc_resp = &res,
6787 .rpc_cred = cred,
6788 };
6789 unsigned long now = jiffies;
6790 int status;
6791
6792 nfs_fattr_init(&locations->fattr);
6793 locations->server = server;
6794 locations->nlocations = 0;
6795
6796 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6797 nfs4_set_sequence_privileged(&args.seq_args);
6798 status = nfs4_call_sync_sequence(clnt, server, &msg,
6799 &args.seq_args, &res.seq_res);
6800 if (status)
6801 return status;
6802
6803 renew_lease(server, now);
6804 return 0;
6805}
6806
6807#ifdef CONFIG_NFS_V4_1
6808
6809/*
6810 * This operation also signals the server that this client is
6811 * performing migration recovery. The server can stop asserting
6812 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6813 * performing this operation is identified in the SEQUENCE
6814 * operation in this compound.
6815 *
6816 * When the client supports GETATTR(fs_locations_info), it can
6817 * be plumbed in here.
6818 */
6819static int _nfs41_proc_get_locations(struct inode *inode,
6820 struct nfs4_fs_locations *locations,
6821 struct page *page, struct rpc_cred *cred)
6822{
6823 struct nfs_server *server = NFS_SERVER(inode);
6824 struct rpc_clnt *clnt = server->client;
6825 u32 bitmask[2] = {
6826 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6827 };
6828 struct nfs4_fs_locations_arg args = {
6829 .fh = NFS_FH(inode),
6830 .page = page,
6831 .bitmask = bitmask,
6832 .migration = 1, /* skip LOOKUP */
6833 };
6834 struct nfs4_fs_locations_res res = {
6835 .fs_locations = locations,
6836 .migration = 1,
6837 };
6838 struct rpc_message msg = {
6839 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6840 .rpc_argp = &args,
6841 .rpc_resp = &res,
6842 .rpc_cred = cred,
6843 };
6844 int status;
6845
6846 nfs_fattr_init(&locations->fattr);
6847 locations->server = server;
6848 locations->nlocations = 0;
6849
6850 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6851 nfs4_set_sequence_privileged(&args.seq_args);
6852 status = nfs4_call_sync_sequence(clnt, server, &msg,
6853 &args.seq_args, &res.seq_res);
6854 if (status == NFS4_OK &&
6855 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6856 status = -NFS4ERR_LEASE_MOVED;
6857 return status;
6858}
6859
6860#endif /* CONFIG_NFS_V4_1 */
6861
6862/**
6863 * nfs4_proc_get_locations - discover locations for a migrated FSID
6864 * @inode: inode on FSID that is migrating
6865 * @locations: result of query
6866 * @page: buffer
6867 * @cred: credential to use for this operation
6868 *
6869 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6870 * operation failed, or a negative errno if a local error occurred.
6871 *
6872 * On success, "locations" is filled in, but if the server has
6873 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6874 * asserted.
6875 *
6876 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6877 * from this client that require migration recovery.
6878 */
6879int nfs4_proc_get_locations(struct inode *inode,
6880 struct nfs4_fs_locations *locations,
6881 struct page *page, struct rpc_cred *cred)
6882{
6883 struct nfs_server *server = NFS_SERVER(inode);
6884 struct nfs_client *clp = server->nfs_client;
6885 const struct nfs4_mig_recovery_ops *ops =
6886 clp->cl_mvops->mig_recovery_ops;
6887 struct nfs4_exception exception = { };
6888 int status;
6889
6890 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6891 (unsigned long long)server->fsid.major,
6892 (unsigned long long)server->fsid.minor,
6893 clp->cl_hostname);
6894 nfs_display_fhandle(NFS_FH(inode), __func__);
6895
6896 do {
6897 status = ops->get_locations(inode, locations, page, cred);
6898 if (status != -NFS4ERR_DELAY)
6899 break;
6900 nfs4_handle_exception(server, status, &exception);
6901 } while (exception.retry);
6902 return status;
6903}
6904
Chuck Lever44c99932013-10-17 14:13:30 -04006905/*
6906 * This operation also signals the server that this client is
6907 * performing "lease moved" recovery. The server can stop
6908 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6909 * is appended to this compound to identify the client ID which is
6910 * performing recovery.
6911 */
6912static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6913{
6914 struct nfs_server *server = NFS_SERVER(inode);
6915 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6916 struct rpc_clnt *clnt = server->client;
6917 struct nfs4_fsid_present_arg args = {
6918 .fh = NFS_FH(inode),
6919 .clientid = clp->cl_clientid,
6920 .renew = 1, /* append RENEW */
6921 };
6922 struct nfs4_fsid_present_res res = {
6923 .renew = 1,
6924 };
6925 struct rpc_message msg = {
6926 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6927 .rpc_argp = &args,
6928 .rpc_resp = &res,
6929 .rpc_cred = cred,
6930 };
6931 unsigned long now = jiffies;
6932 int status;
6933
6934 res.fh = nfs_alloc_fhandle();
6935 if (res.fh == NULL)
6936 return -ENOMEM;
6937
6938 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6939 nfs4_set_sequence_privileged(&args.seq_args);
6940 status = nfs4_call_sync_sequence(clnt, server, &msg,
6941 &args.seq_args, &res.seq_res);
6942 nfs_free_fhandle(res.fh);
6943 if (status)
6944 return status;
6945
6946 do_renew_lease(clp, now);
6947 return 0;
6948}
6949
6950#ifdef CONFIG_NFS_V4_1
6951
6952/*
6953 * This operation also signals the server that this client is
6954 * performing "lease moved" recovery. The server can stop asserting
6955 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6956 * this operation is identified in the SEQUENCE operation in this
6957 * compound.
6958 */
6959static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6960{
6961 struct nfs_server *server = NFS_SERVER(inode);
6962 struct rpc_clnt *clnt = server->client;
6963 struct nfs4_fsid_present_arg args = {
6964 .fh = NFS_FH(inode),
6965 };
6966 struct nfs4_fsid_present_res res = {
6967 };
6968 struct rpc_message msg = {
6969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6970 .rpc_argp = &args,
6971 .rpc_resp = &res,
6972 .rpc_cred = cred,
6973 };
6974 int status;
6975
6976 res.fh = nfs_alloc_fhandle();
6977 if (res.fh == NULL)
6978 return -ENOMEM;
6979
6980 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6981 nfs4_set_sequence_privileged(&args.seq_args);
6982 status = nfs4_call_sync_sequence(clnt, server, &msg,
6983 &args.seq_args, &res.seq_res);
6984 nfs_free_fhandle(res.fh);
6985 if (status == NFS4_OK &&
6986 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6987 status = -NFS4ERR_LEASE_MOVED;
6988 return status;
6989}
6990
6991#endif /* CONFIG_NFS_V4_1 */
6992
6993/**
6994 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6995 * @inode: inode on FSID to check
6996 * @cred: credential to use for this operation
6997 *
6998 * Server indicates whether the FSID is present, moved, or not
6999 * recognized. This operation is necessary to clear a LEASE_MOVED
7000 * condition for this client ID.
7001 *
7002 * Returns NFS4_OK if the FSID is present on this server,
7003 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7004 * NFS4ERR code if some error occurred on the server, or a
7005 * negative errno if a local failure occurred.
7006 */
7007int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7008{
7009 struct nfs_server *server = NFS_SERVER(inode);
7010 struct nfs_client *clp = server->nfs_client;
7011 const struct nfs4_mig_recovery_ops *ops =
7012 clp->cl_mvops->mig_recovery_ops;
7013 struct nfs4_exception exception = { };
7014 int status;
7015
7016 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7017 (unsigned long long)server->fsid.major,
7018 (unsigned long long)server->fsid.minor,
7019 clp->cl_hostname);
7020 nfs_display_fhandle(NFS_FH(inode), __func__);
7021
7022 do {
7023 status = ops->fsid_present(inode, cred);
7024 if (status != -NFS4ERR_DELAY)
7025 break;
7026 nfs4_handle_exception(server, status, &exception);
7027 } while (exception.retry);
7028 return status;
7029}
7030
Andy Adamson5ec16a82013-08-08 10:57:55 -04007031/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007032 * If 'use_integrity' is true and the state managment nfs_client
7033 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7034 * and the machine credential as per RFC3530bis and RFC5661 Security
7035 * Considerations sections. Otherwise, just use the user cred with the
7036 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007037 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007038static 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 +00007039{
7040 int status;
7041 struct nfs4_secinfo_arg args = {
7042 .dir_fh = NFS_FH(dir),
7043 .name = name,
7044 };
7045 struct nfs4_secinfo_res res = {
7046 .flavors = flavors,
7047 };
7048 struct rpc_message msg = {
7049 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7050 .rpc_argp = &args,
7051 .rpc_resp = &res,
7052 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007053 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007054 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007055
7056 if (use_integrity) {
7057 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007058 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7059 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007060 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007061
7062 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007063
7064 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7065 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7066
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007067 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7068 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007069 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007070
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007071 if (cred)
7072 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007073
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007074 return status;
7075}
7076
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007077int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7078 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007079{
7080 struct nfs4_exception exception = { };
7081 int err;
7082 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007083 err = -NFS4ERR_WRONGSEC;
7084
7085 /* try to use integrity protection with machine cred */
7086 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7087 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7088
7089 /*
7090 * if unable to use integrity protection, or SECINFO with
7091 * integrity protection returns NFS4ERR_WRONGSEC (which is
7092 * disallowed by spec, but exists in deployed servers) use
7093 * the current filesystem's rpc_client and the user cred.
7094 */
7095 if (err == -NFS4ERR_WRONGSEC)
7096 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7097
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007098 trace_nfs4_secinfo(dir, name, err);
7099 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007100 &exception);
7101 } while (exception.retry);
7102 return err;
7103}
7104
Andy Adamson557134a2009-04-01 09:21:53 -04007105#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007106/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007107 * Check the exchange flags returned by the server for invalid flags, having
7108 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7109 * DS flags set.
7110 */
7111static int nfs4_check_cl_exchange_flags(u32 flags)
7112{
7113 if (flags & ~EXCHGID4_FLAG_MASK_R)
7114 goto out_inval;
7115 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7116 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7117 goto out_inval;
7118 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7119 goto out_inval;
7120 return NFS_OK;
7121out_inval:
7122 return -NFS4ERR_INVAL;
7123}
7124
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007125static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007126nfs41_same_server_scope(struct nfs41_server_scope *a,
7127 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007128{
7129 if (a->server_scope_sz == b->server_scope_sz &&
7130 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7131 return true;
7132
7133 return false;
7134}
7135
Andy Adamson02a95de2016-02-05 16:08:37 -05007136static void
7137nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7138{
7139}
7140
7141static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7142 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7143};
7144
Andy Adamson357f54d2010-12-14 10:11:57 -05007145/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007146 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007147 *
7148 * The 4.1 client currently uses the same TCP connection for the
7149 * fore and backchannel.
7150 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007151static
7152int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7153 struct rpc_xprt *xprt,
7154 struct nfs_client *clp,
7155 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007156{
7157 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007158 struct nfs41_bind_conn_to_session_args args = {
7159 .client = clp,
7160 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7161 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007162 struct nfs41_bind_conn_to_session_res res;
7163 struct rpc_message msg = {
7164 .rpc_proc =
7165 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007166 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007167 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007168 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007169 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007170 struct rpc_task_setup task_setup_data = {
7171 .rpc_client = clnt,
7172 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007173 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007174 .rpc_message = &msg,
7175 .flags = RPC_TASK_TIMEOUT,
7176 };
7177 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007178
7179 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007180
Trond Myklebust71a097c2015-02-18 09:27:18 -08007181 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7182 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7183 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007184
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007185 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7186 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7187 args.dir = NFS4_CDFC4_FORE;
7188
7189 task = rpc_run_task(&task_setup_data);
7190 if (!IS_ERR(task)) {
7191 status = task->tk_status;
7192 rpc_put_task(task);
7193 } else
7194 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007195 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007196 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007197 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007198 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7199 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7200 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007201 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007202 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007203 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007204 dprintk("NFS: %s: Unexpected direction from server\n",
7205 __func__);
7206 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007207 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007208 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007209 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007210 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7211 __func__);
7212 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007213 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007214 }
7215 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007216out:
7217 dprintk("<-- %s status= %d\n", __func__, status);
7218 return status;
7219}
7220
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007221struct rpc_bind_conn_calldata {
7222 struct nfs_client *clp;
7223 struct rpc_cred *cred;
7224};
7225
7226static int
7227nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7228 struct rpc_xprt *xprt,
7229 void *calldata)
7230{
7231 struct rpc_bind_conn_calldata *p = calldata;
7232
7233 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7234}
7235
7236int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7237{
7238 struct rpc_bind_conn_calldata data = {
7239 .clp = clp,
7240 .cred = cred,
7241 };
7242 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7243 nfs4_proc_bind_conn_to_session_callback, &data);
7244}
7245
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007246/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007247 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7248 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007249 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007250static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7251 .how = SP4_MACH_CRED,
7252 .enforce.u.words = {
7253 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7254 1 << (OP_EXCHANGE_ID - 32) |
7255 1 << (OP_CREATE_SESSION - 32) |
7256 1 << (OP_DESTROY_SESSION - 32) |
7257 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007258 },
7259 .allow.u.words = {
7260 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007261 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007262 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007263 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007264 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007265 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007266 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007267 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007268 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007269 1 << (OP_FREE_STATEID - 32) |
7270 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007271 }
7272};
7273
7274/*
7275 * Select the state protection mode for client `clp' given the server results
7276 * from exchange_id in `sp'.
7277 *
7278 * Returns 0 on success, negative errno otherwise.
7279 */
7280static int nfs4_sp4_select_mode(struct nfs_client *clp,
7281 struct nfs41_state_protection *sp)
7282{
7283 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7284 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7285 1 << (OP_EXCHANGE_ID - 32) |
7286 1 << (OP_CREATE_SESSION - 32) |
7287 1 << (OP_DESTROY_SESSION - 32) |
7288 1 << (OP_DESTROY_CLIENTID - 32)
7289 };
7290 unsigned int i;
7291
7292 if (sp->how == SP4_MACH_CRED) {
7293 /* Print state protect result */
7294 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7295 for (i = 0; i <= LAST_NFS4_OP; i++) {
7296 if (test_bit(i, sp->enforce.u.longs))
7297 dfprintk(MOUNT, " enforce op %d\n", i);
7298 if (test_bit(i, sp->allow.u.longs))
7299 dfprintk(MOUNT, " allow op %d\n", i);
7300 }
7301
7302 /* make sure nothing is on enforce list that isn't supported */
7303 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7304 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7305 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7306 return -EINVAL;
7307 }
7308 }
7309
7310 /*
7311 * Minimal mode - state operations are allowed to use machine
7312 * credential. Note this already happens by default, so the
7313 * client doesn't have to do anything more than the negotiation.
7314 *
7315 * NOTE: we don't care if EXCHANGE_ID is in the list -
7316 * we're already using the machine cred for exchange_id
7317 * and will never use a different cred.
7318 */
7319 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7320 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7321 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7322 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7323 dfprintk(MOUNT, "sp4_mach_cred:\n");
7324 dfprintk(MOUNT, " minimal mode enabled\n");
7325 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7326 } else {
7327 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7328 return -EINVAL;
7329 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007330
7331 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007332 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7333 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007334 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7335 dfprintk(MOUNT, " cleanup mode enabled\n");
7336 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7337 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007338
Andrew Elble99ade3c2015-12-02 09:39:51 -05007339 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7340 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7341 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7342 &clp->cl_sp4_flags);
7343 }
7344
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007345 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7346 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7347 dfprintk(MOUNT, " secinfo mode enabled\n");
7348 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7349 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007350
7351 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7352 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7353 dfprintk(MOUNT, " stateid mode enabled\n");
7354 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7355 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007356
7357 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7358 dfprintk(MOUNT, " write mode enabled\n");
7359 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7360 }
7361
7362 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7363 dfprintk(MOUNT, " commit mode enabled\n");
7364 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7365 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007366 }
7367
7368 return 0;
7369}
7370
Andy Adamson8d89bd72016-09-09 09:22:18 -04007371struct nfs41_exchange_id_data {
7372 struct nfs41_exchange_id_res res;
7373 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007374 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007375 int rpc_status;
7376};
7377
7378static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007379{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007380 struct nfs41_exchange_id_data *cdata =
7381 (struct nfs41_exchange_id_data *)data;
7382 struct nfs_client *clp = cdata->args.client;
7383 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007384
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007385 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007386
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007387 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007388 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007389
7390 if (cdata->xprt && status == 0) {
7391 status = nfs4_detect_session_trunking(clp, &cdata->res,
7392 cdata->xprt);
7393 goto out;
7394 }
7395
Andy Adamson8d89bd72016-09-09 09:22:18 -04007396 if (status == 0)
7397 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007398
Chuck Lever177313f2012-05-21 22:44:58 -04007399 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007400 clp->cl_clientid = cdata->res.clientid;
7401 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007402 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007403 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007404 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007405 &clp->cl_session->session_state);
7406 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007407 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007408
Chuck Leveracdeb692012-05-21 22:46:16 -04007409 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007410 clp->cl_serverowner = cdata->res.server_owner;
7411 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007412
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007413 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007414 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007415 clp->cl_implid = cdata->res.impl_id;
7416 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007417
Chuck Lever177313f2012-05-21 22:44:58 -04007418 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007419 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007420 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007421 dprintk("%s: server_scope mismatch detected\n",
7422 __func__);
7423 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007424 kfree(clp->cl_serverscope);
7425 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007426 }
7427
Chuck Lever177313f2012-05-21 22:44:58 -04007428 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007429 clp->cl_serverscope = cdata->res.server_scope;
7430 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007431 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007432 /* Save the EXCHANGE_ID verifier session trunk tests */
7433 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7434 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007435 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007436out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007437 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007438 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007439}
7440
7441static void nfs4_exchange_id_release(void *data)
7442{
7443 struct nfs41_exchange_id_data *cdata =
7444 (struct nfs41_exchange_id_data *)data;
7445
7446 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007447 if (cdata->xprt) {
7448 xprt_put(cdata->xprt);
7449 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7450 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007451 kfree(cdata->res.impl_id);
7452 kfree(cdata->res.server_scope);
7453 kfree(cdata->res.server_owner);
7454 kfree(cdata);
7455}
7456
7457static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7458 .rpc_call_done = nfs4_exchange_id_done,
7459 .rpc_release = nfs4_exchange_id_release,
7460};
7461
Benny Halevy99fe60d2009-04-01 09:22:29 -04007462/*
7463 * _nfs4_proc_exchange_id()
7464 *
7465 * Wrapper for EXCHANGE_ID operation.
7466 */
7467static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007468 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007469{
7470 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007471 struct rpc_message msg = {
7472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007473 .rpc_cred = cred,
7474 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007475 struct rpc_task_setup task_setup_data = {
7476 .rpc_client = clp->cl_rpcclient,
7477 .callback_ops = &nfs4_exchange_id_call_ops,
7478 .rpc_message = &msg,
7479 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7480 };
7481 struct nfs41_exchange_id_data *calldata;
7482 struct rpc_task *task;
7483 int status = -EIO;
7484
7485 if (!atomic_inc_not_zero(&clp->cl_count))
7486 goto out;
7487
7488 status = -ENOMEM;
7489 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7490 if (!calldata)
7491 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007492
Andy Adamsonad0849a2016-09-09 09:22:28 -04007493 if (!xprt)
7494 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007495
7496 status = nfs4_init_uniform_client_string(clp);
7497 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007498 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007499
7500 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7501 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7502 clp->cl_owner_id);
7503
Andy Adamson8d89bd72016-09-09 09:22:18 -04007504 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7505 GFP_NOFS);
7506 status = -ENOMEM;
7507 if (unlikely(calldata->res.server_owner == NULL))
7508 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007509
Andy Adamson8d89bd72016-09-09 09:22:18 -04007510 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007511 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007512 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007513 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007514
Andy Adamson8d89bd72016-09-09 09:22:18 -04007515 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7516 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007517 goto out_server_scope;
7518
7519 switch (sp4_how) {
7520 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007521 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007522 break;
7523
7524 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007525 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007526 break;
7527
7528 default:
7529 /* unsupported! */
7530 WARN_ON_ONCE(1);
7531 status = -EINVAL;
7532 goto out_impl_id;
7533 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007534 if (xprt) {
7535 calldata->xprt = xprt;
7536 task_setup_data.rpc_xprt = xprt;
7537 task_setup_data.flags =
7538 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7539 calldata->args.verifier = &clp->cl_confirm;
7540 } else {
7541 calldata->args.verifier = &verifier;
7542 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007543 calldata->args.client = clp;
7544#ifdef CONFIG_NFS_V4_1_MIGRATION
7545 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7546 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7547 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7548#else
7549 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7550 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7551#endif
7552 msg.rpc_argp = &calldata->args;
7553 msg.rpc_resp = &calldata->res;
7554 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007555
Andy Adamson8d89bd72016-09-09 09:22:18 -04007556 task = rpc_run_task(&task_setup_data);
7557 if (IS_ERR(task)) {
7558 status = PTR_ERR(task);
7559 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007560 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007561
Andy Adamsonad0849a2016-09-09 09:22:28 -04007562 if (!xprt) {
7563 status = rpc_wait_for_completion_task(task);
7564 if (!status)
7565 status = calldata->rpc_status;
7566 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007567 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007568
Andy Adamson8d89bd72016-09-09 09:22:18 -04007569 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007570out:
Chuck Lever177313f2012-05-21 22:44:58 -04007571 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007572 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007573 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007574 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007575 clp->cl_implid->date.seconds,
7576 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007577 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007578 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007579
7580out_impl_id:
7581 kfree(calldata->res.impl_id);
7582out_server_scope:
7583 kfree(calldata->res.server_scope);
7584out_server_owner:
7585 kfree(calldata->res.server_owner);
7586out_calldata:
7587 kfree(calldata);
7588 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007589}
7590
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007591/*
7592 * nfs4_proc_exchange_id()
7593 *
7594 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7595 *
7596 * Since the clientid has expired, all compounds using sessions
7597 * associated with the stale clientid will be returning
7598 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7599 * be in some phase of session reset.
7600 *
7601 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7602 */
7603int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7604{
7605 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7606 int status;
7607
7608 /* try SP4_MACH_CRED if krb5i/p */
7609 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7610 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007611 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007612 if (!status)
7613 return 0;
7614 }
7615
7616 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007617 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007618}
7619
Andy Adamson04fa2c62016-09-09 09:22:29 -04007620/**
7621 * nfs4_test_session_trunk
7622 *
7623 * This is an add_xprt_test() test function called from
7624 * rpc_clnt_setup_test_and_add_xprt.
7625 *
7626 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7627 * and is dereferrenced in nfs4_exchange_id_release
7628 *
7629 * Upon success, add the new transport to the rpc_clnt
7630 *
7631 * @clnt: struct rpc_clnt to get new transport
7632 * @xprt: the rpc_xprt to test
7633 * @data: call data for _nfs4_proc_exchange_id.
7634 */
7635int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7636 void *data)
7637{
7638 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7639 u32 sp4_how;
7640
7641 dprintk("--> %s try %s\n", __func__,
7642 xprt->address_strings[RPC_DISPLAY_ADDR]);
7643
7644 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7645
7646 /* Test connection for session trunking. Async exchange_id call */
7647 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7648}
7649EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7650
Trond Myklebust66245532012-05-25 17:18:09 -04007651static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7652 struct rpc_cred *cred)
7653{
7654 struct rpc_message msg = {
7655 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7656 .rpc_argp = clp,
7657 .rpc_cred = cred,
7658 };
7659 int status;
7660
7661 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007662 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007663 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007664 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007665 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7666 return status;
7667}
7668
7669static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7670 struct rpc_cred *cred)
7671{
7672 unsigned int loop;
7673 int ret;
7674
7675 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7676 ret = _nfs4_proc_destroy_clientid(clp, cred);
7677 switch (ret) {
7678 case -NFS4ERR_DELAY:
7679 case -NFS4ERR_CLIENTID_BUSY:
7680 ssleep(1);
7681 break;
7682 default:
7683 return ret;
7684 }
7685 }
7686 return 0;
7687}
7688
7689int nfs4_destroy_clientid(struct nfs_client *clp)
7690{
7691 struct rpc_cred *cred;
7692 int ret = 0;
7693
7694 if (clp->cl_mvops->minor_version < 1)
7695 goto out;
7696 if (clp->cl_exchange_flags == 0)
7697 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007698 if (clp->cl_preserve_clid)
7699 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007700 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007701 ret = nfs4_proc_destroy_clientid(clp, cred);
7702 if (cred)
7703 put_rpccred(cred);
7704 switch (ret) {
7705 case 0:
7706 case -NFS4ERR_STALE_CLIENTID:
7707 clp->cl_exchange_flags = 0;
7708 }
7709out:
7710 return ret;
7711}
7712
Andy Adamson2050f0c2009-04-01 09:22:30 -04007713struct nfs4_get_lease_time_data {
7714 struct nfs4_get_lease_time_args *args;
7715 struct nfs4_get_lease_time_res *res;
7716 struct nfs_client *clp;
7717};
7718
7719static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7720 void *calldata)
7721{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007722 struct nfs4_get_lease_time_data *data =
7723 (struct nfs4_get_lease_time_data *)calldata;
7724
7725 dprintk("--> %s\n", __func__);
7726 /* just setup sequence, do not trigger session recovery
7727 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007728 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007729 &data->args->la_seq_args,
7730 &data->res->lr_seq_res,
7731 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007732 dprintk("<-- %s\n", __func__);
7733}
7734
7735/*
7736 * Called from nfs4_state_manager thread for session setup, so don't recover
7737 * from sequence operation or clientid errors.
7738 */
7739static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7740{
7741 struct nfs4_get_lease_time_data *data =
7742 (struct nfs4_get_lease_time_data *)calldata;
7743
7744 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007745 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7746 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007747 switch (task->tk_status) {
7748 case -NFS4ERR_DELAY:
7749 case -NFS4ERR_GRACE:
7750 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7751 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7752 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007753 /* fall through */
7754 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007755 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007756 return;
7757 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007758 dprintk("<-- %s\n", __func__);
7759}
7760
Trond Myklebust17280172012-03-11 13:11:00 -04007761static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007762 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7763 .rpc_call_done = nfs4_get_lease_time_done,
7764};
7765
7766int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7767{
7768 struct rpc_task *task;
7769 struct nfs4_get_lease_time_args args;
7770 struct nfs4_get_lease_time_res res = {
7771 .lr_fsinfo = fsinfo,
7772 };
7773 struct nfs4_get_lease_time_data data = {
7774 .args = &args,
7775 .res = &res,
7776 .clp = clp,
7777 };
7778 struct rpc_message msg = {
7779 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7780 .rpc_argp = &args,
7781 .rpc_resp = &res,
7782 };
7783 struct rpc_task_setup task_setup = {
7784 .rpc_client = clp->cl_rpcclient,
7785 .rpc_message = &msg,
7786 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007787 .callback_data = &data,
7788 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007789 };
7790 int status;
7791
Chuck Levera9c92d62013-08-09 12:48:18 -04007792 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007793 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007794 dprintk("--> %s\n", __func__);
7795 task = rpc_run_task(&task_setup);
7796
7797 if (IS_ERR(task))
7798 status = PTR_ERR(task);
7799 else {
7800 status = task->tk_status;
7801 rpc_put_task(task);
7802 }
7803 dprintk("<-- %s return %d\n", __func__, status);
7804
7805 return status;
7806}
7807
Andy Adamsonfc931582009-04-01 09:22:31 -04007808/*
7809 * Initialize the values to be used by the client in CREATE_SESSION
7810 * If nfs4_init_session set the fore channel request and response sizes,
7811 * use them.
7812 *
7813 * Set the back channel max_resp_sz_cached to zero to force the client to
7814 * always set csa_cachethis to FALSE because the current implementation
7815 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7816 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007817static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7818 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007819{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007820 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007821 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007822
Andy Adamson18aad3d2013-06-26 12:21:49 -04007823 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7824 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7825
Andy Adamsonfc931582009-04-01 09:22:31 -04007826 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007827 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7828 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007829 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007830 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007831
7832 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007833 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007834 __func__,
7835 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007836 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007837
7838 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007839 args->bc_attrs.max_rqst_sz = max_bc_payload;
7840 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007841 args->bc_attrs.max_resp_sz_cached = 0;
7842 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007843 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007844
7845 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7846 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7847 __func__,
7848 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7849 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7850 args->bc_attrs.max_reqs);
7851}
7852
Trond Myklebust79969dd2015-02-18 11:30:18 -08007853static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7854 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007855{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007856 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007857 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007858
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007859 if (rcvd->max_resp_sz > sent->max_resp_sz)
7860 return -EINVAL;
7861 /*
7862 * Our requested max_ops is the minimum we need; we're not
7863 * prepared to break up compounds into smaller pieces than that.
7864 * So, no point even trying to continue if the server won't
7865 * cooperate:
7866 */
7867 if (rcvd->max_ops < sent->max_ops)
7868 return -EINVAL;
7869 if (rcvd->max_reqs == 0)
7870 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007871 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7872 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007873 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007874}
7875
Trond Myklebust79969dd2015-02-18 11:30:18 -08007876static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7877 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007878{
7879 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007880 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007881
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007882 if (!(res->flags & SESSION4_BACK_CHAN))
7883 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007884 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7885 return -EINVAL;
7886 if (rcvd->max_resp_sz < sent->max_resp_sz)
7887 return -EINVAL;
7888 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7889 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007890 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007891 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007892 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007893 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007894out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007895 return 0;
7896}
Andy Adamson8d353012009-04-01 09:22:32 -04007897
Andy Adamson8d353012009-04-01 09:22:32 -04007898static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007899 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007900{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007901 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007902
Trond Myklebust79969dd2015-02-18 11:30:18 -08007903 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007904 if (ret)
7905 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007906 return nfs4_verify_back_channel_attrs(args, res);
7907}
7908
7909static void nfs4_update_session(struct nfs4_session *session,
7910 struct nfs41_create_session_res *res)
7911{
7912 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007913 /* Mark client id and session as being confirmed */
7914 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7915 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007916 session->flags = res->flags;
7917 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007918 if (res->flags & SESSION4_BACK_CHAN)
7919 memcpy(&session->bc_attrs, &res->bc_attrs,
7920 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007921}
7922
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007923static int _nfs4_proc_create_session(struct nfs_client *clp,
7924 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007925{
7926 struct nfs4_session *session = clp->cl_session;
7927 struct nfs41_create_session_args args = {
7928 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007929 .clientid = clp->cl_clientid,
7930 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007931 .cb_program = NFS4_CALLBACK,
7932 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007933 struct nfs41_create_session_res res;
7934
Andy Adamsonfc931582009-04-01 09:22:31 -04007935 struct rpc_message msg = {
7936 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7937 .rpc_argp = &args,
7938 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007939 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007940 };
7941 int status;
7942
Chuck Lever6b26cc82016-05-02 14:40:40 -04007943 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007944 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007945
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007946 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007947 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007948
Trond Myklebustb519d402016-09-11 14:50:01 -04007949 switch (status) {
7950 case -NFS4ERR_STALE_CLIENTID:
7951 case -NFS4ERR_DELAY:
7952 case -ETIMEDOUT:
7953 case -EACCES:
7954 case -EAGAIN:
7955 goto out;
7956 };
7957
7958 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007959 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007960 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007961 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007962 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007963 if (status)
7964 goto out;
7965 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007966 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007967out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007968 return status;
7969}
7970
7971/*
7972 * Issues a CREATE_SESSION operation to the server.
7973 * It is the responsibility of the caller to verify the session is
7974 * expired before calling this routine.
7975 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007976int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007977{
7978 int status;
7979 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007980 struct nfs4_session *session = clp->cl_session;
7981
7982 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7983
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007984 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007985 if (status)
7986 goto out;
7987
Andy Adamsonaacd5532011-11-09 13:58:21 -05007988 /* Init or reset the session slot tables */
7989 status = nfs4_setup_session_slot_tables(session);
7990 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007991 if (status)
7992 goto out;
7993
7994 ptr = (unsigned *)&session->sess_id.data[0];
7995 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7996 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007997out:
7998 dprintk("<-- %s\n", __func__);
7999 return status;
8000}
8001
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008002/*
8003 * Issue the over-the-wire RPC DESTROY_SESSION.
8004 * The caller must serialize access to this routine.
8005 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008006int nfs4_proc_destroy_session(struct nfs4_session *session,
8007 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008008{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008009 struct rpc_message msg = {
8010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8011 .rpc_argp = session,
8012 .rpc_cred = cred,
8013 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008014 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008015
8016 dprintk("--> nfs4_proc_destroy_session\n");
8017
8018 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008019 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8020 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008021
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008022 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008023 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008024
8025 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008026 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008027 "Session has been destroyed regardless...\n", status);
8028
8029 dprintk("<-- nfs4_proc_destroy_session\n");
8030 return status;
8031}
8032
Trond Myklebust7b38c362012-05-23 13:23:31 -04008033/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008034 * Renew the cl_session lease.
8035 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008036struct nfs4_sequence_data {
8037 struct nfs_client *clp;
8038 struct nfs4_sequence_args args;
8039 struct nfs4_sequence_res res;
8040};
8041
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008042static void nfs41_sequence_release(void *data)
8043{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008044 struct nfs4_sequence_data *calldata = data;
8045 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008046
Alexandros Batsakis71358402010-02-05 03:45:05 -08008047 if (atomic_read(&clp->cl_count) > 1)
8048 nfs4_schedule_state_renewal(clp);
8049 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008050 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008051}
8052
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008053static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8054{
8055 switch(task->tk_status) {
8056 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008057 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8058 return -EAGAIN;
8059 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008060 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008061 }
8062 return 0;
8063}
8064
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008065static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008066{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008067 struct nfs4_sequence_data *calldata = data;
8068 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008069
Trond Myklebust14516c32010-07-31 14:29:06 -04008070 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8071 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008072
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008073 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008074 if (task->tk_status < 0) {
8075 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008076 if (atomic_read(&clp->cl_count) == 1)
8077 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008078
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008079 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8080 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008081 return;
8082 }
8083 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008084 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008085out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008086 dprintk("<-- %s\n", __func__);
8087}
8088
8089static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8090{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008091 struct nfs4_sequence_data *calldata = data;
8092 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008093 struct nfs4_sequence_args *args;
8094 struct nfs4_sequence_res *res;
8095
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008096 args = task->tk_msg.rpc_argp;
8097 res = task->tk_msg.rpc_resp;
8098
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008099 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008100}
8101
8102static const struct rpc_call_ops nfs41_sequence_ops = {
8103 .rpc_call_done = nfs41_sequence_call_done,
8104 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008105 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008106};
8107
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008108static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8109 struct rpc_cred *cred,
8110 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008111{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008112 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008113 struct rpc_message msg = {
8114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8115 .rpc_cred = cred,
8116 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008117 struct rpc_task_setup task_setup_data = {
8118 .rpc_client = clp->cl_rpcclient,
8119 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008120 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008121 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008122 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008123
Alexandros Batsakis71358402010-02-05 03:45:05 -08008124 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008125 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008126 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008127 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008128 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008129 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008130 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008131 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008132 if (is_privileged)
8133 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008134 msg.rpc_argp = &calldata->args;
8135 msg.rpc_resp = &calldata->res;
8136 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008137 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008138
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008139 return rpc_run_task(&task_setup_data);
8140}
8141
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008142static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008143{
8144 struct rpc_task *task;
8145 int ret = 0;
8146
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008147 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008148 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008149 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008150 if (IS_ERR(task))
8151 ret = PTR_ERR(task);
8152 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008153 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008154 dprintk("<-- %s status=%d\n", __func__, ret);
8155 return ret;
8156}
8157
8158static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8159{
8160 struct rpc_task *task;
8161 int ret;
8162
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008163 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008164 if (IS_ERR(task)) {
8165 ret = PTR_ERR(task);
8166 goto out;
8167 }
8168 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008169 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008170 ret = task->tk_status;
8171 rpc_put_task(task);
8172out:
8173 dprintk("<-- %s status=%d\n", __func__, ret);
8174 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008175}
8176
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008177struct nfs4_reclaim_complete_data {
8178 struct nfs_client *clp;
8179 struct nfs41_reclaim_complete_args arg;
8180 struct nfs41_reclaim_complete_res res;
8181};
8182
8183static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8184{
8185 struct nfs4_reclaim_complete_data *calldata = data;
8186
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008187 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008188 &calldata->arg.seq_args,
8189 &calldata->res.seq_res,
8190 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008191}
8192
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008193static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8194{
8195 switch(task->tk_status) {
8196 case 0:
8197 case -NFS4ERR_COMPLETE_ALREADY:
8198 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8199 break;
8200 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008201 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008202 /* fall through */
8203 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008204 return -EAGAIN;
8205 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008206 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008207 }
8208 return 0;
8209}
8210
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008211static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8212{
8213 struct nfs4_reclaim_complete_data *calldata = data;
8214 struct nfs_client *clp = calldata->clp;
8215 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8216
8217 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008218 if (!nfs41_sequence_done(task, res))
8219 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008220
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008221 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008222 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8223 rpc_restart_call_prepare(task);
8224 return;
8225 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008226 dprintk("<-- %s\n", __func__);
8227}
8228
8229static void nfs4_free_reclaim_complete_data(void *data)
8230{
8231 struct nfs4_reclaim_complete_data *calldata = data;
8232
8233 kfree(calldata);
8234}
8235
8236static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8237 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8238 .rpc_call_done = nfs4_reclaim_complete_done,
8239 .rpc_release = nfs4_free_reclaim_complete_data,
8240};
8241
8242/*
8243 * Issue a global reclaim complete.
8244 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008245static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8246 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008247{
8248 struct nfs4_reclaim_complete_data *calldata;
8249 struct rpc_task *task;
8250 struct rpc_message msg = {
8251 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008252 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008253 };
8254 struct rpc_task_setup task_setup_data = {
8255 .rpc_client = clp->cl_rpcclient,
8256 .rpc_message = &msg,
8257 .callback_ops = &nfs4_reclaim_complete_call_ops,
8258 .flags = RPC_TASK_ASYNC,
8259 };
8260 int status = -ENOMEM;
8261
8262 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008263 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008264 if (calldata == NULL)
8265 goto out;
8266 calldata->clp = clp;
8267 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008268
Chuck Levera9c92d62013-08-09 12:48:18 -04008269 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008270 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008271 msg.rpc_argp = &calldata->arg;
8272 msg.rpc_resp = &calldata->res;
8273 task_setup_data.callback_data = calldata;
8274 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008275 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008276 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008277 goto out;
8278 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008279 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008280 if (status == 0)
8281 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008282 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008283 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008284out:
8285 dprintk("<-- %s status=%d\n", __func__, status);
8286 return status;
8287}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008288
8289static void
8290nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8291{
8292 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008293 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008294
8295 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008296 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008297 &lgp->res.seq_res, task);
8298 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008299}
8300
8301static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8302{
8303 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008304
8305 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008306 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008307 dprintk("<-- %s\n", __func__);
8308}
8309
8310static int
8311nfs4_layoutget_handle_exception(struct rpc_task *task,
8312 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8313{
Trond Myklebustee314c22012-10-01 17:25:48 -07008314 struct inode *inode = lgp->args.inode;
8315 struct nfs_server *server = NFS_SERVER(inode);
8316 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008317 int nfs4err = task->tk_status;
8318 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008319 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008320
Boaz Harroshed7e5422014-01-22 20:34:54 +02008321 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008322
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008323 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008324 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008325 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008326
8327 /*
8328 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8329 * on the file. set tk_status to -ENODATA to tell upper layer to
8330 * retry go inband.
8331 */
8332 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008333 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008334 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008335 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008336 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8337 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8338 */
8339 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008340 status = -EOVERFLOW;
8341 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008342 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008343 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008344 * (or clients) writing to the same RAID stripe except when
8345 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008346 *
8347 * Treat it like we would RECALLCONFLICT -- we retry for a little
8348 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008349 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008350 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008351 if (lgp->args.minlength == 0) {
8352 status = -EOVERFLOW;
8353 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008354 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008355 status = -EBUSY;
8356 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008357 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008358 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008359 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008360 case -NFS4ERR_DELEG_REVOKED:
8361 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008362 case -NFS4ERR_EXPIRED:
8363 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008364 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008365 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008366 lo = NFS_I(inode)->layout;
8367 /* If the open stateid was bad, then recover it. */
8368 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8369 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008370 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008371 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008372 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008373 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008374 break;
8375 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008376
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008377 /*
8378 * Mark the bad layout state as invalid, then retry
8379 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008380 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008381 spin_unlock(&inode->i_lock);
8382 pnfs_free_lseg_list(&head);
8383 status = -EAGAIN;
8384 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008385 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008386
Trond Myklebust8ac09252017-01-23 22:44:12 -05008387 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008388 err = nfs4_handle_exception(server, nfs4err, exception);
8389 if (!status) {
8390 if (exception->retry)
8391 status = -EAGAIN;
8392 else
8393 status = err;
8394 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008395out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008396 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008397 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008398}
8399
Idan Kedar85541162012-08-02 11:47:10 +03008400static size_t max_response_pages(struct nfs_server *server)
8401{
8402 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8403 return nfs_page_array_len(0, max_resp_sz);
8404}
8405
8406static void nfs4_free_pages(struct page **pages, size_t size)
8407{
8408 int i;
8409
8410 if (!pages)
8411 return;
8412
8413 for (i = 0; i < size; i++) {
8414 if (!pages[i])
8415 break;
8416 __free_page(pages[i]);
8417 }
8418 kfree(pages);
8419}
8420
8421static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8422{
8423 struct page **pages;
8424 int i;
8425
8426 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8427 if (!pages) {
8428 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8429 return NULL;
8430 }
8431
8432 for (i = 0; i < size; i++) {
8433 pages[i] = alloc_page(gfp_flags);
8434 if (!pages[i]) {
8435 dprintk("%s: failed to allocate page\n", __func__);
8436 nfs4_free_pages(pages, size);
8437 return NULL;
8438 }
8439 }
8440
8441 return pages;
8442}
8443
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008444static void nfs4_layoutget_release(void *calldata)
8445{
8446 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008447 struct inode *inode = lgp->args.inode;
8448 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008449 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008450
8451 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008452 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008453 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008454 put_nfs_open_context(lgp->args.ctx);
8455 kfree(calldata);
8456 dprintk("<-- %s\n", __func__);
8457}
8458
8459static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8460 .rpc_call_prepare = nfs4_layoutget_prepare,
8461 .rpc_call_done = nfs4_layoutget_done,
8462 .rpc_release = nfs4_layoutget_release,
8463};
8464
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008465struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008466nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008467{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008468 struct inode *inode = lgp->args.inode;
8469 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008470 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008471 struct rpc_task *task;
8472 struct rpc_message msg = {
8473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8474 .rpc_argp = &lgp->args,
8475 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008476 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008477 };
8478 struct rpc_task_setup task_setup_data = {
8479 .rpc_client = server->client,
8480 .rpc_message = &msg,
8481 .callback_ops = &nfs4_layoutget_call_ops,
8482 .callback_data = lgp,
8483 .flags = RPC_TASK_ASYNC,
8484 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008485 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008486 struct nfs4_exception exception = {
8487 .inode = inode,
8488 .timeout = *timeout,
8489 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008490 int status = 0;
8491
8492 dprintk("--> %s\n", __func__);
8493
Peng Tao4bd5a982014-11-17 11:05:17 +08008494 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8495 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8496
Idan Kedar85541162012-08-02 11:47:10 +03008497 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8498 if (!lgp->args.layout.pages) {
8499 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008500 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008501 }
8502 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8503
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008504 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008505 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008506 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008507
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008508 task = rpc_run_task(&task_setup_data);
8509 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008510 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008511 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008512 if (status == 0) {
8513 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8514 *timeout = exception.timeout;
8515 }
8516
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008517 trace_nfs4_layoutget(lgp->args.ctx,
8518 &lgp->args.range,
8519 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008520 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008521 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008522
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008523 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8524 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008525 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008526 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008527 rpc_put_task(task);
8528 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008529 if (status)
8530 return ERR_PTR(status);
8531 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008532}
8533
Benny Halevycbe82602011-05-22 19:52:37 +03008534static void
8535nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8536{
8537 struct nfs4_layoutreturn *lrp = calldata;
8538
8539 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008540 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008541 &lrp->args.seq_args,
8542 &lrp->res.seq_res,
8543 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008544}
8545
8546static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8547{
8548 struct nfs4_layoutreturn *lrp = calldata;
8549 struct nfs_server *server;
8550
8551 dprintk("--> %s\n", __func__);
8552
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008553 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008554 return;
8555
8556 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008557 switch (task->tk_status) {
8558 default:
8559 task->tk_status = 0;
8560 case 0:
8561 break;
8562 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008563 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008564 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008565 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008566 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008567 return;
8568 }
Benny Halevycbe82602011-05-22 19:52:37 +03008569 dprintk("<-- %s\n", __func__);
8570}
8571
8572static void nfs4_layoutreturn_release(void *calldata)
8573{
8574 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008575 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008576
8577 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008578 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008579 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008580 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008581 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8582 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008583 pnfs_put_layout_hdr(lrp->args.layout);
8584 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008585 kfree(calldata);
8586 dprintk("<-- %s\n", __func__);
8587}
8588
8589static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8590 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8591 .rpc_call_done = nfs4_layoutreturn_done,
8592 .rpc_release = nfs4_layoutreturn_release,
8593};
8594
Peng Tao6c166052014-11-17 09:30:40 +08008595int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008596{
8597 struct rpc_task *task;
8598 struct rpc_message msg = {
8599 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8600 .rpc_argp = &lrp->args,
8601 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008602 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008603 };
8604 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008605 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008606 .rpc_message = &msg,
8607 .callback_ops = &nfs4_layoutreturn_call_ops,
8608 .callback_data = lrp,
8609 };
Peng Tao6c166052014-11-17 09:30:40 +08008610 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008611
Andrew Elble99ade3c2015-12-02 09:39:51 -05008612 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8613 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8614 &task_setup_data.rpc_client, &msg);
8615
Benny Halevycbe82602011-05-22 19:52:37 +03008616 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008617 if (!sync) {
8618 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8619 if (!lrp->inode) {
8620 nfs4_layoutreturn_release(lrp);
8621 return -EAGAIN;
8622 }
8623 task_setup_data.flags |= RPC_TASK_ASYNC;
8624 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008625 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008626 task = rpc_run_task(&task_setup_data);
8627 if (IS_ERR(task))
8628 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008629 if (sync)
8630 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008631 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008632 dprintk("<-- %s status=%d\n", __func__, status);
8633 rpc_put_task(task);
8634 return status;
8635}
8636
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008637static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008638_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8639 struct pnfs_device *pdev,
8640 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008641{
8642 struct nfs4_getdeviceinfo_args args = {
8643 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008644 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8645 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008646 };
8647 struct nfs4_getdeviceinfo_res res = {
8648 .pdev = pdev,
8649 };
8650 struct rpc_message msg = {
8651 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8652 .rpc_argp = &args,
8653 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008654 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008655 };
8656 int status;
8657
8658 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008659 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008660 if (res.notification & ~args.notify_types)
8661 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008662 if (res.notification != args.notify_types)
8663 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008664
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008665 dprintk("<-- %s status=%d\n", __func__, status);
8666
8667 return status;
8668}
8669
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008670int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8671 struct pnfs_device *pdev,
8672 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008673{
8674 struct nfs4_exception exception = { };
8675 int err;
8676
8677 do {
8678 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008679 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008680 &exception);
8681 } while (exception.retry);
8682 return err;
8683}
8684EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8685
Andy Adamson863a3c62011-03-23 13:27:54 +00008686static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8687{
8688 struct nfs4_layoutcommit_data *data = calldata;
8689 struct nfs_server *server = NFS_SERVER(data->args.inode);
8690
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008691 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008692 &data->args.seq_args,
8693 &data->res.seq_res,
8694 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008695}
8696
8697static void
8698nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8699{
8700 struct nfs4_layoutcommit_data *data = calldata;
8701 struct nfs_server *server = NFS_SERVER(data->args.inode);
8702
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008703 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008704 return;
8705
8706 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008707 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8708 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8709 case -NFS4ERR_BADLAYOUT: /* no layout */
8710 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008711 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008712 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008713 break;
8714 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008715 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008716 rpc_restart_call_prepare(task);
8717 return;
8718 }
8719 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008720}
8721
8722static void nfs4_layoutcommit_release(void *calldata)
8723{
8724 struct nfs4_layoutcommit_data *data = calldata;
8725
Andy Adamsondb29c082011-07-30 20:52:38 -04008726 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008727 nfs_post_op_update_inode_force_wcc(data->args.inode,
8728 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008729 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008730 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008731 kfree(data);
8732}
8733
8734static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8735 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8736 .rpc_call_done = nfs4_layoutcommit_done,
8737 .rpc_release = nfs4_layoutcommit_release,
8738};
8739
8740int
Andy Adamsonef311532011-03-12 02:58:10 -05008741nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008742{
8743 struct rpc_message msg = {
8744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8745 .rpc_argp = &data->args,
8746 .rpc_resp = &data->res,
8747 .rpc_cred = data->cred,
8748 };
8749 struct rpc_task_setup task_setup_data = {
8750 .task = &data->task,
8751 .rpc_client = NFS_CLIENT(data->args.inode),
8752 .rpc_message = &msg,
8753 .callback_ops = &nfs4_layoutcommit_ops,
8754 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008755 };
8756 struct rpc_task *task;
8757 int status = 0;
8758
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008759 dprintk("NFS: initiating layoutcommit call. sync %d "
8760 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008761 data->args.lastbytewritten,
8762 data->args.inode->i_ino);
8763
Trond Myklebust472e2592015-02-05 16:50:30 -05008764 if (!sync) {
8765 data->inode = nfs_igrab_and_active(data->args.inode);
8766 if (data->inode == NULL) {
8767 nfs4_layoutcommit_release(data);
8768 return -EAGAIN;
8769 }
8770 task_setup_data.flags = RPC_TASK_ASYNC;
8771 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008772 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008773 task = rpc_run_task(&task_setup_data);
8774 if (IS_ERR(task))
8775 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008776 if (sync)
8777 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008778 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008779 dprintk("%s: status %d\n", __func__, status);
8780 rpc_put_task(task);
8781 return status;
8782}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008783
Andy Adamson97431202013-08-08 10:57:56 -04008784/**
8785 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8786 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8787 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008788static int
8789_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008790 struct nfs_fsinfo *info,
8791 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008792{
8793 struct nfs41_secinfo_no_name_args args = {
8794 .style = SECINFO_STYLE_CURRENT_FH,
8795 };
8796 struct nfs4_secinfo_res res = {
8797 .flavors = flavors,
8798 };
8799 struct rpc_message msg = {
8800 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8801 .rpc_argp = &args,
8802 .rpc_resp = &res,
8803 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008804 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008805 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008806 int status;
8807
8808 if (use_integrity) {
8809 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008810 cred = nfs4_get_clid_cred(server->nfs_client);
8811 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008812 }
8813
8814 dprintk("--> %s\n", __func__);
8815 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8816 &res.seq_res, 0);
8817 dprintk("<-- %s status=%d\n", __func__, status);
8818
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008819 if (cred)
8820 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008821
8822 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008823}
8824
8825static int
8826nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8827 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8828{
8829 struct nfs4_exception exception = { };
8830 int err;
8831 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008832 /* first try using integrity protection */
8833 err = -NFS4ERR_WRONGSEC;
8834
8835 /* try to use integrity protection with machine cred */
8836 if (_nfs4_is_integrity_protected(server->nfs_client))
8837 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8838 flavors, true);
8839
8840 /*
8841 * if unable to use integrity protection, or SECINFO with
8842 * integrity protection returns NFS4ERR_WRONGSEC (which is
8843 * disallowed by spec, but exists in deployed servers) use
8844 * the current filesystem's rpc_client and the user cred.
8845 */
8846 if (err == -NFS4ERR_WRONGSEC)
8847 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8848 flavors, false);
8849
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008850 switch (err) {
8851 case 0:
8852 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008853 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008854 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008855 default:
8856 err = nfs4_handle_exception(server, err, &exception);
8857 }
8858 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008859out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008860 return err;
8861}
8862
8863static int
8864nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8865 struct nfs_fsinfo *info)
8866{
8867 int err;
8868 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008869 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008870 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008871 struct nfs4_secinfo4 *secinfo;
8872 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008873
8874 page = alloc_page(GFP_KERNEL);
8875 if (!page) {
8876 err = -ENOMEM;
8877 goto out;
8878 }
8879
8880 flavors = page_address(page);
8881 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8882
8883 /*
8884 * Fall back on "guess and check" method if
8885 * the server doesn't support SECINFO_NO_NAME
8886 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008887 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008888 err = nfs4_find_root_sec(server, fhandle, info);
8889 goto out_freepage;
8890 }
8891 if (err)
8892 goto out_freepage;
8893
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008894 for (i = 0; i < flavors->num_flavors; i++) {
8895 secinfo = &flavors->flavors[i];
8896
8897 switch (secinfo->flavor) {
8898 case RPC_AUTH_NULL:
8899 case RPC_AUTH_UNIX:
8900 case RPC_AUTH_GSS:
8901 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8902 &secinfo->flavor_info);
8903 break;
8904 default:
8905 flavor = RPC_AUTH_MAXFLAVOR;
8906 break;
8907 }
8908
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008909 if (!nfs_auth_info_match(&server->auth_info, flavor))
8910 flavor = RPC_AUTH_MAXFLAVOR;
8911
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008912 if (flavor != RPC_AUTH_MAXFLAVOR) {
8913 err = nfs4_lookup_root_sec(server, fhandle,
8914 info, flavor);
8915 if (!err)
8916 break;
8917 }
8918 }
8919
8920 if (flavor == RPC_AUTH_MAXFLAVOR)
8921 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008922
8923out_freepage:
8924 put_page(page);
8925 if (err == -EACCES)
8926 return -EPERM;
8927out:
8928 return err;
8929}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008930
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008931static int _nfs41_test_stateid(struct nfs_server *server,
8932 nfs4_stateid *stateid,
8933 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008934{
8935 int status;
8936 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008937 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008938 };
8939 struct nfs41_test_stateid_res res;
8940 struct rpc_message msg = {
8941 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8942 .rpc_argp = &args,
8943 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008944 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008945 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008946 struct rpc_clnt *rpc_client = server->client;
8947
8948 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8949 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008950
Chuck Lever38527b12012-07-11 16:30:23 -04008951 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008952 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008953 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008954 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008955 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008956 if (status != NFS_OK) {
8957 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008958 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008959 }
8960 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008961 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008962}
8963
Trond Myklebust43912bb2016-09-22 13:38:56 -04008964static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8965 int err, struct nfs4_exception *exception)
8966{
8967 exception->retry = 0;
8968 switch(err) {
8969 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008970 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008971 nfs4_handle_exception(server, err, exception);
8972 break;
8973 case -NFS4ERR_BADSESSION:
8974 case -NFS4ERR_BADSLOT:
8975 case -NFS4ERR_BAD_HIGH_SLOT:
8976 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8977 case -NFS4ERR_DEADSESSION:
8978 nfs4_do_handle_exception(server, err, exception);
8979 }
8980}
8981
Chuck Lever38527b12012-07-11 16:30:23 -04008982/**
8983 * nfs41_test_stateid - perform a TEST_STATEID operation
8984 *
8985 * @server: server / transport on which to perform the operation
8986 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008987 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008988 *
8989 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8990 * Otherwise a negative NFS4ERR value is returned if the operation
8991 * failed or the state ID is not currently valid.
8992 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008993static int nfs41_test_stateid(struct nfs_server *server,
8994 nfs4_stateid *stateid,
8995 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008996{
8997 struct nfs4_exception exception = { };
8998 int err;
8999 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009000 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009001 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009002 } while (exception.retry);
9003 return err;
9004}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009005
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009006struct nfs_free_stateid_data {
9007 struct nfs_server *server;
9008 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009009 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009010};
9011
9012static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9013{
9014 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009015 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009016 &data->args.seq_args,
9017 &data->res.seq_res,
9018 task);
9019}
9020
9021static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9022{
9023 struct nfs_free_stateid_data *data = calldata;
9024
9025 nfs41_sequence_done(task, &data->res.seq_res);
9026
9027 switch (task->tk_status) {
9028 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009029 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009030 rpc_restart_call_prepare(task);
9031 }
9032}
9033
9034static void nfs41_free_stateid_release(void *calldata)
9035{
9036 kfree(calldata);
9037}
9038
Trond Myklebust17f26b12013-08-21 15:48:42 -04009039static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009040 .rpc_call_prepare = nfs41_free_stateid_prepare,
9041 .rpc_call_done = nfs41_free_stateid_done,
9042 .rpc_release = nfs41_free_stateid_release,
9043};
9044
9045static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009046 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009047 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009048 bool privileged)
9049{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009050 struct rpc_message msg = {
9051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009052 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009053 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009054 struct rpc_task_setup task_setup = {
9055 .rpc_client = server->client,
9056 .rpc_message = &msg,
9057 .callback_ops = &nfs41_free_stateid_ops,
9058 .flags = RPC_TASK_ASYNC,
9059 };
9060 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009061
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009062 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9063 &task_setup.rpc_client, &msg);
9064
Chuck Lever38527b12012-07-11 16:30:23 -04009065 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009066 data = kmalloc(sizeof(*data), GFP_NOFS);
9067 if (!data)
9068 return ERR_PTR(-ENOMEM);
9069 data->server = server;
9070 nfs4_stateid_copy(&data->args.stateid, stateid);
9071
9072 task_setup.callback_data = data;
9073
9074 msg.rpc_argp = &data->args;
9075 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009076 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009077 if (privileged)
9078 nfs4_set_sequence_privileged(&data->args.seq_args);
9079
9080 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009081}
9082
Chuck Lever38527b12012-07-11 16:30:23 -04009083/**
9084 * nfs41_free_stateid - perform a FREE_STATEID operation
9085 *
9086 * @server: server / transport on which to perform the operation
9087 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009088 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009089 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009090 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009091 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009092 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009093static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009094 const nfs4_stateid *stateid,
9095 struct rpc_cred *cred,
9096 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009097{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009098 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009099
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009100 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009101 if (IS_ERR(task))
9102 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009103 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009104 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009105}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009106
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009107static void
9108nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009109{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009110 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009111
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009112 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009113 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009114}
9115
Trond Myklebust36281ca2012-03-04 18:13:56 -05009116static bool nfs41_match_stateid(const nfs4_stateid *s1,
9117 const nfs4_stateid *s2)
9118{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009119 if (s1->type != s2->type)
9120 return false;
9121
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009122 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009123 return false;
9124
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009125 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009126 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009127 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009128 return true;
9129
9130 return false;
9131}
9132
Andy Adamson557134a2009-04-01 09:21:53 -04009133#endif /* CONFIG_NFS_V4_1 */
9134
Trond Myklebust36281ca2012-03-04 18:13:56 -05009135static bool nfs4_match_stateid(const nfs4_stateid *s1,
9136 const nfs4_stateid *s2)
9137{
Trond Myklebustf597c532012-03-04 18:13:56 -05009138 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009139}
9140
9141
Trond Myklebust17280172012-03-11 13:11:00 -04009142static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009143 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009144 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009145 .recover_open = nfs4_open_reclaim,
9146 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009147 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009148 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009149};
9150
Andy Adamson591d71c2009-04-01 09:22:47 -04009151#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009152static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009153 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9154 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9155 .recover_open = nfs4_open_reclaim,
9156 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009157 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009158 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009159 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009160};
9161#endif /* CONFIG_NFS_V4_1 */
9162
Trond Myklebust17280172012-03-11 13:11:00 -04009163static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009164 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009165 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009166 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009167 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009168 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009169};
9170
Andy Adamson591d71c2009-04-01 09:22:47 -04009171#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009172static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009173 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9174 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009175 .recover_open = nfs41_open_expired,
9176 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009177 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009178};
9179#endif /* CONFIG_NFS_V4_1 */
9180
Trond Myklebust17280172012-03-11 13:11:00 -04009181static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009182 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009183 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009184 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009185};
9186
9187#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009188static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009189 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009190 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009191 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009192};
9193#endif
9194
Chuck Leverec011fe2013-10-17 14:12:39 -04009195static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009196 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009197 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009198};
9199
9200#if defined(CONFIG_NFS_V4_1)
9201static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009202 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009203 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009204};
9205#endif /* CONFIG_NFS_V4_1 */
9206
Trond Myklebust97dc1352010-06-16 09:52:26 -04009207static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9208 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009209 .init_caps = NFS_CAP_READDIRPLUS
9210 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009211 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009212 .init_client = nfs40_init_client,
9213 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009214 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009215 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009216 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009217 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009218 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009219 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009220 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9221 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9222 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009223 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009224};
9225
9226#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009227static struct nfs_seqid *
9228nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9229{
9230 return NULL;
9231}
9232
Trond Myklebust97dc1352010-06-16 09:52:26 -04009233static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9234 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009235 .init_caps = NFS_CAP_READDIRPLUS
9236 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009237 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009238 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009239 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009240 .init_client = nfs41_init_client,
9241 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009242 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009243 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009244 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009245 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009246 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009247 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009248 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009249 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9250 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9251 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009252 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009253};
9254#endif
9255
Steve Dickson42c2c422013-05-22 12:50:38 -04009256#if defined(CONFIG_NFS_V4_2)
9257static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9258 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009259 .init_caps = NFS_CAP_READDIRPLUS
9260 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009261 | NFS_CAP_POSIX_LOCK
9262 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009263 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009264 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009265 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009266 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009267 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009268 | NFS_CAP_LAYOUTSTATS
9269 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009270 .init_client = nfs41_init_client,
9271 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009272 .match_stateid = nfs41_match_stateid,
9273 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009274 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009275 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009276 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009277 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009278 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009279 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9280 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9281 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009282 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009283};
9284#endif
9285
Trond Myklebust97dc1352010-06-16 09:52:26 -04009286const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9287 [0] = &nfs_v4_0_minor_ops,
9288#if defined(CONFIG_NFS_V4_1)
9289 [1] = &nfs_v4_1_minor_ops,
9290#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009291#if defined(CONFIG_NFS_V4_2)
9292 [2] = &nfs_v4_2_minor_ops,
9293#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009294};
9295
Trond Myklebust13997822016-07-24 17:10:52 -04009296static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009297{
9298 ssize_t error, error2;
9299
9300 error = generic_listxattr(dentry, list, size);
9301 if (error < 0)
9302 return error;
9303 if (list) {
9304 list += error;
9305 size -= error;
9306 }
9307
9308 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9309 if (error2 < 0)
9310 return error2;
9311 return error + error2;
9312}
9313
Trond Myklebust17f26b12013-08-21 15:48:42 -04009314static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009315 .create = nfs_create,
9316 .lookup = nfs_lookup,
9317 .atomic_open = nfs_atomic_open,
9318 .link = nfs_link,
9319 .unlink = nfs_unlink,
9320 .symlink = nfs_symlink,
9321 .mkdir = nfs_mkdir,
9322 .rmdir = nfs_rmdir,
9323 .mknod = nfs_mknod,
9324 .rename = nfs_rename,
9325 .permission = nfs_permission,
9326 .getattr = nfs_getattr,
9327 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009328 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009329};
9330
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009331static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009332 .permission = nfs_permission,
9333 .getattr = nfs_getattr,
9334 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009335 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009336};
9337
David Howells509de812006-08-22 20:06:11 -04009338const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009339 .version = 4, /* protocol version */
9340 .dentry_ops = &nfs4_dentry_operations,
9341 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009342 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009343 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009344 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009345 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009346 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009347 .getattr = nfs4_proc_getattr,
9348 .setattr = nfs4_proc_setattr,
9349 .lookup = nfs4_proc_lookup,
9350 .access = nfs4_proc_access,
9351 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009352 .create = nfs4_proc_create,
9353 .remove = nfs4_proc_remove,
9354 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009355 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009356 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009357 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009358 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009359 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009360 .link = nfs4_proc_link,
9361 .symlink = nfs4_proc_symlink,
9362 .mkdir = nfs4_proc_mkdir,
9363 .rmdir = nfs4_proc_remove,
9364 .readdir = nfs4_proc_readdir,
9365 .mknod = nfs4_proc_mknod,
9366 .statfs = nfs4_proc_statfs,
9367 .fsinfo = nfs4_proc_fsinfo,
9368 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009369 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009370 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009371 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009372 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009373 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009374 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009375 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009376 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009377 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009378 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009379 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009380 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009381 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009382 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009383 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009384 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009385 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009386 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009387 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009388 .create_server = nfs4_create_server,
9389 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009390};
9391
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009392static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009393 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009394 .list = nfs4_xattr_list_nfs4_acl,
9395 .get = nfs4_xattr_get_nfs4_acl,
9396 .set = nfs4_xattr_set_nfs4_acl,
9397};
9398
9399const struct xattr_handler *nfs4_xattr_handlers[] = {
9400 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009401#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9402 &nfs4_xattr_nfs4_label_handler,
9403#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009404 NULL
9405};
9406
Linus Torvalds1da177e2005-04-16 15:20:36 -07009407/*
9408 * Local variables:
9409 * c-basic-offset: 8
9410 * End:
9411 */