blob: dbfc7574fab0e636f0738b4d04e905cfaa850f8f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Trond Myklebust4ce79712005-06-22 17:16:21 +000058#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050060#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050061#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040062#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040063#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040064#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040065#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020076/* file attributes which can be mapped to nfs attributes */
77#define NFS4_VALID_ATTRS (ATTR_MODE \
78 | ATTR_UID \
79 | ATTR_GID \
80 | ATTR_SIZE \
81 | ATTR_ATIME \
82 | ATTR_MTIME \
83 | ATTR_CTIME \
84 | ATTR_ATIME_SET \
85 | ATTR_MTIME_SET)
86
Trond Myklebustcdd4e682006-01-03 09:55:12 +010087struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010088static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080089static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050091static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040092static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
93static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040094static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
95 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110096 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040097 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040098#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040099static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
100 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400101static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
102 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400103#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400104
105#ifdef CONFIG_NFS_V4_SECURITY_LABEL
106static inline struct nfs4_label *
107nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
108 struct iattr *sattr, struct nfs4_label *label)
109{
110 int err;
111
112 if (label == NULL)
113 return NULL;
114
115 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
116 return NULL;
117
David Quigleyaa9c2662013-05-22 12:50:44 -0400118 err = security_dentry_init_security(dentry, sattr->ia_mode,
119 &dentry->d_name, (void **)&label->label, &label->len);
120 if (err == 0)
121 return label;
122
123 return NULL;
124}
125static inline void
126nfs4_label_release_security(struct nfs4_label *label)
127{
128 if (label)
129 security_release_secctx(label->label, label->len);
130}
131static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
132{
133 if (label)
134 return server->attr_bitmask;
135
136 return server->attr_bitmask_nl;
137}
138#else
139static inline struct nfs4_label *
140nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
141 struct iattr *sattr, struct nfs4_label *l)
142{ return NULL; }
143static inline void
144nfs4_label_release_security(struct nfs4_label *label)
145{ return; }
146static inline u32 *
147nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
148{ return server->attr_bitmask; }
149#endif
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500152static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Trond Myklebust52567b02009-10-23 14:46:42 -0400154 if (err >= -1000)
155 return err;
156 switch (err) {
157 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500158 case -NFS4ERR_LAYOUTTRYLATER:
159 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400160 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000161 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400162 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000163 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800164 case -NFS4ERR_BADOWNER:
165 case -NFS4ERR_BADNAME:
166 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400167 case -NFS4ERR_SHARE_DENIED:
168 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400169 case -NFS4ERR_MINOR_VERS_MISMATCH:
170 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400171 case -NFS4ERR_FILE_OPEN:
172 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400173 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700175 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400176 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400178 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181/*
182 * This is our standard bitmap for GETATTR requests.
183 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400184const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 FATTR4_WORD0_TYPE
186 | FATTR4_WORD0_CHANGE
187 | FATTR4_WORD0_SIZE
188 | FATTR4_WORD0_FSID
189 | FATTR4_WORD0_FILEID,
190 FATTR4_WORD1_MODE
191 | FATTR4_WORD1_NUMLINKS
192 | FATTR4_WORD1_OWNER
193 | FATTR4_WORD1_OWNER_GROUP
194 | FATTR4_WORD1_RAWDEV
195 | FATTR4_WORD1_SPACE_USED
196 | FATTR4_WORD1_TIME_ACCESS
197 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400198 | FATTR4_WORD1_TIME_MODIFY
199 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400200#ifdef CONFIG_NFS_V4_SECURITY_LABEL
201 FATTR4_WORD2_SECURITY_LABEL
202#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203};
204
Trond Myklebust1549210f2012-06-05 09:16:47 -0400205static const u32 nfs4_pnfs_open_bitmap[3] = {
206 FATTR4_WORD0_TYPE
207 | FATTR4_WORD0_CHANGE
208 | FATTR4_WORD0_SIZE
209 | FATTR4_WORD0_FSID
210 | FATTR4_WORD0_FILEID,
211 FATTR4_WORD1_MODE
212 | FATTR4_WORD1_NUMLINKS
213 | FATTR4_WORD1_OWNER
214 | FATTR4_WORD1_OWNER_GROUP
215 | FATTR4_WORD1_RAWDEV
216 | FATTR4_WORD1_SPACE_USED
217 | FATTR4_WORD1_TIME_ACCESS
218 | FATTR4_WORD1_TIME_METADATA
219 | FATTR4_WORD1_TIME_MODIFY,
220 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500221#ifdef CONFIG_NFS_V4_SECURITY_LABEL
222 | FATTR4_WORD2_SECURITY_LABEL
223#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400224};
225
Andy Adamsone23008e2012-10-02 21:07:32 -0400226static const u32 nfs4_open_noattr_bitmap[3] = {
227 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400228 | FATTR4_WORD0_FILEID,
229};
230
David Quigleya09df2c2013-05-22 12:50:41 -0400231const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 FATTR4_WORD0_FILES_AVAIL
233 | FATTR4_WORD0_FILES_FREE
234 | FATTR4_WORD0_FILES_TOTAL,
235 FATTR4_WORD1_SPACE_AVAIL
236 | FATTR4_WORD1_SPACE_FREE
237 | FATTR4_WORD1_SPACE_TOTAL
238};
239
David Quigleya09df2c2013-05-22 12:50:41 -0400240const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 FATTR4_WORD0_MAXLINK
242 | FATTR4_WORD0_MAXNAME,
243 0
244};
245
Fred Isamandae100c2011-07-30 20:52:37 -0400246const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 | FATTR4_WORD0_MAXREAD
248 | FATTR4_WORD0_MAXWRITE
249 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700250 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400251 | FATTR4_WORD1_FS_LAYOUT_TYPES,
252 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800253 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254};
255
David Quigleya09df2c2013-05-22 12:50:41 -0400256const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400257 FATTR4_WORD0_TYPE
258 | FATTR4_WORD0_CHANGE
259 | FATTR4_WORD0_SIZE
260 | FATTR4_WORD0_FSID
261 | FATTR4_WORD0_FILEID
262 | FATTR4_WORD0_FS_LOCATIONS,
263 FATTR4_WORD1_MODE
264 | FATTR4_WORD1_NUMLINKS
265 | FATTR4_WORD1_OWNER
266 | FATTR4_WORD1_OWNER_GROUP
267 | FATTR4_WORD1_RAWDEV
268 | FATTR4_WORD1_SPACE_USED
269 | FATTR4_WORD1_TIME_ACCESS
270 | FATTR4_WORD1_TIME_METADATA
271 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400272 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400273};
274
Al Virobc4785c2006-10-19 23:28:51 -0700275static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 struct nfs4_readdir_arg *readdir)
277{
Al Viro0dbb4c62006-10-19 23:28:49 -0700278 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000281 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
283 return;
284 }
285
286 readdir->cookie = 0;
287 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
288 if (cookie == 2)
289 return;
290
291 /*
292 * NFSv4 servers do not return entries for '.' and '..'
293 * Therefore, we fake these entries here. We let '.'
294 * have cookie 0 and '..' have cookie 1. Note that
295 * when talking to the server, we always send cookie 0
296 * instead of 1 or 2.
297 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800298 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 if (cookie == 0) {
301 *p++ = xdr_one; /* next */
302 *p++ = xdr_zero; /* cookie, first word */
303 *p++ = xdr_one; /* cookie, second word */
304 *p++ = xdr_one; /* entry len */
305 memcpy(p, ".\0\0\0", 4); /* entry */
306 p++;
307 *p++ = xdr_one; /* bitmap length */
308 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
309 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000310 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312
313 *p++ = xdr_one; /* next */
314 *p++ = xdr_zero; /* cookie, first word */
315 *p++ = xdr_two; /* cookie, second word */
316 *p++ = xdr_two; /* entry len */
317 memcpy(p, "..\0\0", 4); /* entry */
318 p++;
319 *p++ = xdr_one; /* bitmap length */
320 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
321 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000322 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 readdir->pgbase = (char *)p - (char *)start;
325 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800326 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Trond Myklebust26d36302016-09-22 13:39:05 -0400329static void nfs4_test_and_free_stateid(struct nfs_server *server,
330 nfs4_stateid *stateid,
331 struct rpc_cred *cred)
332{
333 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
334
335 ops->test_and_free_expired(server, stateid, cred);
336}
337
338static void __nfs4_free_revoked_stateid(struct nfs_server *server,
339 nfs4_stateid *stateid,
340 struct rpc_cred *cred)
341{
342 stateid->type = NFS4_REVOKED_STATEID_TYPE;
343 nfs4_test_and_free_stateid(server, stateid, cred);
344}
345
346static void nfs4_free_revoked_stateid(struct nfs_server *server,
347 const nfs4_stateid *stateid,
348 struct rpc_cred *cred)
349{
350 nfs4_stateid tmp;
351
352 nfs4_stateid_copy(&tmp, stateid);
353 __nfs4_free_revoked_stateid(server, &tmp, cred);
354}
355
NeilBrown8478eaa2014-09-18 16:09:27 +1000356static long nfs4_update_delay(long *timeout)
357{
358 long ret;
359 if (!timeout)
360 return NFS4_POLL_RETRY_MAX;
361 if (*timeout <= 0)
362 *timeout = NFS4_POLL_RETRY_MIN;
363 if (*timeout > NFS4_POLL_RETRY_MAX)
364 *timeout = NFS4_POLL_RETRY_MAX;
365 ret = *timeout;
366 *timeout <<= 1;
367 return ret;
368}
369
Trond Myklebust65de8722008-12-23 15:21:44 -0500370static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
371{
372 int res = 0;
373
374 might_sleep();
375
NeilBrown8478eaa2014-09-18 16:09:27 +1000376 freezable_schedule_timeout_killable_unsafe(
377 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500378 if (fatal_signal_pending(current))
379 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500380 return res;
381}
382
383/* This is the error handling routine for processes that are allowed
384 * to sleep.
385 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400386static int nfs4_do_handle_exception(struct nfs_server *server,
387 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500388{
389 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500390 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400391 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500392 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500393 int ret = errorcode;
394
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400395 exception->delay = 0;
396 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500397 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400398
399 if (stateid == NULL && state != NULL)
400 stateid = &state->stateid;
401
Trond Myklebust65de8722008-12-23 15:21:44 -0500402 switch(errorcode) {
403 case 0:
404 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400405 case -NFS4ERR_DELEG_REVOKED:
406 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400407 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400408 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400409 if (inode != NULL && stateid != NULL) {
410 nfs_inode_find_state_and_recover(inode,
411 stateid);
412 goto wait_on_recovery;
413 }
414 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400415 if (inode) {
416 int err;
417
418 err = nfs_async_inode_return_delegation(inode,
419 stateid);
420 if (err == 0)
421 goto wait_on_recovery;
422 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
423 exception->retry = 1;
424 break;
425 }
426 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500427 if (state == NULL)
428 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400429 ret = nfs4_schedule_stateid_recovery(server, state);
430 if (ret < 0)
431 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500432 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500433 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500434 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500435 nfs4_schedule_lease_recovery(clp);
436 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400437 case -NFS4ERR_MOVED:
438 ret = nfs4_schedule_migration_recovery(server);
439 if (ret < 0)
440 break;
441 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400442 case -NFS4ERR_LEASE_MOVED:
443 nfs4_schedule_lease_moved_recovery(clp);
444 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500445#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400446 case -NFS4ERR_BADSESSION:
447 case -NFS4ERR_BADSLOT:
448 case -NFS4ERR_BAD_HIGH_SLOT:
449 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
450 case -NFS4ERR_DEADSESSION:
451 case -NFS4ERR_SEQ_FALSE_RETRY:
452 case -NFS4ERR_SEQ_MISORDERED:
453 dprintk("%s ERROR: %d Reset session\n", __func__,
454 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400455 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400456 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500457#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500458 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500459 if (exception->timeout > HZ) {
460 /* We have retried a decent amount, time to
461 * fail
462 */
463 ret = -EBUSY;
464 break;
465 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500466 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400467 nfs_inc_server_stats(server, NFSIOS_DELAY);
468 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400469 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400470 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400471 exception->delay = 1;
472 return 0;
473
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400474 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500475 case -NFS4ERR_OLD_STATEID:
476 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800477 break;
478 case -NFS4ERR_BADOWNER:
479 /* The following works around a Linux server bug! */
480 case -NFS4ERR_BADNAME:
481 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
482 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
483 exception->retry = 1;
484 printk(KERN_WARNING "NFS: v4 server %s "
485 "does not accept raw "
486 "uid/gids. "
487 "Reenabling the idmapper.\n",
488 server->nfs_client->cl_hostname);
489 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500490 }
491 /* We failed to handle the error */
492 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500493wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400494 exception->recovering = 1;
495 return 0;
496}
497
498/* This is the error handling routine for processes that are allowed
499 * to sleep.
500 */
501int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
502{
503 struct nfs_client *clp = server->nfs_client;
504 int ret;
505
506 ret = nfs4_do_handle_exception(server, errorcode, exception);
507 if (exception->delay) {
508 ret = nfs4_delay(server->client, &exception->timeout);
509 goto out_retry;
510 }
511 if (exception->recovering) {
512 ret = nfs4_wait_clnt_recover(clp);
513 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
514 return -EIO;
515 goto out_retry;
516 }
517 return ret;
518out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500519 if (ret == 0)
520 exception->retry = 1;
521 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500522}
523
Trond Myklebust037fc982015-09-20 15:51:00 -0400524static int
525nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
526 int errorcode, struct nfs4_exception *exception)
527{
528 struct nfs_client *clp = server->nfs_client;
529 int ret;
530
531 ret = nfs4_do_handle_exception(server, errorcode, exception);
532 if (exception->delay) {
533 rpc_delay(task, nfs4_update_delay(&exception->timeout));
534 goto out_retry;
535 }
536 if (exception->recovering) {
537 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
538 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
539 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
540 goto out_retry;
541 }
542 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
543 ret = -EIO;
544 return ret;
545out_retry:
546 if (ret == 0)
547 exception->retry = 1;
548 return ret;
549}
550
551static int
552nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
553 struct nfs4_state *state, long *timeout)
554{
555 struct nfs4_exception exception = {
556 .state = state,
557 };
558
559 if (task->tk_status >= 0)
560 return 0;
561 if (timeout)
562 exception.timeout = *timeout;
563 task->tk_status = nfs4_async_handle_exception(task, server,
564 task->tk_status,
565 &exception);
566 if (exception.delay && timeout)
567 *timeout = exception.timeout;
568 if (exception.retry)
569 return -EAGAIN;
570 return 0;
571}
572
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400573/*
574 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
575 * or 'false' otherwise.
576 */
577static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
578{
579 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500580 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400581}
Trond Myklebust65de8722008-12-23 15:21:44 -0500582
Trond Myklebust452e9352010-07-31 14:29:06 -0400583static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 spin_lock(&clp->cl_lock);
586 if (time_before(clp->cl_last_renewal,timestamp))
587 clp->cl_last_renewal = timestamp;
588 spin_unlock(&clp->cl_lock);
589}
590
Trond Myklebust452e9352010-07-31 14:29:06 -0400591static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
592{
Trond Myklebustbe824162015-07-05 14:50:46 -0400593 struct nfs_client *clp = server->nfs_client;
594
595 if (!nfs4_has_session(clp))
596 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400597}
598
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400599struct nfs4_call_sync_data {
600 const struct nfs_server *seq_server;
601 struct nfs4_sequence_args *seq_args;
602 struct nfs4_sequence_res *seq_res;
603};
604
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800605void nfs4_init_sequence(struct nfs4_sequence_args *args,
606 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400607{
608 args->sa_slot = NULL;
609 args->sa_cache_this = cache_reply;
610 args->sa_privileged = 0;
611
612 res->sr_slot = NULL;
613}
614
615static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
616{
617 args->sa_privileged = 1;
618}
619
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400620static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400621{
622 struct nfs4_slot *slot = res->sr_slot;
623 struct nfs4_slot_table *tbl;
624
Chuck Lever3bd23842013-08-09 12:49:19 -0400625 tbl = slot->table;
626 spin_lock(&tbl->slot_tbl_lock);
627 if (!nfs41_wake_and_assign_slot(tbl, slot))
628 nfs4_free_slot(tbl, slot);
629 spin_unlock(&tbl->slot_tbl_lock);
630
631 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400632}
633
634static int nfs40_sequence_done(struct rpc_task *task,
635 struct nfs4_sequence_res *res)
636{
637 if (res->sr_slot != NULL)
638 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400639 return 1;
640}
641
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400642#if defined(CONFIG_NFS_V4_1)
643
Trond Myklebustd185a332010-06-16 09:52:25 -0400644static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400645{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500646 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400647 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500648 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500649 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400650
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500651 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500652 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500653
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400654 /* Bump the slot sequence number */
655 if (slot->seq_done)
656 slot->seq_nr++;
657 slot->seq_done = 0;
658
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500659 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500660 /* Be nice to the server: try to ensure that the last transmitted
661 * value for highest_user_slotid <= target_highest_slotid
662 */
663 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
664 send_new_highest_used_slotid = true;
665
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500666 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500667 send_new_highest_used_slotid = false;
668 goto out_unlock;
669 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500670 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500671
672 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
673 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500674out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500675 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400676 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500677 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400678 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400679 if (waitqueue_active(&tbl->slot_waitq))
680 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400681}
682
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400683static int nfs41_sequence_process(struct rpc_task *task,
684 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400685{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500686 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500687 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400688 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500689 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500690 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400691
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500692 if (slot == NULL)
693 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400694 /* don't increment the sequence number if the task wasn't sent */
695 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400696 goto out;
697
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500698 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500699
Trond Myklebustac20d162012-12-15 15:36:07 -0500700 if (slot->interrupted) {
701 slot->interrupted = 0;
702 interrupted = true;
703 }
704
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400705 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500706 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400707 switch (res->sr_status) {
708 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400709 /* If previous op on slot was interrupted and we reused
710 * the seq# and got a reply from the cache, then retry
711 */
712 if (task->tk_status == -EREMOTEIO && interrupted) {
713 ++slot->seq_nr;
714 goto retry_nowait;
715 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400716 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400717 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500718 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500719 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500720 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400721 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
722 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500723 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400724 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500725 case 1:
726 /*
727 * sr_status remains 1 if an RPC level error occurred.
728 * The server may or may not have processed the sequence
729 * operation..
730 * Mark the slot as having hosted an interrupted RPC call.
731 */
732 slot->interrupted = 1;
733 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400734 case -NFS4ERR_DELAY:
735 /* The server detected a resend of the RPC call and
736 * returned NFS4ERR_DELAY as per Section 2.10.6.2
737 * of RFC5661.
738 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500739 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400740 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500741 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500742 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400743 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500744 case -NFS4ERR_BADSLOT:
745 /*
746 * The slot id we used was probably retired. Try again
747 * using a different slot id.
748 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500749 goto retry_nowait;
750 case -NFS4ERR_SEQ_MISORDERED:
751 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500752 * Was the last operation on this sequence interrupted?
753 * If so, retry after bumping the sequence number.
754 */
755 if (interrupted) {
756 ++slot->seq_nr;
757 goto retry_nowait;
758 }
759 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500760 * Could this slot have been previously retired?
761 * If so, then the server may be expecting seq_nr = 1!
762 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500763 if (slot->seq_nr != 1) {
764 slot->seq_nr = 1;
765 goto retry_nowait;
766 }
767 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500768 case -NFS4ERR_SEQ_FALSE_RETRY:
769 ++slot->seq_nr;
770 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400771 default:
772 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400773 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400774 }
775out:
776 /* The session may be reset by one of the error handlers. */
777 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500778out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500779 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500780retry_nowait:
781 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400782 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500783 task->tk_status = 0;
784 ret = 0;
785 }
786 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400787out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400788 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400789 goto out;
790 rpc_delay(task, NFS4_POLL_RETRY_MAX);
791 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400792}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400793
794int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
795{
796 if (!nfs41_sequence_process(task, res))
797 return 0;
798 if (res->sr_slot != NULL)
799 nfs41_sequence_free_slot(res);
800 return 1;
801
802}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500803EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400804
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400805static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
806{
807 if (res->sr_slot == NULL)
808 return 1;
809 if (res->sr_slot->table->session != NULL)
810 return nfs41_sequence_process(task, res);
811 return nfs40_sequence_done(task, res);
812}
813
814static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
815{
816 if (res->sr_slot != NULL) {
817 if (res->sr_slot->table->session != NULL)
818 nfs41_sequence_free_slot(res);
819 else
820 nfs40_sequence_free_slot(res);
821 }
822}
823
Peng Tao2c4b1312014-06-11 05:24:16 +0800824int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400825{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500826 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400827 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400828 if (!res->sr_slot->table->session)
829 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400830 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400831}
Peng Tao2c4b1312014-06-11 05:24:16 +0800832EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400833
Andy Adamsonce5039c2009-04-01 09:22:13 -0400834static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
835{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400836 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400837
Trond Myklebust035168a2010-06-16 09:52:26 -0400838 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
839
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500840 nfs4_setup_sequence(data->seq_server->nfs_client,
841 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400842}
843
Andy Adamson69ab40c2009-04-01 09:22:19 -0400844static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
845{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400846 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400847
Trond Myklebust14516c32010-07-31 14:29:06 -0400848 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400849}
850
Trond Myklebust17280172012-03-11 13:11:00 -0400851static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400852 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400853 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400854};
855
Chuck Lever3bd23842013-08-09 12:49:19 -0400856#else /* !CONFIG_NFS_V4_1 */
857
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400858static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
859{
860 return nfs40_sequence_done(task, res);
861}
862
863static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
864{
865 if (res->sr_slot != NULL)
866 nfs40_sequence_free_slot(res);
867}
868
Peng Tao2c4b1312014-06-11 05:24:16 +0800869int nfs4_sequence_done(struct rpc_task *task,
870 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400871{
Chuck Lever3bd23842013-08-09 12:49:19 -0400872 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400873}
Peng Tao2c4b1312014-06-11 05:24:16 +0800874EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400875
876#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400877
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500878int nfs4_setup_sequence(const struct nfs_client *client,
879 struct nfs4_sequence_args *args,
880 struct nfs4_sequence_res *res,
881 struct rpc_task *task)
882{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500883 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500884 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500885 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500886
Anna Schumaker9dd91072017-01-10 12:01:46 -0500887 /* slot already allocated? */
888 if (res->sr_slot != NULL)
889 goto out_start;
890
Anna Schumaker76ee0352017-01-10 16:49:31 -0500891 if (session) {
892 tbl = &session->fc_slot_table;
893 task->tk_timeout = 0;
894 }
895
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500896 spin_lock(&tbl->slot_tbl_lock);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500897 /* The state manager will wait until the slot table is empty */
898 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
899 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500900
Anna Schumaker3d358082017-01-11 10:54:04 -0500901 slot = nfs4_alloc_slot(tbl);
902 if (IS_ERR(slot)) {
903 /* Try again in 1/4 second */
904 if (slot == ERR_PTR(-ENOMEM))
905 task->tk_timeout = HZ >> 2;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500906 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -0500907 }
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500908 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500909
Anna Schumaker3d358082017-01-11 10:54:04 -0500910 slot->privileged = args->sa_privileged ? 1 : 0;
911 args->sa_slot = slot;
912
913 res->sr_slot = slot;
914 if (session) {
915 res->sr_timestamp = jiffies;
916 res->sr_status_flags = 0;
917 res->sr_status = 1;
Anna Schumaker3d358082017-01-11 10:54:04 -0500918 }
919
Anna Schumakerad05cc02017-01-11 13:37:06 -0500920 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd91072017-01-10 12:01:46 -0500921out_start:
922 rpc_call_start(task);
923 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500924
925out_sleep:
926 if (args->sa_privileged)
927 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
928 NULL, RPC_PRIORITY_PRIVILEGED);
929 else
930 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
931 spin_unlock(&tbl->slot_tbl_lock);
932 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500933}
934EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
935
Chuck Lever9915ea72013-08-09 12:48:27 -0400936static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
937{
938 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500939 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -0400940 data->seq_args, data->seq_res, task);
941}
942
943static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
944{
945 struct nfs4_call_sync_data *data = calldata;
946 nfs4_sequence_done(task, data->seq_res);
947}
948
949static const struct rpc_call_ops nfs40_call_sync_ops = {
950 .rpc_call_prepare = nfs40_call_sync_prepare,
951 .rpc_call_done = nfs40_call_sync_done,
952};
953
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400954static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400955 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500956 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100957 struct nfs4_sequence_args *args,
958 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400959{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100960 int ret;
961 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400962 struct nfs_client *clp = server->nfs_client;
963 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100964 .seq_server = server,
965 .seq_args = args,
966 .seq_res = res,
967 };
968 struct rpc_task_setup task_setup = {
969 .rpc_client = clnt,
970 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400971 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100972 .callback_data = &data
973 };
974
975 task = rpc_run_task(&task_setup);
976 if (IS_ERR(task))
977 ret = PTR_ERR(task);
978 else {
979 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400980 rpc_put_task(task);
981 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100982 return ret;
983}
984
Bryan Schumaker7c513052011-03-24 17:12:24 +0000985int nfs4_call_sync(struct rpc_clnt *clnt,
986 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000987 struct rpc_message *msg,
988 struct nfs4_sequence_args *args,
989 struct nfs4_sequence_res *res,
990 int cache_reply)
991{
Chuck Levera9c92d62013-08-09 12:48:18 -0400992 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400993 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000994}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Trond Myklebustd3129ef2017-01-11 22:07:28 -0500996static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
997 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 struct nfs_inode *nfsi = NFS_I(dir);
1000
1001 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001002 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001003 if (cinfo->atomic && cinfo->before == dir->i_version) {
1004 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1005 nfsi->attrtimeo_timestamp = jiffies;
1006 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 nfs_force_lookup_revalidate(dir);
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001008 if (cinfo->before != dir->i_version)
1009 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1010 NFS_INO_INVALID_ACL;
1011 }
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001012 dir->i_version = cinfo->after;
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001013 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001014 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001015 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 spin_unlock(&dir->i_lock);
1017}
1018
1019struct nfs4_opendata {
1020 struct kref kref;
1021 struct nfs_openargs o_arg;
1022 struct nfs_openres o_res;
1023 struct nfs_open_confirmargs c_arg;
1024 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001025 struct nfs4_string owner_name;
1026 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001027 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001029 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001031 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 struct nfs4_state_owner *owner;
1033 struct nfs4_state *state;
1034 struct iattr attrs;
1035 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001036 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001037 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001038 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001039 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 int cancelled;
1041};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001042
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001043static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1044 int err, struct nfs4_exception *exception)
1045{
1046 if (err != -EINVAL)
1047 return false;
1048 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1049 return false;
1050 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1051 exception->retry = 1;
1052 return true;
1053}
1054
Trond Myklebust6ae37332015-01-30 14:21:14 -05001055static u32
1056nfs4_map_atomic_open_share(struct nfs_server *server,
1057 fmode_t fmode, int openflags)
1058{
1059 u32 res = 0;
1060
1061 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1062 case FMODE_READ:
1063 res = NFS4_SHARE_ACCESS_READ;
1064 break;
1065 case FMODE_WRITE:
1066 res = NFS4_SHARE_ACCESS_WRITE;
1067 break;
1068 case FMODE_READ|FMODE_WRITE:
1069 res = NFS4_SHARE_ACCESS_BOTH;
1070 }
1071 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1072 goto out;
1073 /* Want no delegation if we're using O_DIRECT */
1074 if (openflags & O_DIRECT)
1075 res |= NFS4_SHARE_WANT_NO_DELEG;
1076out:
1077 return res;
1078}
1079
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001080static enum open_claim_type4
1081nfs4_map_atomic_open_claim(struct nfs_server *server,
1082 enum open_claim_type4 claim)
1083{
1084 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1085 return claim;
1086 switch (claim) {
1087 default:
1088 return claim;
1089 case NFS4_OPEN_CLAIM_FH:
1090 return NFS4_OPEN_CLAIM_NULL;
1091 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1092 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1093 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1094 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1095 }
1096}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001099{
1100 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001101 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001102 p->o_res.seqid = p->o_arg.seqid;
1103 p->c_res.seqid = p->c_arg.seqid;
1104 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001105 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001106 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001107 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001108}
1109
Al Viro82a2c1b2011-06-22 18:30:55 -04001110static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001111 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001112 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001113 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001114 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001115 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001116{
Al Viro82a2c1b2011-06-22 18:30:55 -04001117 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001118 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001119 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001120 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001121 struct nfs4_opendata *p;
1122
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001123 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001124 if (p == NULL)
1125 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001126
1127 p->f_label = nfs4_label_alloc(server, gfp_mask);
1128 if (IS_ERR(p->f_label))
1129 goto err_free_p;
1130
Kinglong Meea49c2692015-07-27 15:31:38 +08001131 p->a_label = nfs4_label_alloc(server, gfp_mask);
1132 if (IS_ERR(p->a_label))
1133 goto err_free_f;
1134
Trond Myklebust63f5f792015-01-23 19:19:25 -05001135 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1136 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001137 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001138 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001139 nfs_sb_active(dentry->d_sb);
1140 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001141 p->dir = parent;
1142 p->owner = sp;
1143 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001144 p->o_arg.open_flags = flags;
1145 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001146 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001147 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001148 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1149 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001150 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1151 * will return permission denied for all bits until close */
1152 if (!(flags & O_EXCL)) {
1153 /* ask server to check for all possible rights as results
1154 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001155 switch (p->o_arg.claim) {
1156 default:
1157 break;
1158 case NFS4_OPEN_CLAIM_NULL:
1159 case NFS4_OPEN_CLAIM_FH:
1160 p->o_arg.access = NFS4_ACCESS_READ |
1161 NFS4_ACCESS_MODIFY |
1162 NFS4_ACCESS_EXTEND |
1163 NFS4_ACCESS_EXECUTE;
1164 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001165 }
David Howells7539bba2006-08-22 20:06:09 -04001166 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001167 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1168 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001169 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001170 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001171 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001172 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001173 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001174 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001175 case NFS4_OPEN_CLAIM_NULL:
1176 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1177 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1178 p->o_arg.fh = NFS_FH(dir);
1179 break;
1180 case NFS4_OPEN_CLAIM_PREVIOUS:
1181 case NFS4_OPEN_CLAIM_FH:
1182 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1183 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001184 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001185 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001186 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001187 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001188
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001189 p->o_arg.u.attrs = &p->attrs;
1190 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001191
1192 verf[0] = jiffies;
1193 verf[1] = current->pid;
1194 memcpy(p->o_arg.u.verifier.data, verf,
1195 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001196 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001197 p->c_arg.fh = &p->o_res.fh;
1198 p->c_arg.stateid = &p->o_res.stateid;
1199 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001200 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001201 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001202 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001203
1204err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001205 nfs4_label_free(p->a_label);
1206err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001207 nfs4_label_free(p->f_label);
1208err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001209 kfree(p);
1210err:
1211 dput(parent);
1212 return NULL;
1213}
1214
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001215static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001216{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001217 struct nfs4_opendata *p = container_of(kref,
1218 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001219 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001220
1221 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001222 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001223 if (p->state != NULL)
1224 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001225 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001226
Kinglong Meea49c2692015-07-27 15:31:38 +08001227 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001228 nfs4_label_free(p->f_label);
1229
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001230 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001231 dput(p->dentry);
1232 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001233 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001234 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001235 kfree(p);
1236}
1237
1238static void nfs4_opendata_put(struct nfs4_opendata *p)
1239{
1240 if (p != NULL)
1241 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001242}
1243
Trond Myklebust24311f82015-09-20 10:50:17 -04001244static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1245 fmode_t fmode)
1246{
1247 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1248 case FMODE_READ|FMODE_WRITE:
1249 return state->n_rdwr != 0;
1250 case FMODE_WRITE:
1251 return state->n_wronly != 0;
1252 case FMODE_READ:
1253 return state->n_rdonly != 0;
1254 }
1255 WARN_ON_ONCE(1);
1256 return false;
1257}
1258
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001259static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001260{
1261 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001262
Trond Myklebust536e43d2012-01-17 22:04:26 -05001263 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001264 goto out;
1265 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001266 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001267 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1268 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001269 break;
1270 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001271 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1272 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001273 break;
1274 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001275 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1276 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001277 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001278out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001279 return ret;
1280}
1281
Trond Myklebust2a606182015-08-19 22:30:00 -05001282static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1283 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001284{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001285 if (delegation == NULL)
1286 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001287 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001288 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001289 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1290 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001291 switch (claim) {
1292 case NFS4_OPEN_CLAIM_NULL:
1293 case NFS4_OPEN_CLAIM_FH:
1294 break;
1295 case NFS4_OPEN_CLAIM_PREVIOUS:
1296 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1297 break;
1298 default:
1299 return 0;
1300 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001301 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001302 return 1;
1303}
1304
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001305static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001306{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001307 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001308 case FMODE_WRITE:
1309 state->n_wronly++;
1310 break;
1311 case FMODE_READ:
1312 state->n_rdonly++;
1313 break;
1314 case FMODE_READ|FMODE_WRITE:
1315 state->n_rdwr++;
1316 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001317 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001318}
1319
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001320#ifdef CONFIG_NFS_V4_1
1321static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1322{
1323 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1324 return true;
1325 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1326 return true;
1327 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1328 return true;
1329 return false;
1330}
1331#endif /* CONFIG_NFS_V4_1 */
1332
Trond Myklebust4f14c192014-02-12 19:15:06 -05001333static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001334{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001335 struct nfs_client *clp = state->owner->so_server->nfs_client;
1336 bool need_recover = false;
1337
1338 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1339 need_recover = true;
1340 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1341 need_recover = true;
1342 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1343 need_recover = true;
1344 if (need_recover)
1345 nfs4_state_mark_reclaim_nograce(clp, state);
1346}
1347
Trond Myklebuste999e802014-02-10 18:20:47 -05001348static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001349 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001350{
1351 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1352 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001353 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001354 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001355 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001356 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001357 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001358 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1359 return true;
1360 return false;
1361}
1362
Trond Myklebustf95549c2015-01-23 18:06:09 -05001363static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1364{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001365 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1366 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001367 if (state->n_wronly)
1368 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1369 if (state->n_rdonly)
1370 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1371 if (state->n_rdwr)
1372 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001373 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001374}
1375
Trond Myklebust226056c2014-02-11 10:41:07 -05001376static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1377 nfs4_stateid *stateid, fmode_t fmode)
1378{
1379 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1380 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1381 case FMODE_WRITE:
1382 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1383 break;
1384 case FMODE_READ:
1385 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1386 break;
1387 case 0:
1388 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1389 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1390 clear_bit(NFS_OPEN_STATE, &state->flags);
1391 }
1392 if (stateid == NULL)
1393 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001394 /* Handle OPEN+OPEN_DOWNGRADE races */
1395 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1396 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001397 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001398 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001399 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001400 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001401 nfs4_stateid_copy(&state->stateid, stateid);
1402 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001403}
1404
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001405static void nfs_clear_open_stateid(struct nfs4_state *state,
1406 nfs4_stateid *arg_stateid,
1407 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001408{
1409 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001410 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1411 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1412 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001413 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001414 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1415 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001416}
1417
Trond Myklebust1393d962016-09-22 13:39:13 -04001418static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1419 const nfs4_stateid *stateid, fmode_t fmode,
1420 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001421{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001422 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001423 case FMODE_READ:
1424 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1425 break;
1426 case FMODE_WRITE:
1427 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1428 break;
1429 case FMODE_READ|FMODE_WRITE:
1430 set_bit(NFS_O_RDWR_STATE, &state->flags);
1431 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001432 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001433 return;
1434 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1435 nfs4_stateid_copy(&state->stateid, stateid);
1436 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001437}
1438
Trond Myklebust1393d962016-09-22 13:39:13 -04001439static void __update_open_stateid(struct nfs4_state *state,
1440 const nfs4_stateid *open_stateid,
1441 const nfs4_stateid *deleg_stateid,
1442 fmode_t fmode,
1443 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001445 /*
1446 * Protect the call to nfs4_state_set_mode_locked and
1447 * serialise the stateid update
1448 */
Andrew Elble361cad32015-12-02 09:20:57 -05001449 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001450 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001451 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001452 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001453 set_bit(NFS_DELEGATED_STATE, &state->flags);
1454 }
1455 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001456 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001457 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001458 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001459 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
Trond Myklebust1393d962016-09-22 13:39:13 -04001462static int update_open_stateid(struct nfs4_state *state,
1463 const nfs4_stateid *open_stateid,
1464 const nfs4_stateid *delegation,
1465 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001466{
Trond Myklebust1393d962016-09-22 13:39:13 -04001467 struct nfs_server *server = NFS_SERVER(state->inode);
1468 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001469 struct nfs_inode *nfsi = NFS_I(state->inode);
1470 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001471 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001472 int ret = 0;
1473
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001474 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001475
1476 rcu_read_lock();
1477 deleg_cur = rcu_dereference(nfsi->delegation);
1478 if (deleg_cur == NULL)
1479 goto no_delegation;
1480
1481 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001482 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001483 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001484 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001485 goto no_delegation_unlock;
1486
1487 if (delegation == NULL)
1488 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001489 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001490 goto no_delegation_unlock;
1491
Trond Myklebustb7391f42008-12-23 15:21:52 -05001492 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001493 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1494 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001495 ret = 1;
1496no_delegation_unlock:
1497 spin_unlock(&deleg_cur->lock);
1498no_delegation:
1499 rcu_read_unlock();
1500
1501 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001502 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001503 ret = 1;
1504 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001505 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001506 nfs4_schedule_state_manager(clp);
1507 if (freeme.type != 0)
1508 nfs4_test_and_free_stateid(server, &freeme,
1509 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001510
1511 return ret;
1512}
1513
Trond Myklebust39071e62015-01-24 15:07:56 -05001514static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1515 const nfs4_stateid *stateid)
1516{
1517 struct nfs4_state *state = lsp->ls_state;
1518 bool ret = false;
1519
1520 spin_lock(&state->state_lock);
1521 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1522 goto out_noupdate;
1523 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1524 goto out_noupdate;
1525 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1526 ret = true;
1527out_noupdate:
1528 spin_unlock(&state->state_lock);
1529 return ret;
1530}
Trond Myklebust34310432008-12-23 15:21:38 -05001531
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001532static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001533{
1534 struct nfs_delegation *delegation;
1535
1536 rcu_read_lock();
1537 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001538 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001539 rcu_read_unlock();
1540 return;
1541 }
1542 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001543 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001544}
1545
Trond Myklebust6ee41262007-07-08 14:11:36 -04001546static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001547{
1548 struct nfs4_state *state = opendata->state;
1549 struct nfs_inode *nfsi = NFS_I(state->inode);
1550 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001551 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001552 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001553 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001554 nfs4_stateid stateid;
1555 int ret = -EAGAIN;
1556
Trond Myklebustaac00a82007-07-05 19:02:21 -04001557 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001558 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001559 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001560 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001561 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001562 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001563 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001564 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001565 rcu_read_lock();
1566 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001567 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001568 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001569 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001570 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001571 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001572 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001573 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001574 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001575 if (!opendata->is_recover) {
1576 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1577 if (ret != 0)
1578 goto out;
1579 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001580 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001581
1582 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001583 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001584 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001585 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001586out:
1587 return ERR_PTR(ret);
1588out_return_state:
1589 atomic_inc(&state->count);
1590 return state;
1591}
1592
Andy Adamsone23008e2012-10-02 21:07:32 -04001593static void
1594nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1595{
1596 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1597 struct nfs_delegation *delegation;
1598 int delegation_flags = 0;
1599
1600 rcu_read_lock();
1601 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1602 if (delegation)
1603 delegation_flags = delegation->flags;
1604 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001605 switch (data->o_arg.claim) {
1606 default:
1607 break;
1608 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1609 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001610 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1611 "returning a delegation for "
1612 "OPEN(CLAIM_DELEGATE_CUR)\n",
1613 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001614 return;
1615 }
1616 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001617 nfs_inode_set_delegation(state->inode,
1618 data->owner->so_cred,
1619 &data->o_res);
1620 else
1621 nfs_inode_reclaim_delegation(state->inode,
1622 data->owner->so_cred,
1623 &data->o_res);
1624}
1625
1626/*
1627 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1628 * and update the nfs4_state.
1629 */
1630static struct nfs4_state *
1631_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1632{
1633 struct inode *inode = data->state->inode;
1634 struct nfs4_state *state = data->state;
1635 int ret;
1636
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001637 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001638 if (data->rpc_status)
1639 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001640 /* cached opens have already been processed */
1641 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001642 }
1643
Andy Adamsone23008e2012-10-02 21:07:32 -04001644 ret = nfs_refresh_inode(inode, &data->f_attr);
1645 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001646 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001647
1648 if (data->o_res.delegation_type != 0)
1649 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001650update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001651 update_open_stateid(state, &data->o_res.stateid, NULL,
1652 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001653 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001654
1655 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001656}
1657
1658static struct nfs4_state *
1659_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001660{
1661 struct inode *inode;
1662 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001663 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001664
Trond Myklebustaac00a82007-07-05 19:02:21 -04001665 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001666 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001667 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001668 goto out;
1669 }
1670
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001671 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001672 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001673 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001674 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001675 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001676 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001677 goto err;
1678 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001679 state = nfs4_get_open_state(inode, data->owner);
1680 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001681 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001682 if (data->o_res.delegation_type != 0)
1683 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001684 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001685 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001686 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001687out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001688 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001689 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001690err_put_inode:
1691 iput(inode);
1692err:
1693 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001694}
1695
Andy Adamsone23008e2012-10-02 21:07:32 -04001696static struct nfs4_state *
1697nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1698{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001699 struct nfs4_state *ret;
1700
Andy Adamsone23008e2012-10-02 21:07:32 -04001701 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001702 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1703 else
1704 ret = _nfs4_opendata_to_nfs4_state(data);
1705 nfs4_sequence_free_slot(&data->o_res.seq_res);
1706 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001707}
1708
Trond Myklebust864472e2006-01-03 09:55:15 +01001709static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1710{
1711 struct nfs_inode *nfsi = NFS_I(state->inode);
1712 struct nfs_open_context *ctx;
1713
1714 spin_lock(&state->inode->i_lock);
1715 list_for_each_entry(ctx, &nfsi->open_files, list) {
1716 if (ctx->state != state)
1717 continue;
1718 get_nfs_open_context(ctx);
1719 spin_unlock(&state->inode->i_lock);
1720 return ctx;
1721 }
1722 spin_unlock(&state->inode->i_lock);
1723 return ERR_PTR(-ENOENT);
1724}
1725
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001726static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1727 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001728{
1729 struct nfs4_opendata *opendata;
1730
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001731 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001732 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001733 if (opendata == NULL)
1734 return ERR_PTR(-ENOMEM);
1735 opendata->state = state;
1736 atomic_inc(&state->count);
1737 return opendata;
1738}
1739
Trond Myklebust24311f82015-09-20 10:50:17 -04001740static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1741 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001742{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001743 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001744 int ret;
1745
Trond Myklebust24311f82015-09-20 10:50:17 -04001746 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001747 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001748 opendata->o_arg.open_flags = 0;
1749 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001750 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1751 NFS_SB(opendata->dentry->d_sb),
1752 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001753 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1754 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1755 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001756 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001757 if (ret != 0)
1758 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001759 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001760 if (IS_ERR(newstate))
1761 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001762 if (newstate != opendata->state)
1763 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001764 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001765 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001766}
1767
1768static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1769{
Trond Myklebust864472e2006-01-03 09:55:15 +01001770 int ret;
1771
Trond Myklebust4f14c192014-02-12 19:15:06 -05001772 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1773 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1774 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1775 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001776 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001777 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001778 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001779 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001780 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1781 if (ret != 0)
1782 return ret;
1783 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1784 if (ret != 0)
1785 return ret;
1786 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1787 if (ret != 0)
1788 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001789 /*
1790 * We may have performed cached opens for all three recoveries.
1791 * Check if we need to update the current stateid.
1792 */
1793 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001794 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001795 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001796 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001797 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001798 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001799 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001800 return 0;
1801}
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803/*
1804 * OPEN_RECLAIM:
1805 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001807static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001809 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001810 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001811 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 int status;
1813
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001814 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1815 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001816 if (IS_ERR(opendata))
1817 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001818 rcu_read_lock();
1819 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001820 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001821 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001822 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001823 opendata->o_arg.u.delegation_type = delegation_type;
1824 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001825 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return status;
1827}
1828
Trond Myklebust539cd032007-06-05 11:46:42 -04001829static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
1831 struct nfs_server *server = NFS_SERVER(state->inode);
1832 struct nfs4_exception exception = { };
1833 int err;
1834 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001835 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001836 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001837 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1838 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001839 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001840 break;
1841 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 } while (exception.retry);
1843 return err;
1844}
1845
Trond Myklebust864472e2006-01-03 09:55:15 +01001846static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1847{
1848 struct nfs_open_context *ctx;
1849 int ret;
1850
1851 ctx = nfs4_state_find_open_context(state);
1852 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001853 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001854 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001855 put_nfs_open_context(ctx);
1856 return ret;
1857}
1858
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001859static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001861 switch (err) {
1862 default:
1863 printk(KERN_ERR "NFS: %s: unhandled error "
1864 "%d.\n", __func__, err);
1865 case 0:
1866 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001867 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001868 case -ESTALE:
1869 break;
1870 case -NFS4ERR_BADSESSION:
1871 case -NFS4ERR_BADSLOT:
1872 case -NFS4ERR_BAD_HIGH_SLOT:
1873 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1874 case -NFS4ERR_DEADSESSION:
1875 set_bit(NFS_DELEGATED_STATE, &state->flags);
1876 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1877 return -EAGAIN;
1878 case -NFS4ERR_STALE_CLIENTID:
1879 case -NFS4ERR_STALE_STATEID:
1880 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001881 /* Don't recall a delegation if it was lost */
1882 nfs4_schedule_lease_recovery(server->nfs_client);
1883 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001884 case -NFS4ERR_MOVED:
1885 nfs4_schedule_migration_recovery(server);
1886 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001887 case -NFS4ERR_LEASE_MOVED:
1888 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1889 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001890 case -NFS4ERR_DELEG_REVOKED:
1891 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001892 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001893 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001894 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001895 nfs_inode_find_state_and_recover(state->inode,
1896 stateid);
1897 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001898 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001899 case -NFS4ERR_DELAY:
1900 case -NFS4ERR_GRACE:
1901 set_bit(NFS_DELEGATED_STATE, &state->flags);
1902 ssleep(1);
1903 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001904 case -ENOMEM:
1905 case -NFS4ERR_DENIED:
1906 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1907 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 return err;
1910}
1911
Trond Myklebust24311f82015-09-20 10:50:17 -04001912int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1913 struct nfs4_state *state, const nfs4_stateid *stateid,
1914 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001915{
1916 struct nfs_server *server = NFS_SERVER(state->inode);
1917 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001918 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001919
1920 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1921 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1922 if (IS_ERR(opendata))
1923 return PTR_ERR(opendata);
1924 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001925 write_seqlock(&state->seqlock);
1926 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1927 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001928 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1929 switch (type & (FMODE_READ|FMODE_WRITE)) {
1930 case FMODE_READ|FMODE_WRITE:
1931 case FMODE_WRITE:
1932 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1933 if (err)
1934 break;
1935 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1936 if (err)
1937 break;
1938 case FMODE_READ:
1939 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1940 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001941 nfs4_opendata_put(opendata);
1942 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1943}
1944
Chuck Leverbe05c862013-08-09 12:49:47 -04001945static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1946{
1947 struct nfs4_opendata *data = calldata;
1948
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001949 nfs4_setup_sequence(data->o_arg.server->nfs_client,
1950 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001951}
1952
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001953static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1954{
1955 struct nfs4_opendata *data = calldata;
1956
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001957 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001958
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001959 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001960 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001961 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001962 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001963 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001964 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001965 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001966}
1967
1968static void nfs4_open_confirm_release(void *calldata)
1969{
1970 struct nfs4_opendata *data = calldata;
1971 struct nfs4_state *state = NULL;
1972
1973 /* If this request hasn't been cancelled, do nothing */
1974 if (data->cancelled == 0)
1975 goto out_free;
1976 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001977 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001978 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001979 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001980 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001981 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001982out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001983 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001984}
1985
1986static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001987 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001988 .rpc_call_done = nfs4_open_confirm_done,
1989 .rpc_release = nfs4_open_confirm_release,
1990};
1991
1992/*
1993 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1994 */
1995static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1996{
David Howells2b0143b2015-03-17 22:25:59 +00001997 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001998 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001999 struct rpc_message msg = {
2000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2001 .rpc_argp = &data->c_arg,
2002 .rpc_resp = &data->c_res,
2003 .rpc_cred = data->owner->so_cred,
2004 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002005 struct rpc_task_setup task_setup_data = {
2006 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002007 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002008 .callback_ops = &nfs4_open_confirm_ops,
2009 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002010 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002011 .flags = RPC_TASK_ASYNC,
2012 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 int status;
2014
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002015 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002016 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002017 data->rpc_done = 0;
2018 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002019 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002020 if (data->is_recover)
2021 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002022 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002023 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002024 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002025 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002026 if (status != 0) {
2027 data->cancelled = 1;
2028 smp_wmb();
2029 } else
2030 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002031 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 return status;
2033}
2034
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002035static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002037 struct nfs4_opendata *data = calldata;
2038 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002039 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002040 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002041
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002042 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002043 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002044 /*
2045 * Check if we still need to send an OPEN call, or if we can use
2046 * a delegation instead.
2047 */
2048 if (data->state != NULL) {
2049 struct nfs_delegation *delegation;
2050
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002051 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002052 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002053 rcu_read_lock();
2054 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002055 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002056 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002057 rcu_read_unlock();
2058 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002059 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002060 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002061 switch (claim) {
2062 default:
2063 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002064 case NFS4_OPEN_CLAIM_PREVIOUS:
2065 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2066 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002067 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002068 case NFS4_OPEN_CLAIM_FH:
2069 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002070 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2071 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002072 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002073 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002074 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002075 &data->o_res.seq_res,
2076 task) != 0)
2077 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002078
2079 /* Set the create mode (note dependency on the session type) */
2080 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2081 if (data->o_arg.open_flags & O_EXCL) {
2082 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2083 if (nfs4_has_persistent_session(clp))
2084 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2085 else if (clp->cl_mvops->minor_version > 0)
2086 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2087 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002088 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002089unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002090 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002091 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002092out_no_action:
2093 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002094out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002095 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002096}
2097
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002098static void nfs4_open_done(struct rpc_task *task, void *calldata)
2099{
2100 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002102 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002103
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002104 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002105 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002106
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002107 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002108 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2109 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002110 case S_IFREG:
2111 break;
2112 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002113 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002114 break;
2115 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002116 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002117 break;
2118 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002119 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002120 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002121 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002122 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002123 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2124 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002125 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002126 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002127}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002128
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002129static void nfs4_open_release(void *calldata)
2130{
2131 struct nfs4_opendata *data = calldata;
2132 struct nfs4_state *state = NULL;
2133
2134 /* If this request hasn't been cancelled, do nothing */
2135 if (data->cancelled == 0)
2136 goto out_free;
2137 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002138 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002139 goto out_free;
2140 /* In case we need an open_confirm, no cleanup! */
2141 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2142 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002143 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002144 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002145 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002146out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002147 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002148}
2149
2150static const struct rpc_call_ops nfs4_open_ops = {
2151 .rpc_call_prepare = nfs4_open_prepare,
2152 .rpc_call_done = nfs4_open_done,
2153 .rpc_release = nfs4_open_release,
2154};
2155
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002156static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002157{
David Howells2b0143b2015-03-17 22:25:59 +00002158 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002159 struct nfs_server *server = NFS_SERVER(dir);
2160 struct nfs_openargs *o_arg = &data->o_arg;
2161 struct nfs_openres *o_res = &data->o_res;
2162 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002163 struct rpc_message msg = {
2164 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2165 .rpc_argp = o_arg,
2166 .rpc_resp = o_res,
2167 .rpc_cred = data->owner->so_cred,
2168 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002169 struct rpc_task_setup task_setup_data = {
2170 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002171 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002172 .callback_ops = &nfs4_open_ops,
2173 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002174 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002175 .flags = RPC_TASK_ASYNC,
2176 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002177 int status;
2178
Chuck Levera9c92d62013-08-09 12:48:18 -04002179 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002180 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002181 data->rpc_done = 0;
2182 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002183 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002184 data->is_recover = 0;
2185 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002186 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002187 data->is_recover = 1;
2188 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002189 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002190 if (IS_ERR(task))
2191 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002192 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002193 if (status != 0) {
2194 data->cancelled = 1;
2195 smp_wmb();
2196 } else
2197 status = data->rpc_status;
2198 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002199
2200 return status;
2201}
2202
2203static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2204{
David Howells2b0143b2015-03-17 22:25:59 +00002205 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002206 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002207 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002208
2209 status = nfs4_run_open_task(data, 1);
2210 if (status != 0 || !data->rpc_done)
2211 return status;
2212
Trond Myklebust6926afd2012-01-07 13:22:46 -05002213 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2214
Anna Schumakerd7e98252017-01-11 16:13:29 -05002215 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002216 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002217
2218 return status;
2219}
2220
Trond Myklebustf3792d62014-07-10 08:54:32 -04002221/*
2222 * Additional permission checks in order to distinguish between an
2223 * open for read, and an open for execute. This works around the
2224 * fact that NFSv4 OPEN treats read and execute permissions as being
2225 * the same.
2226 * Note that in the non-execute case, we want to turn off permission
2227 * checking if we just created a new file (POSIX open() semantics).
2228 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002229static int nfs4_opendata_access(struct rpc_cred *cred,
2230 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002231 struct nfs4_state *state, fmode_t fmode,
2232 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002233{
2234 struct nfs_access_entry cache;
2235 u32 mask;
2236
2237 /* access call failed or for some reason the server doesn't
2238 * support any access modes -- defer access call until later */
2239 if (opendata->o_res.access_supported == 0)
2240 return 0;
2241
2242 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002243 /*
2244 * Use openflags to check for exec, because fmode won't
2245 * always have FMODE_EXEC set when file open for exec.
2246 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002247 if (openflags & __FMODE_EXEC) {
2248 /* ONLY check for exec rights */
2249 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002250 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002251 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002252
2253 cache.cred = cred;
2254 cache.jiffies = jiffies;
2255 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2256 nfs_access_add_cache(state->inode, &cache);
2257
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002258 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002259 return 0;
2260
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002261 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002262}
2263
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002264/*
2265 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2266 */
2267static int _nfs4_proc_open(struct nfs4_opendata *data)
2268{
David Howells2b0143b2015-03-17 22:25:59 +00002269 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002270 struct nfs_server *server = NFS_SERVER(dir);
2271 struct nfs_openargs *o_arg = &data->o_arg;
2272 struct nfs_openres *o_res = &data->o_res;
2273 int status;
2274
2275 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002276 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002277 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002278 if (status != 0) {
2279 if (status == -NFS4ERR_BADNAME &&
2280 !(o_arg->open_flags & O_CREAT))
2281 return -ENOENT;
2282 return status;
2283 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002284
Trond Myklebust6926afd2012-01-07 13:22:46 -05002285 nfs_fattr_map_and_free_names(server, &data->f_attr);
2286
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002287 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002288 if (o_arg->open_flags & O_EXCL)
2289 data->file_created = 1;
2290 else if (o_res->cinfo.before != o_res->cinfo.after)
2291 data->file_created = 1;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002292 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002293 update_changeattr(dir, &o_res->cinfo,
2294 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002295 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002296 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2297 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002299 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002301 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002303 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2304 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002305 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002306 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308}
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310/*
2311 * OPEN_EXPIRED:
2312 * reclaim state on the server after a network partition.
2313 * Assumes caller holds the appropriate lock
2314 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002315static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002317 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002318 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002320 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002321 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002322 if (IS_ERR(opendata))
2323 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002324 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002325 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002326 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002327 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002328 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002331static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002332{
Trond Myklebust539cd032007-06-05 11:46:42 -04002333 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002334 struct nfs4_exception exception = { };
2335 int err;
2336
2337 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002338 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002339 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002340 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2341 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002342 switch (err) {
2343 default:
2344 goto out;
2345 case -NFS4ERR_GRACE:
2346 case -NFS4ERR_DELAY:
2347 nfs4_handle_exception(server, err, &exception);
2348 err = 0;
2349 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002350 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002351out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002352 return err;
2353}
2354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2356{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002358 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Trond Myklebust864472e2006-01-03 09:55:15 +01002360 ctx = nfs4_state_find_open_context(state);
2361 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002362 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002363 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002364 put_nfs_open_context(ctx);
2365 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
2367
Trond Myklebust41020b62016-09-22 13:38:58 -04002368static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2369 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002370{
Trond Myklebust41020b62016-09-22 13:38:58 -04002371 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002372 write_seqlock(&state->seqlock);
2373 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2374 write_sequnlock(&state->seqlock);
2375 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2376}
2377
2378static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2379{
2380 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002381 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002382}
2383
2384static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2385{
2386 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2387 nfs40_clear_delegation_stateid(state);
2388 return nfs4_open_expired(sp, state);
2389}
2390
Trond Myklebust45870d62016-09-22 13:38:59 -04002391static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2392 nfs4_stateid *stateid,
2393 struct rpc_cred *cred)
2394{
2395 return -NFS4ERR_BAD_STATEID;
2396}
2397
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002398#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002399static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2400 nfs4_stateid *stateid,
2401 struct rpc_cred *cred)
2402{
2403 int status;
2404
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002405 switch (stateid->type) {
2406 default:
2407 break;
2408 case NFS4_INVALID_STATEID_TYPE:
2409 case NFS4_SPECIAL_STATEID_TYPE:
2410 return -NFS4ERR_BAD_STATEID;
2411 case NFS4_REVOKED_STATEID_TYPE:
2412 goto out_free;
2413 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002414
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002415 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002416 switch (status) {
2417 case -NFS4ERR_EXPIRED:
2418 case -NFS4ERR_ADMIN_REVOKED:
2419 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002420 break;
2421 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002422 return status;
2423 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002424out_free:
2425 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002426 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002427 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002428}
2429
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002430static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002431{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002432 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002433 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002434 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002435 struct rpc_cred *cred;
2436 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002437
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002438 /* Get the delegation credential for use by test/free_stateid */
2439 rcu_read_lock();
2440 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002441 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002442 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002443 return;
2444 }
2445
2446 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002447 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2448 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2449 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002450 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002451 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002452 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002453 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002454
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002455 cred = get_rpccred(delegation->cred);
2456 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002457 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002458 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002459 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002460 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002461
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002462 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002463}
2464
2465/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002466 * nfs41_check_expired_locks - possibly free a lock stateid
2467 *
2468 * @state: NFSv4 state for an inode
2469 *
2470 * Returns NFS_OK if recovery for this stateid is now finished.
2471 * Otherwise a negative NFS4ERR value is returned.
2472 */
2473static int nfs41_check_expired_locks(struct nfs4_state *state)
2474{
2475 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002476 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002477 struct nfs_server *server = NFS_SERVER(state->inode);
2478
2479 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2480 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002481
2482 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002483 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2484 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2485 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2486
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002487 atomic_inc(&lsp->ls_count);
2488 spin_unlock(&state->state_lock);
2489
2490 nfs4_put_lock_state(prev);
2491 prev = lsp;
2492
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002493 status = nfs41_test_and_free_expired_stateid(server,
2494 &lsp->ls_stateid,
2495 cred);
2496 trace_nfs4_test_lock_stateid(state, lsp, status);
2497 if (status == -NFS4ERR_EXPIRED ||
2498 status == -NFS4ERR_BAD_STATEID) {
2499 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002500 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002501 if (!recover_lost_locks)
2502 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2503 } else if (status != NFS_OK) {
2504 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002505 nfs4_put_lock_state(prev);
2506 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002507 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002508 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002509 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002510 }
2511 spin_unlock(&state->state_lock);
2512 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002513out:
2514 return ret;
2515}
2516
2517/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002518 * nfs41_check_open_stateid - possibly free an open stateid
2519 *
2520 * @state: NFSv4 state for an inode
2521 *
2522 * Returns NFS_OK if recovery for this stateid is now finished.
2523 * Otherwise a negative NFS4ERR value is returned.
2524 */
2525static int nfs41_check_open_stateid(struct nfs4_state *state)
2526{
2527 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002528 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002529 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002530 int status;
2531
Trond Myklebustb134fc42016-09-22 13:39:16 -04002532 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002533 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2534 if (nfs4_have_delegation(state->inode, state->state))
2535 return NFS_OK;
2536 return -NFS4ERR_OPENMODE;
2537 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002538 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002539 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002540 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002541 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002542 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002543 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2544 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2545 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002546 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002547 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002548 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002549 if (status != NFS_OK)
2550 return status;
2551 if (nfs_open_stateid_recover_openmode(state))
2552 return -NFS4ERR_OPENMODE;
2553 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002554}
2555
2556static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2557{
Chuck Levereb64cf92012-07-11 16:30:05 -04002558 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002559
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002560 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002561 status = nfs41_check_expired_locks(state);
2562 if (status != NFS_OK)
2563 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002564 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002565 if (status != NFS_OK)
2566 status = nfs4_open_expired(sp, state);
2567 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002568}
2569#endif
2570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002572 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2573 * fields corresponding to attributes that were used to store the verifier.
2574 * Make sure we clobber those fields in the later setattr call
2575 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002576static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2577 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002578{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002579 const u32 *attrset = opendata->o_res.attrset;
2580
2581 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002582 !(sattr->ia_valid & ATTR_ATIME_SET))
2583 sattr->ia_valid |= ATTR_ATIME;
2584
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002585 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002586 !(sattr->ia_valid & ATTR_MTIME_SET))
2587 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002588
2589 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002590 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2591 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002592 sattr->ia_valid &= ~ATTR_MODE;
2593
2594 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2595 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002596}
2597
Trond Myklebustc21443c2013-02-07 14:26:21 -05002598static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2599 fmode_t fmode,
2600 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002601 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002602{
2603 struct nfs4_state_owner *sp = opendata->owner;
2604 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002605 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002606 struct nfs4_state *state;
2607 unsigned int seq;
2608 int ret;
2609
2610 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2611
2612 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002613 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002614 goto out;
2615
2616 state = nfs4_opendata_to_nfs4_state(opendata);
2617 ret = PTR_ERR(state);
2618 if (IS_ERR(state))
2619 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002620 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002621 if (server->caps & NFS_CAP_POSIX_LOCK)
2622 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002623 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2624 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002625
Trond Myklebust275bb302013-05-29 13:11:28 -04002626 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002627 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002628 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002629 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002630 alias = d_exact_alias(dentry, state->inode);
2631 if (!alias)
2632 alias = d_splice_alias(igrab(state->inode), dentry);
2633 /* d_splice_alias() can't fail here - it's a non-directory */
2634 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002635 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002636 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002637 }
2638 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002639 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002640 }
2641
Trond Myklebustc21443c2013-02-07 14:26:21 -05002642 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2643 if (ret != 0)
2644 goto out;
2645
David Howells2b0143b2015-03-17 22:25:59 +00002646 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002647 nfs_inode_attach_open_context(ctx);
2648 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2649 nfs4_schedule_stateid_recovery(server, state);
2650 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002651out:
2652 return ret;
2653}
2654
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002655/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002656 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 */
Andy Adamson82be4172012-05-23 05:02:35 -04002658static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002659 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002660 int flags,
2661 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002662 struct nfs4_label *label,
2663 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664{
2665 struct nfs4_state_owner *sp;
2666 struct nfs4_state *state = NULL;
2667 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002668 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002669 struct dentry *dentry = ctx->dentry;
2670 struct rpc_cred *cred = ctx->cred;
2671 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2672 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002673 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002674 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002675 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
2677 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002679 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2680 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2682 goto out_err;
2683 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002684 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002685 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002686 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002687 if (d_really_is_positive(dentry))
2688 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002689 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002690 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002691 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002692 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002693 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002694 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002695 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
David Quigley14c43f72013-05-22 12:50:43 -04002697 if (label) {
2698 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2699 if (IS_ERR(olabel)) {
2700 status = PTR_ERR(olabel);
2701 goto err_opendata_put;
2702 }
2703 }
2704
Trond Myklebuste911b812014-03-26 13:24:37 -07002705 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2706 if (!opendata->f_attr.mdsthreshold) {
2707 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2708 if (!opendata->f_attr.mdsthreshold)
2709 goto err_free_label;
2710 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002711 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002712 }
David Howells2b0143b2015-03-17 22:25:59 +00002713 if (d_really_is_positive(dentry))
2714 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002715
Trond Myklebust3efb9722013-05-29 13:17:04 -04002716 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002717 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002718 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002719 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002720
NeilBrownefcbc042015-07-30 13:00:56 +10002721 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002722 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002723 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002724 /*
2725 * send create attributes which was not set by open
2726 * with an extra setattr.
2727 */
2728 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2729 nfs_fattr_init(opendata->o_res.f_attr);
2730 status = nfs4_do_setattr(state->inode, cred,
2731 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002732 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002733 if (status == 0) {
2734 nfs_setattr_update_inode(state->inode, sattr,
2735 opendata->o_res.f_attr);
2736 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2737 }
David Quigley1775fd32013-05-22 12:50:42 -04002738 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002739 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002740 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002741 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002742
Trond Myklebuste911b812014-03-26 13:24:37 -07002743 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002744 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002745 opendata->f_attr.mdsthreshold = NULL;
2746 }
Andy Adamson82be4172012-05-23 05:02:35 -04002747
David Quigley14c43f72013-05-22 12:50:43 -04002748 nfs4_label_free(olabel);
2749
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002750 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002753err_free_label:
2754 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002755err_opendata_put:
2756 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002757err_put_state_owner:
2758 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 return status;
2761}
2762
2763
Andy Adamson82be4172012-05-23 05:02:35 -04002764static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002765 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002766 int flags,
2767 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002768 struct nfs4_label *label,
2769 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002771 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 struct nfs4_exception exception = { };
2773 struct nfs4_state *res;
2774 int status;
2775
2776 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002777 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002778 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002779 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 if (status == 0)
2781 break;
2782 /* NOTE: BAD_SEQID means the server and client disagree about the
2783 * book-keeping w.r.t. state-changing operations
2784 * (OPEN/CLOSE/LOCK/LOCKU...)
2785 * It is actually a sign of a bug on the client or on the server.
2786 *
2787 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002788 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 * have unhashed the old state_owner for us, and that we can
2790 * therefore safely retry using a new one. We should still warn
2791 * the user though...
2792 */
2793 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002794 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002795 " returned a bad sequence-id error!\n",
2796 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 exception.retry = 1;
2798 continue;
2799 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002800 /*
2801 * BAD_STATEID on OPEN means that the server cancelled our
2802 * state before it received the OPEN_CONFIRM.
2803 * Recover by retrying the request as per the discussion
2804 * on Page 181 of RFC3530.
2805 */
2806 if (status == -NFS4ERR_BAD_STATEID) {
2807 exception.retry = 1;
2808 continue;
2809 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002810 if (status == -EAGAIN) {
2811 /* We must have found a delegation */
2812 exception.retry = 1;
2813 continue;
2814 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002815 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2816 continue;
2817 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 status, &exception));
2819 } while (exception.retry);
2820 return res;
2821}
2822
Trond Myklebust8487c472016-06-26 08:44:35 -04002823static int _nfs4_do_setattr(struct inode *inode,
2824 struct nfs_setattrargs *arg,
2825 struct nfs_setattrres *res,
2826 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002827 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002828{
2829 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002830 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04002831 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2832 .rpc_argp = arg,
2833 .rpc_resp = res,
2834 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002835 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002836 struct rpc_cred *delegation_cred = NULL;
2837 unsigned long timestamp = jiffies;
2838 fmode_t fmode;
2839 bool truncate;
2840 int status;
2841
2842 nfs_fattr_init(res->fattr);
2843
2844 /* Servers should only apply open mode checks for file size changes */
2845 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2846 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2847
2848 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2849 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002850 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002851 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002852 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002853 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002854 l_ctx = nfs_get_lock_context(ctx);
2855 if (IS_ERR(l_ctx))
2856 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002857 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2858 &arg->stateid, &delegation_cred);
2859 nfs_put_lock_context(l_ctx);
2860 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002861 return -EBADF;
2862 } else
2863 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2864 if (delegation_cred)
2865 msg.rpc_cred = delegation_cred;
2866
2867 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2868
2869 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002870 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002871 renew_lease(server, timestamp);
2872 trace_nfs4_setattr(inode, &arg->stateid, status);
2873 return status;
2874}
2875
2876static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2877 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002878 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002879 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002881 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002882 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002883 struct nfs_setattrargs arg = {
2884 .fh = NFS_FH(inode),
2885 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 .server = server,
2887 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002888 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002889 };
2890 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002892 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002894 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002895 struct nfs4_exception exception = {
2896 .state = state,
2897 .inode = inode,
2898 .stateid = &arg.stateid,
2899 };
2900 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
David Quigleyaa9c2662013-05-22 12:50:44 -04002902 arg.bitmask = nfs4_bitmask(server, ilabel);
2903 if (ilabel)
2904 arg.bitmask = nfs4_bitmask(server, olabel);
2905
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 do {
NeilBrown29b59f92016-10-13 15:26:47 +11002907 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04002908 switch (err) {
2909 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002910 if (!(sattr->ia_valid & ATTR_SIZE)) {
2911 pr_warn_once("NFSv4: server %s is incorrectly "
2912 "applying open mode checks to "
2913 "a SETATTR that is not "
2914 "changing file size.\n",
2915 server->nfs_client->cl_hostname);
2916 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002917 if (state && !(state->state & FMODE_WRITE)) {
2918 err = -EBADF;
2919 if (sattr->ia_valid & ATTR_OPEN)
2920 err = -EACCES;
2921 goto out;
2922 }
2923 }
2924 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002926out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 return err;
2928}
2929
Peng Tao500d7012015-09-22 11:35:22 +08002930static bool
2931nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2932{
2933 if (inode == NULL || !nfs_have_layout(inode))
2934 return false;
2935
2936 return pnfs_wait_on_layoutreturn(inode, task);
2937}
2938
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939struct nfs4_closedata {
2940 struct inode *inode;
2941 struct nfs4_state *state;
2942 struct nfs_closeargs arg;
2943 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05002944 struct {
2945 struct nfs4_layoutreturn_args arg;
2946 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04002947 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05002948 u32 roc_barrier;
2949 bool roc;
2950 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002951 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002952 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953};
2954
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002955static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002956{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002957 struct nfs4_closedata *calldata = data;
2958 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002959 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002960
Trond Myklebustcf805162016-11-15 14:56:07 -05002961 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05002962 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
2963 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07002964 nfs4_put_open_state(calldata->state);
2965 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002966 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002967 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002968 kfree(calldata);
2969}
2970
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002971static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002973 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002976 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
Chuck Levera3ca5652012-03-01 17:00:40 -05002978 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002979 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2980 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002981 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05002982
2983 /* Handle Layoutreturn errors */
2984 if (calldata->arg.lr_args && task->tk_status != 0) {
2985 switch (calldata->res.lr_ret) {
2986 default:
2987 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
2988 break;
2989 case 0:
2990 calldata->arg.lr_args = NULL;
2991 calldata->res.lr_res = NULL;
2992 break;
2993 case -NFS4ERR_ADMIN_REVOKED:
2994 case -NFS4ERR_DELEG_REVOKED:
2995 case -NFS4ERR_EXPIRED:
2996 case -NFS4ERR_BAD_STATEID:
2997 case -NFS4ERR_OLD_STATEID:
2998 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
2999 case -NFS4ERR_WRONG_CRED:
3000 calldata->arg.lr_args = NULL;
3001 calldata->res.lr_res = NULL;
3002 calldata->res.lr_ret = 0;
3003 rpc_restart_call_prepare(task);
3004 return;
3005 }
3006 }
3007
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003008 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 * the state_owner. we keep this around to process errors
3010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 switch (task->tk_status) {
3012 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003013 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003014 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003015 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003016 case -NFS4ERR_ACCESS:
3017 if (calldata->arg.bitmask != NULL) {
3018 calldata->arg.bitmask = NULL;
3019 calldata->res.fattr = NULL;
3020 task->tk_status = 0;
3021 rpc_restart_call_prepare(task);
3022 goto out_release;
3023
3024 }
3025 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003026 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003028 case -NFS4ERR_EXPIRED:
3029 nfs4_free_revoked_stateid(server,
3030 &calldata->arg.stateid,
3031 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003032 case -NFS4ERR_OLD_STATEID:
3033 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003034 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003035 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003036 rpc_restart_call_prepare(task);
3037 goto out_release;
3038 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003039 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003040 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003042 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003043 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003044 goto out_release;
3045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003047 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3048 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003049out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003050 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003051 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003052 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053}
3054
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003055static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003057 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003058 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003059 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003060 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003061 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003062
Chuck Levera3ca5652012-03-01 17:00:40 -05003063 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003064 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003065 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003066
Trond Myklebust88069f72009-12-08 08:33:16 -05003067 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003068 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003069 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3070 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3071 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003072 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003073 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003074 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003075 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003076 if (state->n_rdonly == 0)
3077 call_close |= is_rdonly;
3078 else if (is_rdonly)
3079 calldata->arg.fmode |= FMODE_READ;
3080 if (state->n_wronly == 0)
3081 call_close |= is_wronly;
3082 else if (is_wronly)
3083 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003084 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3085 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003086 } else if (is_rdwr)
3087 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3088
Trond Myklebust5cc78612016-11-14 11:19:56 -05003089 if (!nfs4_valid_open_stateid(state) ||
3090 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003091 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003092 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003093
3094 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003095 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003096 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003097 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003098
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003099 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003100 nfs_release_seqid(calldata->arg.seqid);
3101 goto out_wait;
3102 }
3103
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003104 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003105 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003106
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003107 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003108 /* Close-to-open cache consistency revalidation */
3109 if (!nfs4_have_delegation(inode, FMODE_READ))
3110 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3111 else
3112 calldata->arg.bitmask = NULL;
3113 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003114
Trond Myklebust6ae37332015-01-30 14:21:14 -05003115 calldata->arg.share_access =
3116 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3117 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003118
Trond Myklebustd8d84982016-12-19 12:14:44 -05003119 if (calldata->res.fattr == NULL)
3120 calldata->arg.bitmask = NULL;
3121 else if (calldata->arg.bitmask == NULL)
3122 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003123 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003124 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003125 &calldata->arg.seq_args,
3126 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003127 task) != 0)
3128 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003129 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003130 return;
3131out_no_action:
3132 task->tk_action = NULL;
3133out_wait:
3134 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135}
3136
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003137static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003138 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003139 .rpc_call_done = nfs4_close_done,
3140 .rpc_release = nfs4_free_closedata,
3141};
3142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143/*
3144 * It is possible for data to be read/written from a mem-mapped file
3145 * after the sys_close call (which hits the vfs layer as a flush).
3146 * This means that we can't safely call nfsv4 close on a file until
3147 * the inode is cleared. This in turn means that we are not good
3148 * NFSv4 citizens - we do not indicate to the server to update the file's
3149 * share state even when we are done with one of the three share
3150 * stateid's in the inode.
3151 *
3152 * NOTE: Caller must be holding the sp->so_owner semaphore!
3153 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003154int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003156 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003157 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003159 struct nfs4_state_owner *sp = state->owner;
3160 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003161 struct rpc_message msg = {
3162 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3163 .rpc_cred = state->owner->so_cred,
3164 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003165 struct rpc_task_setup task_setup_data = {
3166 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003167 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003168 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003169 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003170 .flags = RPC_TASK_ASYNC,
3171 };
Trond Myklebust95121352005-10-18 14:20:12 -07003172 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003174 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3175 &task_setup_data.rpc_client, &msg);
3176
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003177 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003179 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003180 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003181 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003183 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003185 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3186 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003187 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003188 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003189 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003190 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003191 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003192 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003193 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003194 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003195 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003196 calldata->lr.roc = pnfs_roc(state->inode,
3197 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3198 if (calldata->lr.roc) {
3199 calldata->arg.lr_args = &calldata->lr.arg;
3200 calldata->res.lr_res = &calldata->lr.res;
3201 }
Al Viro643168c2011-06-22 18:20:23 -04003202 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003203
Trond Myklebust1174dd12010-12-21 10:52:24 -05003204 msg.rpc_argp = &calldata->arg;
3205 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003206 task_setup_data.callback_data = calldata;
3207 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003208 if (IS_ERR(task))
3209 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003210 status = 0;
3211 if (wait)
3212 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003213 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003214 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003215out_free_calldata:
3216 kfree(calldata);
3217out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003218 nfs4_put_open_state(state);
3219 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003220 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
Trond Myklebust2b484292010-09-17 10:56:51 -04003223static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003224nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3225 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003228 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3229
3230 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Trond Myklebust565277f2007-10-15 18:17:53 -04003232 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003233 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003234
3235 nfs4_label_release_security(label);
3236
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003237 if (IS_ERR(state))
3238 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003239 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240}
3241
Trond Myklebust1185a552009-12-03 15:54:02 -05003242static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003243{
3244 if (ctx->state == NULL)
3245 return;
3246 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003247 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003248 else
Al Viro643168c2011-06-22 18:20:23 -04003249 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003250}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
Trond Myklebustb944dba2013-11-04 15:20:20 -05003252#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3253#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003254#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003255
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3257{
Kinglong Mee8c612822015-08-26 21:12:58 +08003258 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003259 struct nfs4_server_caps_arg args = {
3260 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003261 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003262 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 struct nfs4_server_caps_res res = {};
3264 struct rpc_message msg = {
3265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003266 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 .rpc_resp = &res,
3268 };
3269 int status;
3270
Kinglong Mee8c612822015-08-26 21:12:58 +08003271 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3272 FATTR4_WORD0_FH_EXPIRE_TYPE |
3273 FATTR4_WORD0_LINK_SUPPORT |
3274 FATTR4_WORD0_SYMLINK_SUPPORT |
3275 FATTR4_WORD0_ACLSUPPORT;
3276 if (minorversion)
3277 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3278
Bryan Schumaker7c513052011-03-24 17:12:24 +00003279 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003281 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003282 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003283 case 0:
3284 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3285 res.attr_bitmask[2] = 0;
3286 break;
3287 case 1:
3288 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3289 break;
3290 case 2:
3291 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003294 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3295 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3296 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3297 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003298 NFS_CAP_CTIME|NFS_CAP_MTIME|
3299 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003300 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3301 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 server->caps |= NFS_CAP_ACLS;
3303 if (res.has_links != 0)
3304 server->caps |= NFS_CAP_HARDLINKS;
3305 if (res.has_symlinks != 0)
3306 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003307 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3308 server->caps |= NFS_CAP_FILEID;
3309 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3310 server->caps |= NFS_CAP_MODE;
3311 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3312 server->caps |= NFS_CAP_NLINK;
3313 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3314 server->caps |= NFS_CAP_OWNER;
3315 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3316 server->caps |= NFS_CAP_OWNER_GROUP;
3317 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3318 server->caps |= NFS_CAP_ATIME;
3319 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3320 server->caps |= NFS_CAP_CTIME;
3321 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3322 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003323#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3324 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3325 server->caps |= NFS_CAP_SECURITY_LABEL;
3326#endif
3327 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3328 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003329 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003330
Trond Myklebusta65318b2009-03-11 14:10:28 -04003331 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3332 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3333 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003334 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003335 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3336 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003338 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003340
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 return status;
3342}
3343
Trond Myklebust55a97592006-06-09 09:34:19 -04003344int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345{
3346 struct nfs4_exception exception = { };
3347 int err;
3348 do {
3349 err = nfs4_handle_exception(server,
3350 _nfs4_server_capabilities(server, fhandle),
3351 &exception);
3352 } while (exception.retry);
3353 return err;
3354}
3355
3356static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3357 struct nfs_fsinfo *info)
3358{
David Quigleyaa9c2662013-05-22 12:50:44 -04003359 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003361 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 };
3363 struct nfs4_lookup_res res = {
3364 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003365 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 .fh = fhandle,
3367 };
3368 struct rpc_message msg = {
3369 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3370 .rpc_argp = &args,
3371 .rpc_resp = &res,
3372 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003373
David Quigleyaa9c2662013-05-22 12:50:44 -04003374 bitmask[0] = nfs4_fattr_bitmap[0];
3375 bitmask[1] = nfs4_fattr_bitmap[1];
3376 /*
3377 * Process the label in the upcoming getfattr
3378 */
3379 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3380
Trond Myklebust0e574af2005-10-27 22:12:38 -04003381 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003382 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383}
3384
3385static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3386 struct nfs_fsinfo *info)
3387{
3388 struct nfs4_exception exception = { };
3389 int err;
3390 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003391 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003392 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003393 switch (err) {
3394 case 0:
3395 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003396 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003397 default:
3398 err = nfs4_handle_exception(server, err, &exception);
3399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003401out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 return err;
3403}
3404
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003405static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3406 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3407{
Trond Myklebustc2190662013-08-26 19:23:04 -04003408 struct rpc_auth_create_args auth_args = {
3409 .pseudoflavor = flavor,
3410 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003411 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003412
Trond Myklebustc2190662013-08-26 19:23:04 -04003413 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003414 if (IS_ERR(auth))
3415 return -EACCES;
3416 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003417}
3418
Chuck Lever9a744ba2013-03-16 15:56:02 -04003419/*
3420 * Retry pseudoroot lookup with various security flavors. We do this when:
3421 *
3422 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3423 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3424 *
3425 * Returns zero on success, or a negative NFS4ERR value, or a
3426 * negative errno value.
3427 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003428static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003429 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003431 /* Per 3530bis 15.33.5 */
3432 static const rpc_authflavor_t flav_array[] = {
3433 RPC_AUTH_GSS_KRB5P,
3434 RPC_AUTH_GSS_KRB5I,
3435 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003436 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003437 RPC_AUTH_NULL,
3438 };
3439 int status = -EPERM;
3440 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003442 if (server->auth_info.flavor_len > 0) {
3443 /* try each flavor specified by user */
3444 for (i = 0; i < server->auth_info.flavor_len; i++) {
3445 status = nfs4_lookup_root_sec(server, fhandle, info,
3446 server->auth_info.flavors[i]);
3447 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3448 continue;
3449 break;
3450 }
3451 } else {
3452 /* no flavors specified by user, try default list */
3453 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3454 status = nfs4_lookup_root_sec(server, fhandle, info,
3455 flav_array[i]);
3456 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3457 continue;
3458 break;
3459 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003460 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003461
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003462 /*
3463 * -EACCESS could mean that the user doesn't have correct permissions
3464 * to access the mount. It could also mean that we tried to mount
3465 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3466 * existing mount programs don't handle -EACCES very well so it should
3467 * be mapped to -EPERM instead.
3468 */
3469 if (status == -EACCES)
3470 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003471 return status;
3472}
3473
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003474/**
3475 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3476 * @server: initialized nfs_server handle
3477 * @fhandle: we fill in the pseudo-fs root file handle
3478 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003479 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003480 *
3481 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003482 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003483int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003484 struct nfs_fsinfo *info,
3485 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003486{
Andre Przywarac7757072015-04-23 17:17:40 +01003487 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003488
Andre Przywarac7757072015-04-23 17:17:40 +01003489 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003490 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003491
3492 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003493 status = server->nfs_client->cl_mvops->find_root_sec(server,
3494 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003495
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 if (status == 0)
3497 status = nfs4_server_capabilities(server, fhandle);
3498 if (status == 0)
3499 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003500
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003501 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502}
3503
Bryan Schumakerbae36242012-05-10 15:07:31 -04003504static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3505 struct nfs_fsinfo *info)
3506{
3507 int error;
3508 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003509 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003510
3511 error = nfs4_server_capabilities(server, mntfh);
3512 if (error < 0) {
3513 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3514 return error;
3515 }
3516
David Quigley14c43f72013-05-22 12:50:43 -04003517 label = nfs4_label_alloc(server, GFP_KERNEL);
3518 if (IS_ERR(label))
3519 return PTR_ERR(label);
3520
David Quigley1775fd32013-05-22 12:50:42 -04003521 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003522 if (error < 0) {
3523 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003524 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003525 }
3526
3527 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3528 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3529 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3530
David Quigley14c43f72013-05-22 12:50:43 -04003531err_free_label:
3532 nfs4_label_free(label);
3533
Bryan Schumakerbae36242012-05-10 15:07:31 -04003534 return error;
3535}
3536
Manoj Naik6b97fd32006-06-09 09:34:29 -04003537/*
3538 * Get locations and (maybe) other attributes of a referral.
3539 * Note that we'll actually follow the referral later when
3540 * we detect fsid mismatch in inode revalidation
3541 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003542static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3543 const struct qstr *name, struct nfs_fattr *fattr,
3544 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003545{
3546 int status = -ENOMEM;
3547 struct page *page = NULL;
3548 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003549
3550 page = alloc_page(GFP_KERNEL);
3551 if (page == NULL)
3552 goto out;
3553 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3554 if (locations == NULL)
3555 goto out;
3556
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003557 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003558 if (status != 0)
3559 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003560
3561 /*
3562 * If the fsid didn't change, this is a migration event, not a
3563 * referral. Cause us to drop into the exception handler, which
3564 * will kick off migration recovery.
3565 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003566 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003567 dprintk("%s: server did not return a different fsid for"
3568 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003569 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003570 goto out;
3571 }
Andy Adamson533eb462011-06-13 18:25:56 -04003572 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3573 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003574
Andy Adamson533eb462011-06-13 18:25:56 -04003575 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003576 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003577 memset(fhandle, 0, sizeof(struct nfs_fh));
3578out:
3579 if (page)
3580 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003581 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003582 return status;
3583}
3584
David Quigley1775fd32013-05-22 12:50:42 -04003585static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3586 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
3588 struct nfs4_getattr_arg args = {
3589 .fh = fhandle,
3590 .bitmask = server->attr_bitmask,
3591 };
3592 struct nfs4_getattr_res res = {
3593 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003594 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 .server = server,
3596 };
3597 struct rpc_message msg = {
3598 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3599 .rpc_argp = &args,
3600 .rpc_resp = &res,
3601 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003602
3603 args.bitmask = nfs4_bitmask(server, label);
3604
Trond Myklebust0e574af2005-10-27 22:12:38 -04003605 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003606 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607}
3608
David Quigley1775fd32013-05-22 12:50:42 -04003609static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3610 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611{
3612 struct nfs4_exception exception = { };
3613 int err;
3614 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003615 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3616 trace_nfs4_getattr(server, fhandle, fattr, err);
3617 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 &exception);
3619 } while (exception.retry);
3620 return err;
3621}
3622
3623/*
3624 * The file is not closed if it is opened due to the a request to change
3625 * the size of the file. The open call will not be needed once the
3626 * VFS layer lookup-intents are implemented.
3627 *
3628 * Close is called when the inode is destroyed.
3629 * If we haven't opened the file for O_WRONLY, we
3630 * need to in the size_change case to obtain a stateid.
3631 *
3632 * Got race?
3633 * Because OPEN is always done by name in nfsv4, it is
3634 * possible that we opened a different file by the same
3635 * name. We can recognize this race condition, but we
3636 * can't do anything about it besides returning an error.
3637 *
3638 * This will be fixed with VFS changes (lookup-intent).
3639 */
3640static int
3641nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3642 struct iattr *sattr)
3643{
David Howells2b0143b2015-03-17 22:25:59 +00003644 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003645 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003646 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003647 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 int status;
3649
Peng Tao88ac8152014-09-12 11:04:10 +08003650 if (pnfs_ld_layoutret_on_setattr(inode) &&
3651 sattr->ia_valid & ATTR_SIZE &&
3652 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003653 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003654
Trond Myklebust0e574af2005-10-27 22:12:38 -04003655 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
Andy Adamson26699402012-05-30 16:12:24 -04003657 /* Deal with open(O_TRUNC) */
3658 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003659 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003660
3661 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003662 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003663 return 0;
3664
Trond Myklebustd5308382005-11-04 15:33:38 -05003665 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003666 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003667
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003668 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003669 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003670 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003671 }
3672
David Quigley14c43f72013-05-22 12:50:43 -04003673 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3674 if (IS_ERR(label))
3675 return PTR_ERR(label);
3676
NeilBrown29b59f92016-10-13 15:26:47 +11003677 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003678 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003679 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003680 nfs_setsecurity(inode, fattr, label);
3681 }
David Quigley14c43f72013-05-22 12:50:43 -04003682 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 return status;
3684}
3685
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003686static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3687 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003688 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003689{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003690 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003691 int status;
3692 struct nfs4_lookup_arg args = {
3693 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003694 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003695 .name = name,
3696 };
3697 struct nfs4_lookup_res res = {
3698 .server = server,
3699 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003700 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003701 .fh = fhandle,
3702 };
3703 struct rpc_message msg = {
3704 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3705 .rpc_argp = &args,
3706 .rpc_resp = &res,
3707 };
3708
David Quigleyaa9c2662013-05-22 12:50:44 -04003709 args.bitmask = nfs4_bitmask(server, label);
3710
David Howells2b3de442006-08-22 20:06:09 -04003711 nfs_fattr_init(fattr);
3712
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003714 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 dprintk("NFS reply lookup: %d\n", status);
3716 return status;
3717}
3718
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003719static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003720{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003721 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003722 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003723 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3724 fattr->nlink = 2;
3725}
3726
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003727static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003728 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003729 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003730{
3731 struct nfs4_exception exception = { };
3732 struct rpc_clnt *client = *clnt;
3733 int err;
3734 do {
David Quigley1775fd32013-05-22 12:50:42 -04003735 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003736 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003737 switch (err) {
3738 case -NFS4ERR_BADNAME:
3739 err = -ENOENT;
3740 goto out;
3741 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003742 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003743 if (err == -NFS4ERR_MOVED)
3744 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003745 goto out;
3746 case -NFS4ERR_WRONGSEC:
3747 err = -EPERM;
3748 if (client != *clnt)
3749 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003750 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003751 if (IS_ERR(client))
3752 return PTR_ERR(client);
3753
3754 exception.retry = 1;
3755 break;
3756 default:
3757 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3758 }
3759 } while (exception.retry);
3760
3761out:
3762 if (err == 0)
3763 *clnt = client;
3764 else if (client != *clnt)
3765 rpc_shutdown_client(client);
3766
3767 return err;
3768}
3769
Al Virobeffb8f2016-07-20 16:34:42 -04003770static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003771 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3772 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003774 int status;
3775 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003776
David Quigley1775fd32013-05-22 12:50:42 -04003777 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003778 if (client != NFS_CLIENT(dir)) {
3779 rpc_shutdown_client(client);
3780 nfs_fixup_secinfo_attributes(fattr);
3781 }
3782 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783}
3784
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003785struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003786nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003787 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3788{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003789 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003790 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003791
David Quigley1775fd32013-05-22 12:50:42 -04003792 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003793 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003794 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003795 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003796}
3797
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3799{
Trond Myklebust76b32992007-08-10 17:45:11 -04003800 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 struct nfs4_accessargs args = {
3802 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003803 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003805 struct nfs4_accessres res = {
3806 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003807 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 struct rpc_message msg = {
3809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3810 .rpc_argp = &args,
3811 .rpc_resp = &res,
3812 .rpc_cred = entry->cred,
3813 };
3814 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003815 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816
3817 /*
3818 * Determine which access bits we want to ask for...
3819 */
3820 if (mode & MAY_READ)
3821 args.access |= NFS4_ACCESS_READ;
3822 if (S_ISDIR(inode->i_mode)) {
3823 if (mode & MAY_WRITE)
3824 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3825 if (mode & MAY_EXEC)
3826 args.access |= NFS4_ACCESS_LOOKUP;
3827 } else {
3828 if (mode & MAY_WRITE)
3829 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3830 if (mode & MAY_EXEC)
3831 args.access |= NFS4_ACCESS_EXECUTE;
3832 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003833
3834 res.fattr = nfs_alloc_fattr();
3835 if (res.fattr == NULL)
3836 return -ENOMEM;
3837
Bryan Schumaker7c513052011-03-24 17:12:24 +00003838 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003840 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003841 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003843 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 return status;
3845}
3846
3847static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3848{
3849 struct nfs4_exception exception = { };
3850 int err;
3851 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003852 err = _nfs4_proc_access(inode, entry);
3853 trace_nfs4_access(inode, err);
3854 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 &exception);
3856 } while (exception.retry);
3857 return err;
3858}
3859
3860/*
3861 * TODO: For the time being, we don't try to get any attributes
3862 * along with any of the zero-copy operations READ, READDIR,
3863 * READLINK, WRITE.
3864 *
3865 * In the case of the first three, we want to put the GETATTR
3866 * after the read-type operation -- this is because it is hard
3867 * to predict the length of a GETATTR response in v4, and thus
3868 * align the READ data correctly. This means that the GETATTR
3869 * may end up partially falling into the page cache, and we should
3870 * shift it into the 'tail' of the xdr_buf before processing.
3871 * To do this efficiently, we need to know the total length
3872 * of data received, which doesn't seem to be available outside
3873 * of the RPC layer.
3874 *
3875 * In the case of WRITE, we also want to put the GETATTR after
3876 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003877 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 *
3879 * Both of these changes to the XDR layer would in fact be quite
3880 * minor, but I decided to leave them for a subsequent patch.
3881 */
3882static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3883 unsigned int pgbase, unsigned int pglen)
3884{
3885 struct nfs4_readlink args = {
3886 .fh = NFS_FH(inode),
3887 .pgbase = pgbase,
3888 .pglen = pglen,
3889 .pages = &page,
3890 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003891 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 struct rpc_message msg = {
3893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3894 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003895 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 };
3897
Bryan Schumaker7c513052011-03-24 17:12:24 +00003898 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 -07003899}
3900
3901static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3902 unsigned int pgbase, unsigned int pglen)
3903{
3904 struct nfs4_exception exception = { };
3905 int err;
3906 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003907 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3908 trace_nfs4_readlink(inode, err);
3909 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 &exception);
3911 } while (exception.retry);
3912 return err;
3913}
3914
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003916 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918static int
3919nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003920 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003922 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04003923 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003924 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 int status = 0;
3927
NeilBrown532d4de2016-10-13 15:26:47 +11003928 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003929 if (IS_ERR(ctx))
3930 return PTR_ERR(ctx);
3931
David Quigleyaa9c2662013-05-22 12:50:44 -04003932 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3933
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003934 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
3935 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003936 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 if (IS_ERR(state)) {
3938 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003939 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003942 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003943 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 return status;
3945}
3946
Al Virobeffb8f2016-07-20 16:34:42 -04003947static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948{
Trond Myklebust16e42952005-10-27 22:12:44 -04003949 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003950 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003952 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003954 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003955 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003956 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3959 .rpc_argp = &args,
3960 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05003962 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04003963 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003964
Bryan Schumaker7c513052011-03-24 17:12:24 +00003965 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003966 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05003967 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 return status;
3969}
3970
Al Virobeffb8f2016-07-20 16:34:42 -04003971static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972{
3973 struct nfs4_exception exception = { };
3974 int err;
3975 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003976 err = _nfs4_proc_remove(dir, name);
3977 trace_nfs4_remove(dir, name, err);
3978 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 &exception);
3980 } while (exception.retry);
3981 return err;
3982}
3983
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003984static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003986 struct nfs_server *server = NFS_SERVER(dir);
3987 struct nfs_removeargs *args = msg->rpc_argp;
3988 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003990 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003992 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003993
3994 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995}
3996
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003997static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3998{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003999 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004000 &data->args.seq_args,
4001 &data->res.seq_res,
4002 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003}
4004
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004005static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004007 struct nfs_unlinkdata *data = task->tk_calldata;
4008 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004009
Trond Myklebust14516c32010-07-31 14:29:06 -04004010 if (!nfs4_sequence_done(task, &res->seq_res))
4011 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004012 if (nfs4_async_handle_error(task, res->server, NULL,
4013 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004014 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004015 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004016 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004017 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018}
4019
Jeff Laytond3d41522010-09-17 17:31:57 -04004020static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4021{
4022 struct nfs_server *server = NFS_SERVER(dir);
4023 struct nfs_renameargs *arg = msg->rpc_argp;
4024 struct nfs_renameres *res = msg->rpc_resp;
4025
4026 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004027 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004028 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004029}
4030
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004031static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4032{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004033 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004034 &data->args.seq_args,
4035 &data->res.seq_res,
4036 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004037}
4038
4039static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4040 struct inode *new_dir)
4041{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004042 struct nfs_renamedata *data = task->tk_calldata;
4043 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004044
4045 if (!nfs4_sequence_done(task, &res->seq_res))
4046 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004047 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004048 return 0;
4049
Trond Myklebustc733c492017-01-11 12:32:26 -05004050 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004051 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004052 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004053 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004054 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004055 return 1;
4056}
4057
Al Virobeffb8f2016-07-20 16:34:42 -04004058static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004060 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 struct nfs4_link_arg arg = {
4062 .fh = NFS_FH(inode),
4063 .dir_fh = NFS_FH(dir),
4064 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004065 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004067 struct nfs4_link_res res = {
4068 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004069 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004070 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 struct rpc_message msg = {
4072 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4073 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004074 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004076 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
Trond Myklebust136f2622010-04-16 16:22:49 -04004078 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004079 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004080 goto out;
4081
David Quigley14c43f72013-05-22 12:50:43 -04004082 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4083 if (IS_ERR(res.label)) {
4084 status = PTR_ERR(res.label);
4085 goto out;
4086 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004087 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004088
Bryan Schumaker7c513052011-03-24 17:12:24 +00004089 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004090 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004091 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004092 status = nfs_post_op_update_inode(inode, res.fattr);
4093 if (!status)
4094 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004095 }
David Quigley14c43f72013-05-22 12:50:43 -04004096
4097
4098 nfs4_label_free(res.label);
4099
Trond Myklebust136f2622010-04-16 16:22:49 -04004100out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004101 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 return status;
4103}
4104
Al Virobeffb8f2016-07-20 16:34:42 -04004105static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106{
4107 struct nfs4_exception exception = { };
4108 int err;
4109 do {
4110 err = nfs4_handle_exception(NFS_SERVER(inode),
4111 _nfs4_proc_link(inode, dir, name),
4112 &exception);
4113 } while (exception.retry);
4114 return err;
4115}
4116
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004117struct nfs4_createdata {
4118 struct rpc_message msg;
4119 struct nfs4_create_arg arg;
4120 struct nfs4_create_res res;
4121 struct nfs_fh fh;
4122 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004123 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004124};
4125
4126static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004127 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004128{
4129 struct nfs4_createdata *data;
4130
4131 data = kzalloc(sizeof(*data), GFP_KERNEL);
4132 if (data != NULL) {
4133 struct nfs_server *server = NFS_SERVER(dir);
4134
David Quigley14c43f72013-05-22 12:50:43 -04004135 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4136 if (IS_ERR(data->label))
4137 goto out_free;
4138
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004139 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4140 data->msg.rpc_argp = &data->arg;
4141 data->msg.rpc_resp = &data->res;
4142 data->arg.dir_fh = NFS_FH(dir);
4143 data->arg.server = server;
4144 data->arg.name = name;
4145 data->arg.attrs = sattr;
4146 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004147 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004148 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004149 data->res.server = server;
4150 data->res.fh = &data->fh;
4151 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004152 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004153 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004154 }
4155 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004156out_free:
4157 kfree(data);
4158 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004159}
4160
4161static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4162{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004163 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004164 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004165 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004166 update_changeattr(dir, &data->res.dir_cinfo,
4167 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004168 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004169 }
4170 return status;
4171}
4172
4173static void nfs4_free_createdata(struct nfs4_createdata *data)
4174{
David Quigley14c43f72013-05-22 12:50:43 -04004175 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004176 kfree(data);
4177}
4178
Chuck Lever4f390c12006-08-22 20:06:22 -04004179static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004180 struct page *page, unsigned int len, struct iattr *sattr,
4181 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004183 struct nfs4_createdata *data;
4184 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185
Chuck Lever94a6d752006-08-22 20:06:23 -04004186 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004187 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004188
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004189 status = -ENOMEM;
4190 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4191 if (data == NULL)
4192 goto out;
4193
4194 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4195 data->arg.u.symlink.pages = &page;
4196 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004197 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004199 status = nfs4_do_create(dir, dentry, data);
4200
4201 nfs4_free_createdata(data);
4202out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 return status;
4204}
4205
Chuck Lever4f390c12006-08-22 20:06:22 -04004206static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004207 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208{
4209 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004210 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004212
4213 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4214
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004216 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4217 trace_nfs4_symlink(dir, &dentry->d_name, err);
4218 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 &exception);
4220 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004221
4222 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 return err;
4224}
4225
4226static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004227 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004229 struct nfs4_createdata *data;
4230 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004232 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4233 if (data == NULL)
4234 goto out;
4235
David Quigley1775fd32013-05-22 12:50:42 -04004236 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004237 status = nfs4_do_create(dir, dentry, data);
4238
4239 nfs4_free_createdata(data);
4240out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 return status;
4242}
4243
4244static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4245 struct iattr *sattr)
4246{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004247 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004249 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004251
David Quigleyaa9c2662013-05-22 12:50:44 -04004252 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4253
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004254 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4255 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004257 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4258 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4259 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 &exception);
4261 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004262 nfs4_label_release_security(label);
4263
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 return err;
4265}
4266
4267static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004268 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269{
David Howells2b0143b2015-03-17 22:25:59 +00004270 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 struct nfs4_readdir_arg args = {
4272 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004273 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 .pgbase = 0,
4275 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004276 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004277 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 };
4279 struct nfs4_readdir_res res;
4280 struct rpc_message msg = {
4281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4282 .rpc_argp = &args,
4283 .rpc_resp = &res,
4284 .rpc_cred = cred,
4285 };
4286 int status;
4287
Al Viro6de14722013-09-16 10:53:17 -04004288 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4289 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004290 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004291 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004293 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 -05004294 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004295 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004296 status += args.pgbase;
4297 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004298
4299 nfs_invalidate_atime(dir);
4300
Harvey Harrison3110ff82008-05-02 13:42:44 -07004301 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 return status;
4303}
4304
4305static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004306 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307{
4308 struct nfs4_exception exception = { };
4309 int err;
4310 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004311 err = _nfs4_proc_readdir(dentry, cred, cookie,
4312 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004313 trace_nfs4_readdir(d_inode(dentry), err);
4314 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 &exception);
4316 } while (exception.retry);
4317 return err;
4318}
4319
4320static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004321 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004323 struct nfs4_createdata *data;
4324 int mode = sattr->ia_mode;
4325 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004327 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4328 if (data == NULL)
4329 goto out;
4330
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004332 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004334 data->arg.ftype = NF4BLK;
4335 data->arg.u.device.specdata1 = MAJOR(rdev);
4336 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 }
4338 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004339 data->arg.ftype = NF4CHR;
4340 data->arg.u.device.specdata1 = MAJOR(rdev);
4341 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004342 } else if (!S_ISSOCK(mode)) {
4343 status = -EINVAL;
4344 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 }
David Quigley1775fd32013-05-22 12:50:42 -04004346
David Quigleyaa9c2662013-05-22 12:50:44 -04004347 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004348 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004349out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004350 nfs4_free_createdata(data);
4351out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 return status;
4353}
4354
4355static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4356 struct iattr *sattr, dev_t rdev)
4357{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004358 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004360 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004362
David Quigleyaa9c2662013-05-22 12:50:44 -04004363 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4364
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004365 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4366 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004368 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4369 trace_nfs4_mknod(dir, &dentry->d_name, err);
4370 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 &exception);
4372 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004373
4374 nfs4_label_release_security(label);
4375
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 return err;
4377}
4378
4379static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4380 struct nfs_fsstat *fsstat)
4381{
4382 struct nfs4_statfs_arg args = {
4383 .fh = fhandle,
4384 .bitmask = server->attr_bitmask,
4385 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004386 struct nfs4_statfs_res res = {
4387 .fsstat = fsstat,
4388 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 struct rpc_message msg = {
4390 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4391 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004392 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 };
4394
Trond Myklebust0e574af2005-10-27 22:12:38 -04004395 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004396 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397}
4398
4399static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4400{
4401 struct nfs4_exception exception = { };
4402 int err;
4403 do {
4404 err = nfs4_handle_exception(server,
4405 _nfs4_proc_statfs(server, fhandle, fsstat),
4406 &exception);
4407 } while (exception.retry);
4408 return err;
4409}
4410
4411static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4412 struct nfs_fsinfo *fsinfo)
4413{
4414 struct nfs4_fsinfo_arg args = {
4415 .fh = fhandle,
4416 .bitmask = server->attr_bitmask,
4417 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004418 struct nfs4_fsinfo_res res = {
4419 .fsinfo = fsinfo,
4420 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 struct rpc_message msg = {
4422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4423 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004424 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 };
4426
Bryan Schumaker7c513052011-03-24 17:12:24 +00004427 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428}
4429
4430static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4431{
4432 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004433 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 int err;
4435
4436 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004437 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004438 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004439 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004440 nfs4_set_lease_period(server->nfs_client,
4441 fsinfo->lease_time * HZ,
4442 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004443 break;
4444 }
4445 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 } while (exception.retry);
4447 return err;
4448}
4449
4450static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4451{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004452 int error;
4453
Trond Myklebust0e574af2005-10-27 22:12:38 -04004454 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004455 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004456 if (error == 0) {
4457 /* block layout checks this! */
4458 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004459 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004460 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004461
4462 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463}
4464
4465static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4466 struct nfs_pathconf *pathconf)
4467{
4468 struct nfs4_pathconf_arg args = {
4469 .fh = fhandle,
4470 .bitmask = server->attr_bitmask,
4471 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004472 struct nfs4_pathconf_res res = {
4473 .pathconf = pathconf,
4474 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 struct rpc_message msg = {
4476 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4477 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004478 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 };
4480
4481 /* None of the pathconf attributes are mandatory to implement */
4482 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4483 memset(pathconf, 0, sizeof(*pathconf));
4484 return 0;
4485 }
4486
Trond Myklebust0e574af2005-10-27 22:12:38 -04004487 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004488 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489}
4490
4491static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4492 struct nfs_pathconf *pathconf)
4493{
4494 struct nfs4_exception exception = { };
4495 int err;
4496
4497 do {
4498 err = nfs4_handle_exception(server,
4499 _nfs4_proc_pathconf(server, fhandle, pathconf),
4500 &exception);
4501 } while (exception.retry);
4502 return err;
4503}
4504
Trond Myklebust5521abf2013-03-16 20:54:34 -04004505int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004506 const struct nfs_open_context *ctx,
4507 const struct nfs_lock_context *l_ctx,
4508 fmode_t fmode)
4509{
NeilBrown17393472016-10-13 15:26:47 +11004510 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004511}
4512EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4513
Trond Myklebust5521abf2013-03-16 20:54:34 -04004514static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4515 const struct nfs_open_context *ctx,
4516 const struct nfs_lock_context *l_ctx,
4517 fmode_t fmode)
4518{
4519 nfs4_stateid current_stateid;
4520
Trond Myklebuste1253be2014-03-05 08:44:23 -05004521 /* If the current stateid represents a lost lock, then exit */
4522 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4523 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004524 return nfs4_stateid_match(stateid, &current_stateid);
4525}
4526
4527static bool nfs4_error_stateid_expired(int err)
4528{
4529 switch (err) {
4530 case -NFS4ERR_DELEG_REVOKED:
4531 case -NFS4ERR_ADMIN_REVOKED:
4532 case -NFS4ERR_BAD_STATEID:
4533 case -NFS4ERR_STALE_STATEID:
4534 case -NFS4ERR_OLD_STATEID:
4535 case -NFS4ERR_OPENMODE:
4536 case -NFS4ERR_EXPIRED:
4537 return true;
4538 }
4539 return false;
4540}
4541
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004542static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004544 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004546 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004547 if (task->tk_status < 0) {
4548 struct nfs4_exception exception = {
4549 .inode = hdr->inode,
4550 .state = hdr->args.context->state,
4551 .stateid = &hdr->args.stateid,
4552 };
4553 task->tk_status = nfs4_async_handle_exception(task,
4554 server, task->tk_status, &exception);
4555 if (exception.retry) {
4556 rpc_restart_call_prepare(task);
4557 return -EAGAIN;
4558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004560
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004562 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004563 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564}
4565
Trond Myklebust5521abf2013-03-16 20:54:34 -04004566static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004567 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004568{
4569
4570 if (!nfs4_error_stateid_expired(task->tk_status) ||
4571 nfs4_stateid_is_current(&args->stateid,
4572 args->context,
4573 args->lock_context,
4574 FMODE_READ))
4575 return false;
4576 rpc_restart_call_prepare(task);
4577 return true;
4578}
4579
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004580static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004581{
4582
4583 dprintk("--> %s\n", __func__);
4584
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004585 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004586 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004587 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004588 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004589 if (task->tk_status > 0)
4590 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004591 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4592 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004593}
4594
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004595static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4596 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004598 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004599 if (!hdr->pgio_done_cb)
4600 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004601 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004602 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603}
4604
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004605static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4606 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004607{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004608 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004609 &hdr->args.seq_args,
4610 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004611 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004612 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004613 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4614 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004615 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004616 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004617 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004618 return -EIO;
4619 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620}
4621
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004622static int nfs4_write_done_cb(struct rpc_task *task,
4623 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004625 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004626
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004627 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004628 if (task->tk_status < 0) {
4629 struct nfs4_exception exception = {
4630 .inode = hdr->inode,
4631 .state = hdr->args.context->state,
4632 .stateid = &hdr->args.stateid,
4633 };
4634 task->tk_status = nfs4_async_handle_exception(task,
4635 NFS_SERVER(inode), task->tk_status,
4636 &exception);
4637 if (exception.retry) {
4638 rpc_restart_call_prepare(task);
4639 return -EAGAIN;
4640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004642 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004643 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004644 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004645 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004646 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647}
4648
Trond Myklebust5521abf2013-03-16 20:54:34 -04004649static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004650 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004651{
4652
4653 if (!nfs4_error_stateid_expired(task->tk_status) ||
4654 nfs4_stateid_is_current(&args->stateid,
4655 args->context,
4656 args->lock_context,
4657 FMODE_WRITE))
4658 return false;
4659 rpc_restart_call_prepare(task);
4660 return true;
4661}
4662
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004663static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004664{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004665 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004666 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004667 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004668 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004669 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4670 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004671}
4672
Trond Myklebust5a37f852012-04-28 14:55:16 -04004673static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004674bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004675{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004676 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004677 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004678 return false;
4679 /* Otherwise, request attributes if and only if we don't hold
4680 * a delegation
4681 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004682 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004683}
Fred Isamana69aef12011-03-03 15:13:47 +00004684
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004685static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4686 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004688 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004689
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004690 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4691 hdr->args.bitmask = NULL;
4692 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004693 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004694 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004695
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004696 if (!hdr->pgio_done_cb)
4697 hdr->pgio_done_cb = nfs4_write_done_cb;
4698 hdr->res.server = server;
4699 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004701 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004702 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703}
4704
Fred Isaman0b7c0152012-04-20 14:47:39 -04004705static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4706{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004707 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004708 &data->args.seq_args,
4709 &data->res.seq_res,
4710 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711}
4712
Fred Isaman0b7c0152012-04-20 14:47:39 -04004713static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004716
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004717 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004718 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4719 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004720 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004721 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004723 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724}
4725
Fred Isaman0b7c0152012-04-20 14:47:39 -04004726static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004727{
4728 if (!nfs4_sequence_done(task, &data->res.seq_res))
4729 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004730 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004731}
4732
Fred Isaman0b7c0152012-04-20 14:47:39 -04004733static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004735 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004736
Fred Isaman0b7c0152012-04-20 14:47:39 -04004737 if (data->commit_done_cb == NULL)
4738 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004739 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004740 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004741 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742}
4743
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004744struct nfs4_renewdata {
4745 struct nfs_client *client;
4746 unsigned long timestamp;
4747};
4748
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749/*
4750 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4751 * standalone procedure for queueing an asynchronous RENEW.
4752 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004753static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004754{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004755 struct nfs4_renewdata *data = calldata;
4756 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004757
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004758 if (atomic_read(&clp->cl_count) > 1)
4759 nfs4_schedule_state_renewal(clp);
4760 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004761 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004762}
4763
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004764static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004766 struct nfs4_renewdata *data = calldata;
4767 struct nfs_client *clp = data->client;
4768 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004770 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004771 switch (task->tk_status) {
4772 case 0:
4773 break;
4774 case -NFS4ERR_LEASE_MOVED:
4775 nfs4_schedule_lease_moved_recovery(clp);
4776 break;
4777 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004778 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004779 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4780 return;
4781 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004782 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004783 return;
4784 }
4785 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004787 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788}
4789
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004790static const struct rpc_call_ops nfs4_renew_ops = {
4791 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004792 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004793};
4794
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004795static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796{
4797 struct rpc_message msg = {
4798 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4799 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004800 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004802 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004804 if (renew_flags == 0)
4805 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004806 if (!atomic_inc_not_zero(&clp->cl_count))
4807 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004808 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004809 if (data == NULL)
4810 return -ENOMEM;
4811 data->client = clp;
4812 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004813 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004814 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815}
4816
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004817static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818{
4819 struct rpc_message msg = {
4820 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4821 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004822 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 };
4824 unsigned long now = jiffies;
4825 int status;
4826
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004827 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 if (status < 0)
4829 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004830 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 return 0;
4832}
4833
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004834static inline int nfs4_server_supports_acls(struct nfs_server *server)
4835{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004836 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004837}
4838
Trond Myklebust21f498c2012-08-24 10:59:25 -04004839/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4840 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004841 * the stack.
4842 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004843#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004844
Neil Hormane9e3d722011-03-04 19:26:03 -05004845static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004846 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004847{
4848 struct page *newpage, **spages;
4849 int rc = 0;
4850 size_t len;
4851 spages = pages;
4852
4853 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004854 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004855 newpage = alloc_page(GFP_KERNEL);
4856
4857 if (newpage == NULL)
4858 goto unwind;
4859 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05004860 buf += len;
4861 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05004862 *pages++ = newpage;
4863 rc++;
4864 } while (buflen != 0);
4865
4866 return rc;
4867
4868unwind:
4869 for(; rc > 0; rc--)
4870 __free_page(spages[rc-1]);
4871 return -ENOMEM;
4872}
4873
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004874struct nfs4_cached_acl {
4875 int cached;
4876 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004877 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004878};
4879
4880static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004881{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004882 struct nfs_inode *nfsi = NFS_I(inode);
4883
4884 spin_lock(&inode->i_lock);
4885 kfree(nfsi->nfs4_acl);
4886 nfsi->nfs4_acl = acl;
4887 spin_unlock(&inode->i_lock);
4888}
4889
4890static void nfs4_zap_acl_attr(struct inode *inode)
4891{
4892 nfs4_set_cached_acl(inode, NULL);
4893}
4894
4895static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4896{
4897 struct nfs_inode *nfsi = NFS_I(inode);
4898 struct nfs4_cached_acl *acl;
4899 int ret = -ENOENT;
4900
4901 spin_lock(&inode->i_lock);
4902 acl = nfsi->nfs4_acl;
4903 if (acl == NULL)
4904 goto out;
4905 if (buf == NULL) /* user is just asking for length */
4906 goto out_len;
4907 if (acl->cached == 0)
4908 goto out;
4909 ret = -ERANGE; /* see getxattr(2) man page */
4910 if (acl->len > buflen)
4911 goto out;
4912 memcpy(buf, acl->data, acl->len);
4913out_len:
4914 ret = acl->len;
4915out:
4916 spin_unlock(&inode->i_lock);
4917 return ret;
4918}
4919
Sachin Prabhu5794d212012-04-17 14:36:40 +01004920static 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 +00004921{
4922 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004923 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004924
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004925 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004926 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004927 if (acl == NULL)
4928 goto out;
4929 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004930 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004931 } else {
4932 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4933 if (acl == NULL)
4934 goto out;
4935 acl->cached = 0;
4936 }
4937 acl->len = acl_len;
4938out:
4939 nfs4_set_cached_acl(inode, acl);
4940}
4941
Andy Adamsonbf118a32011-12-07 11:55:27 -05004942/*
4943 * The getxattr API returns the required buffer length when called with a
4944 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4945 * the required buf. On a NULL buf, we send a page of data to the server
4946 * guessing that the ACL request can be serviced by a page. If so, we cache
4947 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4948 * the cache. If not so, we throw away the page, and cache the required
4949 * length. The next getxattr call will then produce another round trip to
4950 * the server, this time with the input buf of the required size.
4951 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004952static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004953{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05004954 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004955 struct nfs_getaclargs args = {
4956 .fh = NFS_FH(inode),
4957 .acl_pages = pages,
4958 .acl_len = buflen,
4959 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004960 struct nfs_getaclres res = {
4961 .acl_len = buflen,
4962 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004963 struct rpc_message msg = {
4964 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4965 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004966 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004967 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05004968 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004969 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004970
Trond Myklebust21f498c2012-08-24 10:59:25 -04004971 if (npages > ARRAY_SIZE(pages))
4972 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004973
Andy Adamsonbf118a32011-12-07 11:55:27 -05004974 for (i = 0; i < npages; i++) {
4975 pages[i] = alloc_page(GFP_KERNEL);
4976 if (!pages[i])
4977 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004978 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004979
4980 /* for decoding across pages */
4981 res.acl_scratch = alloc_page(GFP_KERNEL);
4982 if (!res.acl_scratch)
4983 goto out_free;
4984
Andy Adamsonbf118a32011-12-07 11:55:27 -05004985 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004986
Peng Taode040be2012-01-10 22:42:47 +08004987 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004988 __func__, buf, buflen, npages, args.acl_len);
4989 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4990 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004991 if (ret)
4992 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004993
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004994 /* Handle the case where the passed-in buffer is too short */
4995 if (res.acl_flags & NFS4_ACL_TRUNC) {
4996 /* Did the user only issue a request for the acl length? */
4997 if (buf == NULL)
4998 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004999 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005000 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005001 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005002 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005003 if (buf) {
5004 if (res.acl_len > buflen) {
5005 ret = -ERANGE;
5006 goto out_free;
5007 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005008 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005009 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005010out_ok:
5011 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005012out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005013 for (i = 0; i < npages; i++)
5014 if (pages[i])
5015 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005016 if (res.acl_scratch)
5017 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005018 return ret;
5019}
5020
Trond Myklebust16b42892006-08-24 12:27:15 -04005021static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5022{
5023 struct nfs4_exception exception = { };
5024 ssize_t ret;
5025 do {
5026 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005027 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005028 if (ret >= 0)
5029 break;
5030 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5031 } while (exception.retry);
5032 return ret;
5033}
5034
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005035static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5036{
5037 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005038 int ret;
5039
5040 if (!nfs4_server_supports_acls(server))
5041 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005042 ret = nfs_revalidate_inode(server, inode);
5043 if (ret < 0)
5044 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005045 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5046 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005047 ret = nfs4_read_cached_acl(inode, buf, buflen);
5048 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005049 /* -ENOENT is returned if there is no ACL or if there is an ACL
5050 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005051 return ret;
5052 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005053}
5054
Trond Myklebust16b42892006-08-24 12:27:15 -04005055static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005056{
5057 struct nfs_server *server = NFS_SERVER(inode);
5058 struct page *pages[NFS4ACL_MAXPAGES];
5059 struct nfs_setaclargs arg = {
5060 .fh = NFS_FH(inode),
5061 .acl_pages = pages,
5062 .acl_len = buflen,
5063 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005064 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005065 struct rpc_message msg = {
5066 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5067 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005068 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005069 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005070 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005071 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005072
5073 if (!nfs4_server_supports_acls(server))
5074 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005075 if (npages > ARRAY_SIZE(pages))
5076 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005077 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005078 if (i < 0)
5079 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005080 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005081 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005082
5083 /*
5084 * Free each page after tx, so the only ref left is
5085 * held by the network stack
5086 */
5087 for (; i > 0; i--)
5088 put_page(pages[i-1]);
5089
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005090 /*
5091 * Acl update can result in inode attribute update.
5092 * so mark the attribute cache invalid.
5093 */
5094 spin_lock(&inode->i_lock);
5095 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5096 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005097 nfs_access_zap_cache(inode);
5098 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005099 return ret;
5100}
5101
Trond Myklebust16b42892006-08-24 12:27:15 -04005102static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5103{
5104 struct nfs4_exception exception = { };
5105 int err;
5106 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005107 err = __nfs4_proc_set_acl(inode, buf, buflen);
5108 trace_nfs4_set_acl(inode, err);
5109 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005110 &exception);
5111 } while (exception.retry);
5112 return err;
5113}
5114
David Quigleyaa9c2662013-05-22 12:50:44 -04005115#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5116static int _nfs4_get_security_label(struct inode *inode, void *buf,
5117 size_t buflen)
5118{
5119 struct nfs_server *server = NFS_SERVER(inode);
5120 struct nfs_fattr fattr;
5121 struct nfs4_label label = {0, 0, buflen, buf};
5122
5123 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005124 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005125 .fh = NFS_FH(inode),
5126 .bitmask = bitmask,
5127 };
5128 struct nfs4_getattr_res res = {
5129 .fattr = &fattr,
5130 .label = &label,
5131 .server = server,
5132 };
5133 struct rpc_message msg = {
5134 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005135 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005136 .rpc_resp = &res,
5137 };
5138 int ret;
5139
5140 nfs_fattr_init(&fattr);
5141
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005142 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005143 if (ret)
5144 return ret;
5145 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5146 return -ENOENT;
5147 if (buflen < label.len)
5148 return -ERANGE;
5149 return 0;
5150}
5151
5152static int nfs4_get_security_label(struct inode *inode, void *buf,
5153 size_t buflen)
5154{
5155 struct nfs4_exception exception = { };
5156 int err;
5157
5158 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5159 return -EOPNOTSUPP;
5160
5161 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005162 err = _nfs4_get_security_label(inode, buf, buflen);
5163 trace_nfs4_get_security_label(inode, err);
5164 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005165 &exception);
5166 } while (exception.retry);
5167 return err;
5168}
5169
5170static int _nfs4_do_set_security_label(struct inode *inode,
5171 struct nfs4_label *ilabel,
5172 struct nfs_fattr *fattr,
5173 struct nfs4_label *olabel)
5174{
5175
5176 struct iattr sattr = {0};
5177 struct nfs_server *server = NFS_SERVER(inode);
5178 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005179 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005180 .fh = NFS_FH(inode),
5181 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005182 .server = server,
5183 .bitmask = bitmask,
5184 .label = ilabel,
5185 };
5186 struct nfs_setattrres res = {
5187 .fattr = fattr,
5188 .label = olabel,
5189 .server = server,
5190 };
5191 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005192 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5193 .rpc_argp = &arg,
5194 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005195 };
5196 int status;
5197
Jeff Layton12207f62013-11-01 10:49:32 -04005198 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005199
Jeff Layton12207f62013-11-01 10:49:32 -04005200 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005201 if (status)
5202 dprintk("%s failed: %d\n", __func__, status);
5203
5204 return status;
5205}
5206
5207static int nfs4_do_set_security_label(struct inode *inode,
5208 struct nfs4_label *ilabel,
5209 struct nfs_fattr *fattr,
5210 struct nfs4_label *olabel)
5211{
5212 struct nfs4_exception exception = { };
5213 int err;
5214
5215 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005216 err = _nfs4_do_set_security_label(inode, ilabel,
5217 fattr, olabel);
5218 trace_nfs4_set_security_label(inode, err);
5219 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005220 &exception);
5221 } while (exception.retry);
5222 return err;
5223}
5224
5225static int
Al Viro59301222016-05-27 10:19:30 -04005226nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005227{
5228 struct nfs4_label ilabel, *olabel = NULL;
5229 struct nfs_fattr fattr;
5230 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005231 int status;
5232
5233 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5234 return -EOPNOTSUPP;
5235
5236 nfs_fattr_init(&fattr);
5237
5238 ilabel.pi = 0;
5239 ilabel.lfs = 0;
5240 ilabel.label = (char *)buf;
5241 ilabel.len = buflen;
5242
5243 cred = rpc_lookup_cred();
5244 if (IS_ERR(cred))
5245 return PTR_ERR(cred);
5246
5247 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5248 if (IS_ERR(olabel)) {
5249 status = -PTR_ERR(olabel);
5250 goto out;
5251 }
5252
5253 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5254 if (status == 0)
5255 nfs_setsecurity(inode, &fattr, olabel);
5256
5257 nfs4_label_free(olabel);
5258out:
5259 put_rpccred(cred);
5260 return status;
5261}
5262#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5263
5264
Chuck Leverf0920752012-05-21 22:45:41 -04005265static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5266 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005267{
5268 __be32 verf[2];
5269
Chuck Lever2c820d92012-05-21 22:45:33 -04005270 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5271 /* An impossible timestamp guarantees this value
5272 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005273 verf[0] = cpu_to_be32(U32_MAX);
5274 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005275 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005276 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005277 u64 ns = ktime_to_ns(nn->boot_time);
5278
5279 verf[0] = cpu_to_be32(ns >> 32);
5280 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005281 }
Chuck Levercd937102012-03-02 17:14:31 -05005282 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5283}
5284
Jeff Laytona3192682015-06-09 19:43:59 -04005285static int
5286nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005287{
Jeff Laytona3192682015-06-09 19:43:59 -04005288 size_t len;
5289 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005290
Trond Myklebustceb3a162015-01-03 15:16:04 -05005291 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005292 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005293
Jeff Laytona3192682015-06-09 19:43:59 -04005294 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005295 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005296 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5297 1 +
5298 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5299 1;
5300 rcu_read_unlock();
5301
5302 if (len > NFS4_OPAQUE_LIMIT + 1)
5303 return -EINVAL;
5304
5305 /*
5306 * Since this string is allocated at mount time, and held until the
5307 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5308 * about a memory-reclaim deadlock.
5309 */
5310 str = kmalloc(len, GFP_KERNEL);
5311 if (!str)
5312 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005313
Chuck Levere984a552012-09-14 17:24:21 -04005314 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005315 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005316 clp->cl_ipaddr,
5317 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5318 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005319 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005320
Jeff Laytona3192682015-06-09 19:43:59 -04005321 clp->cl_owner_id = str;
5322 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005323}
5324
Jeff Layton873e3852015-06-09 19:44:00 -04005325static int
5326nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005327{
Jeff Layton873e3852015-06-09 19:44:00 -04005328 size_t len;
5329 char *str;
5330
5331 len = 10 + 10 + 1 + 10 + 1 +
5332 strlen(nfs4_client_id_uniquifier) + 1 +
5333 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5334
5335 if (len > NFS4_OPAQUE_LIMIT + 1)
5336 return -EINVAL;
5337
5338 /*
5339 * Since this string is allocated at mount time, and held until the
5340 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5341 * about a memory-reclaim deadlock.
5342 */
5343 str = kmalloc(len, GFP_KERNEL);
5344 if (!str)
5345 return -ENOMEM;
5346
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005347 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005348 clp->rpc_ops->version, clp->cl_minorversion,
5349 nfs4_client_id_uniquifier,
5350 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005351 clp->cl_owner_id = str;
5352 return 0;
5353}
5354
5355static int
5356nfs4_init_uniform_client_string(struct nfs_client *clp)
5357{
Jeff Layton873e3852015-06-09 19:44:00 -04005358 size_t len;
5359 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005360
5361 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005362 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005363
5364 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005365 return nfs4_init_uniquifier_client_string(clp);
5366
5367 len = 10 + 10 + 1 + 10 + 1 +
5368 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5369
5370 if (len > NFS4_OPAQUE_LIMIT + 1)
5371 return -EINVAL;
5372
5373 /*
5374 * Since this string is allocated at mount time, and held until the
5375 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5376 * about a memory-reclaim deadlock.
5377 */
5378 str = kmalloc(len, GFP_KERNEL);
5379 if (!str)
5380 return -ENOMEM;
5381
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005382 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005383 clp->rpc_ops->version, clp->cl_minorversion,
5384 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005385 clp->cl_owner_id = str;
5386 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005387}
5388
Chuck Lever706cb8d2014-03-12 12:51:47 -04005389/*
5390 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5391 * services. Advertise one based on the address family of the
5392 * clientaddr.
5393 */
5394static unsigned int
5395nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5396{
5397 if (strchr(clp->cl_ipaddr, ':') != NULL)
5398 return scnprintf(buf, len, "tcp6");
5399 else
5400 return scnprintf(buf, len, "tcp");
5401}
5402
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005403static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5404{
5405 struct nfs4_setclientid *sc = calldata;
5406
5407 if (task->tk_status == 0)
5408 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5409}
5410
5411static const struct rpc_call_ops nfs4_setclientid_ops = {
5412 .rpc_call_done = nfs4_setclientid_done,
5413};
5414
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005415/**
5416 * nfs4_proc_setclientid - Negotiate client ID
5417 * @clp: state data structure
5418 * @program: RPC program for NFSv4 callback service
5419 * @port: IP port number for NFS4 callback service
5420 * @cred: RPC credential to use for this call
5421 * @res: where to place the result
5422 *
5423 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5424 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005425int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5426 unsigned short port, struct rpc_cred *cred,
5427 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428{
5429 nfs4_verifier sc_verifier;
5430 struct nfs4_setclientid setclientid = {
5431 .sc_verifier = &sc_verifier,
5432 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005433 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 };
5435 struct rpc_message msg = {
5436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5437 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005438 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005439 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005441 struct rpc_task *task;
5442 struct rpc_task_setup task_setup_data = {
5443 .rpc_client = clp->cl_rpcclient,
5444 .rpc_message = &msg,
5445 .callback_ops = &nfs4_setclientid_ops,
5446 .callback_data = &setclientid,
5447 .flags = RPC_TASK_TIMEOUT,
5448 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005449 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450
Chuck Leverde734832012-07-11 16:30:50 -04005451 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005452 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005453
5454 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5455 status = nfs4_init_uniform_client_string(clp);
5456 else
Jeff Laytona3192682015-06-09 19:43:59 -04005457 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005458
5459 if (status)
5460 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005461
Chuck Leverde734832012-07-11 16:30:50 -04005462 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005463 setclientid.sc_netid_len =
5464 nfs4_init_callback_netid(clp,
5465 setclientid.sc_netid,
5466 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005467 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005468 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 clp->cl_ipaddr, port >> 8, port & 255);
5470
Jeff Layton3a6bb732015-06-09 19:43:57 -04005471 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005472 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005473 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005474 task = rpc_run_task(&task_setup_data);
5475 if (IS_ERR(task)) {
5476 status = PTR_ERR(task);
5477 goto out;
5478 }
5479 status = task->tk_status;
5480 if (setclientid.sc_cred) {
5481 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5482 put_rpccred(setclientid.sc_cred);
5483 }
5484 rpc_put_task(task);
5485out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005486 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005487 dprintk("NFS reply setclientid: %d\n", status);
5488 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489}
5490
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005491/**
5492 * nfs4_proc_setclientid_confirm - Confirm client ID
5493 * @clp: state data structure
5494 * @res: result of a previous SETCLIENTID
5495 * @cred: RPC credential to use for this call
5496 *
5497 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5498 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005499int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005500 struct nfs4_setclientid_res *arg,
5501 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 struct rpc_message msg = {
5504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005505 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005506 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 int status;
5509
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005510 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5511 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5512 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005513 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005514 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005515 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 return status;
5517}
5518
Trond Myklebustfe650402006-01-03 09:55:18 +01005519struct nfs4_delegreturndata {
5520 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005521 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005522 struct nfs_fh fh;
5523 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005524 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005525 struct {
5526 struct nfs4_layoutreturn_args arg;
5527 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005528 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005529 u32 roc_barrier;
5530 bool roc;
5531 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005532 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005533 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005534 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005535};
5536
Trond Myklebustfe650402006-01-03 09:55:18 +01005537static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5538{
5539 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005540
Trond Myklebust14516c32010-07-31 14:29:06 -04005541 if (!nfs4_sequence_done(task, &data->res.seq_res))
5542 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005543
Trond Myklebustca8acf82013-08-13 10:36:56 -04005544 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005545
5546 /* Handle Layoutreturn errors */
5547 if (data->args.lr_args && task->tk_status != 0) {
5548 switch(data->res.lr_ret) {
5549 default:
5550 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5551 break;
5552 case 0:
5553 data->args.lr_args = NULL;
5554 data->res.lr_res = NULL;
5555 break;
5556 case -NFS4ERR_ADMIN_REVOKED:
5557 case -NFS4ERR_DELEG_REVOKED:
5558 case -NFS4ERR_EXPIRED:
5559 case -NFS4ERR_BAD_STATEID:
5560 case -NFS4ERR_OLD_STATEID:
5561 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5562 case -NFS4ERR_WRONG_CRED:
5563 data->args.lr_args = NULL;
5564 data->res.lr_res = NULL;
5565 data->res.lr_ret = 0;
5566 rpc_restart_call_prepare(task);
5567 return;
5568 }
5569 }
5570
Ricardo Labiaga79708862009-12-07 09:23:21 -05005571 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005572 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005573 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005574 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005575 case -NFS4ERR_ADMIN_REVOKED:
5576 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005577 case -NFS4ERR_EXPIRED:
5578 nfs4_free_revoked_stateid(data->res.server,
5579 data->args.stateid,
5580 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005581 case -NFS4ERR_BAD_STATEID:
5582 case -NFS4ERR_OLD_STATEID:
5583 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005584 task->tk_status = 0;
5585 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005586 case -NFS4ERR_ACCESS:
5587 if (data->args.bitmask) {
5588 data->args.bitmask = NULL;
5589 data->res.fattr = NULL;
5590 task->tk_status = 0;
5591 rpc_restart_call_prepare(task);
5592 return;
5593 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005594 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005595 if (nfs4_async_handle_error(task, data->res.server,
5596 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005597 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005598 return;
5599 }
5600 }
5601 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005602}
5603
5604static void nfs4_delegreturn_release(void *calldata)
5605{
Peng Tao039b7562014-07-03 13:05:02 +08005606 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005607 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005608
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005609 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005610 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005611 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5612 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005613 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005614 nfs_iput_and_deactive(inode);
5615 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005616 kfree(calldata);
5617}
5618
Andy Adamson938e1012009-04-01 09:22:28 -04005619static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5620{
5621 struct nfs4_delegreturndata *d_data;
5622
5623 d_data = (struct nfs4_delegreturndata *)data;
5624
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005625 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005626 return;
5627
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005628 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005629 &d_data->args.seq_args,
5630 &d_data->res.seq_res,
5631 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005632}
Andy Adamson938e1012009-04-01 09:22:28 -04005633
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005634static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005635 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005636 .rpc_call_done = nfs4_delegreturn_done,
5637 .rpc_release = nfs4_delegreturn_release,
5638};
5639
Trond Myklebuste6f81072008-01-24 18:14:34 -05005640static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005641{
5642 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005643 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005644 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005645 struct rpc_message msg = {
5646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5647 .rpc_cred = cred,
5648 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005649 struct rpc_task_setup task_setup_data = {
5650 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005651 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005652 .callback_ops = &nfs4_delegreturn_ops,
5653 .flags = RPC_TASK_ASYNC,
5654 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005655 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005656
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005657 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005658 if (data == NULL)
5659 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005660 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005661
5662 nfs4_state_protect(server->nfs_client,
5663 NFS_SP4_MACH_CRED_CLEANUP,
5664 &task_setup_data.rpc_client, &msg);
5665
Trond Myklebustfe650402006-01-03 09:55:18 +01005666 data->args.fhandle = &data->fh;
5667 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005668 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005669 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005670 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005671 data->res.fattr = &data->fattr;
5672 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005673 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005674 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005675 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005676 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005677 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005678 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005679 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005680 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005681 if (data->lr.roc) {
5682 data->args.lr_args = &data->lr.arg;
5683 data->res.lr_res = &data->lr.res;
5684 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005685 } else if (data->lr.roc) {
5686 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5687 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005688 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005689
Trond Myklebustc970aa82007-07-14 15:39:59 -04005690 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005691 msg.rpc_argp = &data->args;
5692 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005693 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005694 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005695 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005696 if (!issync)
5697 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005698 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005699 if (status != 0)
5700 goto out;
5701 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005702out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005703 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005704 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705}
5706
Trond Myklebuste6f81072008-01-24 18:14:34 -05005707int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708{
5709 struct nfs_server *server = NFS_SERVER(inode);
5710 struct nfs4_exception exception = { };
5711 int err;
5712 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005713 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005714 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 switch (err) {
5716 case -NFS4ERR_STALE_STATEID:
5717 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 case 0:
5719 return 0;
5720 }
5721 err = nfs4_handle_exception(server, err, &exception);
5722 } while (exception.retry);
5723 return err;
5724}
5725
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5727{
5728 struct inode *inode = state->inode;
5729 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005730 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005731 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005733 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005735 struct nfs_lockt_res res = {
5736 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 };
5738 struct rpc_message msg = {
5739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005740 .rpc_argp = &arg,
5741 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 .rpc_cred = state->owner->so_cred,
5743 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 struct nfs4_lock_state *lsp;
5745 int status;
5746
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005747 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005748 status = nfs4_set_lock_state(state, request);
5749 if (status != 0)
5750 goto out;
5751 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005752 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005753 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005754 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005755 switch (status) {
5756 case 0:
5757 request->fl_type = F_UNLCK;
5758 break;
5759 case -NFS4ERR_DENIED:
5760 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005762 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005763 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005764out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 return status;
5766}
5767
5768static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5769{
5770 struct nfs4_exception exception = { };
5771 int err;
5772
5773 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005774 err = _nfs4_proc_getlk(state, cmd, request);
5775 trace_nfs4_get_lock(request, state, cmd, err);
5776 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 &exception);
5778 } while (exception.retry);
5779 return err;
5780}
5781
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005782struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005783 struct nfs_locku_args arg;
5784 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005785 struct nfs4_lock_state *lsp;
5786 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005787 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005788 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005789 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005790 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005791};
5792
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005793static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5794 struct nfs_open_context *ctx,
5795 struct nfs4_lock_state *lsp,
5796 struct nfs_seqid *seqid)
5797{
5798 struct nfs4_unlockdata *p;
5799 struct inode *inode = lsp->ls_state->inode;
5800
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005801 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005802 if (p == NULL)
5803 return NULL;
5804 p->arg.fh = NFS_FH(inode);
5805 p->arg.fl = &p->fl;
5806 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005807 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005808 p->lsp = lsp;
5809 atomic_inc(&lsp->ls_count);
5810 /* Ensure we don't close file until we're done freeing locks! */
5811 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005812 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005813 memcpy(&p->fl, fl, sizeof(p->fl));
5814 p->server = NFS_SERVER(inode);
5815 return p;
5816}
5817
Trond Myklebust06f814a2006-01-03 09:55:07 +01005818static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005819{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005820 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005821 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005822 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005823 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005824 put_nfs_open_context(calldata->ctx);
5825 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005826}
5827
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005828static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005829{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005830 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005831
Trond Myklebust14516c32010-07-31 14:29:06 -04005832 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5833 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005834 switch (task->tk_status) {
5835 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005836 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005837 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005838 if (nfs4_update_lock_stateid(calldata->lsp,
5839 &calldata->res.stateid))
5840 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005841 case -NFS4ERR_ADMIN_REVOKED:
5842 case -NFS4ERR_EXPIRED:
5843 nfs4_free_revoked_stateid(calldata->server,
5844 &calldata->arg.stateid,
5845 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005846 case -NFS4ERR_BAD_STATEID:
5847 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005848 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005849 if (!nfs4_stateid_match(&calldata->arg.stateid,
5850 &calldata->lsp->ls_stateid))
5851 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005852 break;
5853 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005854 if (nfs4_async_handle_error(task, calldata->server,
5855 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005856 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005857 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005858 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005859}
5860
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005861static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005862{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005863 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005865 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
5866 nfs_async_iocounter_wait(task, calldata->l_ctx))
5867 return;
5868
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005869 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005870 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005871 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005872 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005873 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005874 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005875 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005876 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005877 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04005878 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005879 &calldata->res.seq_res,
5880 task) != 0)
5881 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005882 return;
5883out_no_action:
5884 task->tk_action = NULL;
5885out_wait:
5886 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887}
5888
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005889static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005890 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005891 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005892 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005893};
5894
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005895static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5896 struct nfs_open_context *ctx,
5897 struct nfs4_lock_state *lsp,
5898 struct nfs_seqid *seqid)
5899{
5900 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005901 struct rpc_message msg = {
5902 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5903 .rpc_cred = ctx->cred,
5904 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005905 struct rpc_task_setup task_setup_data = {
5906 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005907 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005908 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005909 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005910 .flags = RPC_TASK_ASYNC,
5911 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005912
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005913 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5914 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5915
Frank Filz137d6ac2007-07-09 15:32:29 -07005916 /* Ensure this is an unlock - when canceling a lock, the
5917 * canceled lock is passed in, and it won't be an unlock.
5918 */
5919 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005920 if (fl->fl_flags & FL_CLOSE)
5921 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07005922
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005923 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5924 if (data == NULL) {
5925 nfs_free_seqid(seqid);
5926 return ERR_PTR(-ENOMEM);
5927 }
5928
Chuck Levera9c92d62013-08-09 12:48:18 -04005929 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005930 msg.rpc_argp = &data->arg;
5931 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005932 task_setup_data.callback_data = data;
5933 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005934}
5935
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5937{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005938 struct inode *inode = state->inode;
5939 struct nfs4_state_owner *sp = state->owner;
5940 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005941 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005942 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005943 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005944 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005945 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005946 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947
Trond Myklebust9b073572006-06-29 16:38:34 -04005948 status = nfs4_set_lock_state(state, request);
5949 /* Unlock _before_ we do the RPC call */
5950 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005951 /* Exclude nfs_delegation_claim_locks() */
5952 mutex_lock(&sp->so_delegreturn_mutex);
5953 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005954 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005955 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005956 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005957 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005958 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005959 }
5960 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005961 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005962 if (status != 0)
5963 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005964 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005965 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005966 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5967 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005968 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5969 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005970 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005971 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005972 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005973 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005974 status = PTR_ERR(task);
5975 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005976 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005977 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005978 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005979out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005980 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005981 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005982 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983}
5984
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005985struct nfs4_lockdata {
5986 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005987 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005988 struct nfs4_lock_state *lsp;
5989 struct nfs_open_context *ctx;
5990 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005991 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005992 int rpc_status;
5993 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005994 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005995};
5996
5997static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005998 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5999 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006000{
6001 struct nfs4_lockdata *p;
6002 struct inode *inode = lsp->ls_state->inode;
6003 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006004 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006005
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006006 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006007 if (p == NULL)
6008 return NULL;
6009
6010 p->arg.fh = NFS_FH(inode);
6011 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006012 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006013 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006014 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006015 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6016 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006017 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006018 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006019 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006020 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006021 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006022 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006023 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006024 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006025 atomic_inc(&lsp->ls_count);
6026 p->ctx = get_nfs_open_context(ctx);
6027 memcpy(&p->fl, fl, sizeof(p->fl));
6028 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006029out_free_seqid:
6030 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006031out_free:
6032 kfree(p);
6033 return NULL;
6034}
6035
6036static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6037{
6038 struct nfs4_lockdata *data = calldata;
6039 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040
Harvey Harrison3110ff82008-05-02 13:42:44 -07006041 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006042 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006043 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006044 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006045 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006046 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006047 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006048 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006049 nfs4_stateid_copy(&data->arg.open_stateid,
6050 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006051 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006052 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006053 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006054 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006055 nfs4_stateid_copy(&data->arg.lock_stateid,
6056 &data->lsp->ls_stateid);
6057 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006058 if (!nfs4_valid_open_stateid(state)) {
6059 data->rpc_status = -EBADF;
6060 task->tk_action = NULL;
6061 goto out_release_open_seqid;
6062 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006063 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006064 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168a2010-06-16 09:52:26 -04006065 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006066 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006067 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006068 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006069out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006070 nfs_release_seqid(data->arg.open_seqid);
6071out_release_lock_seqid:
6072 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006073out_wait:
6074 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006075 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006076}
6077
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006078static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6079{
6080 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006081 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006082
Harvey Harrison3110ff82008-05-02 13:42:44 -07006083 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006084
Trond Myklebust14516c32010-07-31 14:29:06 -04006085 if (!nfs4_sequence_done(task, &data->res.seq_res))
6086 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006087
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006088 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006089 switch (task->tk_status) {
6090 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006091 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006092 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006093 if (data->arg.new_lock) {
6094 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006095 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006096 rpc_restart_call_prepare(task);
6097 break;
6098 }
6099 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006100 if (data->arg.new_lock_owner != 0) {
6101 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6102 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6103 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6104 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6105 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006106 break;
6107 case -NFS4ERR_BAD_STATEID:
6108 case -NFS4ERR_OLD_STATEID:
6109 case -NFS4ERR_STALE_STATEID:
6110 case -NFS4ERR_EXPIRED:
6111 if (data->arg.new_lock_owner != 0) {
6112 if (!nfs4_stateid_match(&data->arg.open_stateid,
6113 &lsp->ls_state->open_stateid))
6114 rpc_restart_call_prepare(task);
6115 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6116 &lsp->ls_stateid))
6117 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006118 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006119 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006120}
6121
6122static void nfs4_lock_release(void *calldata)
6123{
6124 struct nfs4_lockdata *data = calldata;
6125
Harvey Harrison3110ff82008-05-02 13:42:44 -07006126 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006127 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006128 if (data->cancelled != 0) {
6129 struct rpc_task *task;
6130 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6131 data->arg.lock_seqid);
6132 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006133 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006134 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006135 } else
6136 nfs_free_seqid(data->arg.lock_seqid);
6137 nfs4_put_lock_state(data->lsp);
6138 put_nfs_open_context(data->ctx);
6139 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006140 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006141}
6142
6143static const struct rpc_call_ops nfs4_lock_ops = {
6144 .rpc_call_prepare = nfs4_lock_prepare,
6145 .rpc_call_done = nfs4_lock_done,
6146 .rpc_release = nfs4_lock_release,
6147};
6148
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006149static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6150{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006151 switch (error) {
6152 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006153 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006154 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006155 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006156 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006157 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006158 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006159 break;
6160 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006161 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006162 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006163 };
6164}
6165
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006166static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006167{
6168 struct nfs4_lockdata *data;
6169 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006170 struct rpc_message msg = {
6171 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6172 .rpc_cred = state->owner->so_cred,
6173 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006174 struct rpc_task_setup task_setup_data = {
6175 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006176 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006177 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006178 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006179 .flags = RPC_TASK_ASYNC,
6180 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006181 int ret;
6182
Harvey Harrison3110ff82008-05-02 13:42:44 -07006183 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006184 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006185 fl->fl_u.nfs4_fl.owner,
6186 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006187 if (data == NULL)
6188 return -ENOMEM;
6189 if (IS_SETLKW(cmd))
6190 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006191 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006192 msg.rpc_argp = &data->arg;
6193 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006194 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006195 if (recovery_type > NFS_LOCK_NEW) {
6196 if (recovery_type == NFS_LOCK_RECLAIM)
6197 data->arg.reclaim = NFS_LOCK_RECLAIM;
6198 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006199 } else
6200 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006201 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006202 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006203 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006204 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006205 if (ret == 0) {
6206 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006207 if (ret)
6208 nfs4_handle_setlk_error(data->server, data->lsp,
6209 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006210 } else
6211 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006212 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006213 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006214 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006215 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216}
6217
6218static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6219{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006220 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006221 struct nfs4_exception exception = {
6222 .inode = state->inode,
6223 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006224 int err;
6225
6226 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006227 /* Cache the lock if possible... */
6228 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6229 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006230 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006231 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006232 break;
6233 nfs4_handle_exception(server, err, &exception);
6234 } while (exception.retry);
6235 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236}
6237
6238static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6239{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006240 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006241 struct nfs4_exception exception = {
6242 .inode = state->inode,
6243 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006244 int err;
6245
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006246 err = nfs4_set_lock_state(state, request);
6247 if (err != 0)
6248 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006249 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006250 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6251 return 0;
6252 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006253 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006254 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6255 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006256 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006257 switch (err) {
6258 default:
6259 goto out;
6260 case -NFS4ERR_GRACE:
6261 case -NFS4ERR_DELAY:
6262 nfs4_handle_exception(server, err, &exception);
6263 err = 0;
6264 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006265 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006266out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006267 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268}
6269
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006270#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006271static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6272{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006273 struct nfs4_lock_state *lsp;
6274 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006275
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006276 status = nfs4_set_lock_state(state, request);
6277 if (status != 0)
6278 return status;
6279 lsp = request->fl_u.nfs4_fl.owner;
6280 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6281 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6282 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006283 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006284}
6285#endif
6286
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6288{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006289 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006290 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006291 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006292 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293
Trond Myklebust01c3b862006-06-29 16:38:39 -04006294 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006295 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006296 if (status < 0)
6297 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006298 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006299 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006300 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006301 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006302 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006303 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006304 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006305 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006306 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006307 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006308 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006309 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006310 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006311 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006312out:
6313 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 return status;
6315}
6316
6317static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6318{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006319 struct nfs4_exception exception = {
6320 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006321 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006322 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323 int err;
6324
6325 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006326 err = _nfs4_proc_setlk(state, cmd, request);
6327 if (err == -NFS4ERR_DENIED)
6328 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006330 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331 } while (exception.retry);
6332 return err;
6333}
6334
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006335#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6336#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6337
6338static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006339nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6340 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006341{
6342 int status = -ERESTARTSYS;
6343 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6344
6345 while(!signalled()) {
6346 status = nfs4_proc_setlk(state, cmd, request);
6347 if ((status != -EAGAIN) || IS_SETLK(cmd))
6348 break;
6349 freezable_schedule_timeout_interruptible(timeout);
6350 timeout *= 2;
6351 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6352 status = -ERESTARTSYS;
6353 }
6354 return status;
6355}
6356
Jeff Laytona1d617d82016-09-17 18:17:39 -04006357#ifdef CONFIG_NFS_V4_1
6358struct nfs4_lock_waiter {
6359 struct task_struct *task;
6360 struct inode *inode;
6361 struct nfs_lowner *owner;
6362 bool notified;
6363};
6364
6365static int
6366nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6367{
6368 int ret;
6369 struct cb_notify_lock_args *cbnl = key;
6370 struct nfs4_lock_waiter *waiter = wait->private;
6371 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6372 *wowner = waiter->owner;
6373
6374 /* Only wake if the callback was for the same owner */
6375 if (lowner->clientid != wowner->clientid ||
6376 lowner->id != wowner->id ||
6377 lowner->s_dev != wowner->s_dev)
6378 return 0;
6379
6380 /* Make sure it's for the right inode */
6381 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6382 return 0;
6383
6384 waiter->notified = true;
6385
6386 /* override "private" so we can use default_wake_function */
6387 wait->private = waiter->task;
6388 ret = autoremove_wake_function(wait, mode, flags, key);
6389 wait->private = waiter;
6390 return ret;
6391}
6392
6393static int
6394nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6395{
6396 int status = -ERESTARTSYS;
6397 unsigned long flags;
6398 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6399 struct nfs_server *server = NFS_SERVER(state->inode);
6400 struct nfs_client *clp = server->nfs_client;
6401 wait_queue_head_t *q = &clp->cl_lock_waitq;
6402 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6403 .id = lsp->ls_seqid.owner_id,
6404 .s_dev = server->s_dev };
6405 struct nfs4_lock_waiter waiter = { .task = current,
6406 .inode = state->inode,
6407 .owner = &owner,
6408 .notified = false };
6409 wait_queue_t wait;
6410
6411 /* Don't bother with waitqueue if we don't expect a callback */
6412 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6413 return nfs4_retry_setlk_simple(state, cmd, request);
6414
6415 init_wait(&wait);
6416 wait.private = &waiter;
6417 wait.func = nfs4_wake_lock_waiter;
6418 add_wait_queue(q, &wait);
6419
6420 while(!signalled()) {
6421 status = nfs4_proc_setlk(state, cmd, request);
6422 if ((status != -EAGAIN) || IS_SETLK(cmd))
6423 break;
6424
6425 status = -ERESTARTSYS;
6426 spin_lock_irqsave(&q->lock, flags);
6427 if (waiter.notified) {
6428 spin_unlock_irqrestore(&q->lock, flags);
6429 continue;
6430 }
6431 set_current_state(TASK_INTERRUPTIBLE);
6432 spin_unlock_irqrestore(&q->lock, flags);
6433
6434 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6435 }
6436
6437 finish_wait(q, &wait);
6438 return status;
6439}
6440#else /* !CONFIG_NFS_V4_1 */
6441static inline int
6442nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6443{
6444 return nfs4_retry_setlk_simple(state, cmd, request);
6445}
6446#endif
6447
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448static int
6449nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6450{
6451 struct nfs_open_context *ctx;
6452 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 int status;
6454
6455 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006456 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457 state = ctx->state;
6458
Trond Myklebustd9531262009-07-21 19:22:38 -04006459 if (IS_GETLK(cmd)) {
6460 if (state != NULL)
6461 return nfs4_proc_getlk(state, F_GETLK, request);
6462 return 0;
6463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464
6465 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6466 return -EINVAL;
6467
Trond Myklebustd9531262009-07-21 19:22:38 -04006468 if (request->fl_type == F_UNLCK) {
6469 if (state != NULL)
6470 return nfs4_proc_unlck(state, cmd, request);
6471 return 0;
6472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473
Trond Myklebustd9531262009-07-21 19:22:38 -04006474 if (state == NULL)
6475 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006476
6477 if ((request->fl_flags & FL_POSIX) &&
6478 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6479 return -ENOLCK;
6480
Jeff Layton1ea67db2016-09-17 18:17:37 -04006481 status = nfs4_set_lock_state(state, request);
6482 if (status != 0)
6483 return status;
6484
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006485 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486}
6487
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006488int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006489{
6490 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006491 int err;
6492
6493 err = nfs4_set_lock_state(state, fl);
6494 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006495 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006496 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006497 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006498}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006499
Trond Myklebustcf470c32012-03-07 13:49:12 -05006500struct nfs_release_lockowner_data {
6501 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006502 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006503 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006504 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006505 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006506};
6507
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006508static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6509{
6510 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006511 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006512 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6513 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006514 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006515 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006516}
6517
6518static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6519{
6520 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006521 struct nfs_server *server = data->server;
6522
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006523 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006524
6525 switch (task->tk_status) {
6526 case 0:
6527 renew_lease(server, data->timestamp);
6528 break;
6529 case -NFS4ERR_STALE_CLIENTID:
6530 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006531 nfs4_schedule_lease_recovery(server->nfs_client);
6532 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006533 case -NFS4ERR_LEASE_MOVED:
6534 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006535 if (nfs4_async_handle_error(task, server,
6536 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006537 rpc_restart_call_prepare(task);
6538 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006539}
6540
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006541static void nfs4_release_lockowner_release(void *calldata)
6542{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006543 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006544 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006545 kfree(calldata);
6546}
6547
Trond Myklebust17280172012-03-11 13:11:00 -04006548static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006549 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6550 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006551 .rpc_release = nfs4_release_lockowner_release,
6552};
6553
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006554static void
6555nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006556{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006557 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006558 struct rpc_message msg = {
6559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6560 };
6561
6562 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006563 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006564
Trond Myklebustcf470c32012-03-07 13:49:12 -05006565 data = kmalloc(sizeof(*data), GFP_NOFS);
6566 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006567 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006568 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006569 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006570 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6571 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6572 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006573
Trond Myklebustcf470c32012-03-07 13:49:12 -05006574 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006575 msg.rpc_resp = &data->res;
6576 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006577 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006578}
6579
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006580#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6581
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006582static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006583 struct dentry *unused, struct inode *inode,
6584 const char *key, const void *buf,
6585 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006586{
Al Viro59301222016-05-27 10:19:30 -04006587 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006588}
6589
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006590static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006591 struct dentry *unused, struct inode *inode,
6592 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006593{
Al Virob2968212016-04-10 20:48:24 -04006594 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006595}
6596
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006597static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006598{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006599 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006600}
6601
David Quigleyc9bccef2013-05-22 12:50:45 -04006602#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006603
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006604static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006605 struct dentry *unused, struct inode *inode,
6606 const char *key, const void *buf,
6607 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006608{
6609 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006610 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006611
6612 return -EOPNOTSUPP;
6613}
6614
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006615static int nfs4_xattr_get_nfs4_label(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)
David Quigleyc9bccef2013-05-22 12:50:45 -04006618{
6619 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006620 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006621 return -EOPNOTSUPP;
6622}
6623
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006624static ssize_t
6625nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006626{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006627 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006628
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006629 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6630 len = security_inode_listsecurity(inode, list, list_len);
6631 if (list_len && len > list_len)
6632 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006633 }
6634 return len;
6635}
6636
6637static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6638 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006639 .get = nfs4_xattr_get_nfs4_label,
6640 .set = nfs4_xattr_set_nfs4_label,
6641};
David Quigleyc9bccef2013-05-22 12:50:45 -04006642
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006643#else
6644
6645static ssize_t
6646nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6647{
6648 return 0;
6649}
6650
6651#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006652
Andy Adamson533eb462011-06-13 18:25:56 -04006653/*
6654 * nfs_fhget will use either the mounted_on_fileid or the fileid
6655 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006656static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6657{
Andy Adamson533eb462011-06-13 18:25:56 -04006658 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6659 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6660 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006661 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006662 return;
6663
6664 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006665 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006666 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6667 fattr->nlink = 2;
6668}
6669
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006670static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6671 const struct qstr *name,
6672 struct nfs4_fs_locations *fs_locations,
6673 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006674{
6675 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006676 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006677 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006678 };
6679 struct nfs4_fs_locations_arg args = {
6680 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006681 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006682 .page = page,
6683 .bitmask = bitmask,
6684 };
Benny Halevy22958462009-04-01 09:22:02 -04006685 struct nfs4_fs_locations_res res = {
6686 .fs_locations = fs_locations,
6687 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006688 struct rpc_message msg = {
6689 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6690 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006691 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006692 };
6693 int status;
6694
Harvey Harrison3110ff82008-05-02 13:42:44 -07006695 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006696
6697 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6698 * is not supported */
6699 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6700 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6701 else
6702 bitmask[0] |= FATTR4_WORD0_FILEID;
6703
Trond Myklebustc228fd32007-01-13 02:28:11 -05006704 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006705 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006706 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006707 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006708 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006709 return status;
6710}
6711
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006712int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6713 const struct qstr *name,
6714 struct nfs4_fs_locations *fs_locations,
6715 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006716{
6717 struct nfs4_exception exception = { };
6718 int err;
6719 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006720 err = _nfs4_proc_fs_locations(client, dir, name,
6721 fs_locations, page);
6722 trace_nfs4_get_fs_locations(dir, name, err);
6723 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006724 &exception);
6725 } while (exception.retry);
6726 return err;
6727}
6728
Chuck Leverb03d7352013-10-17 14:12:50 -04006729/*
6730 * This operation also signals the server that this client is
6731 * performing migration recovery. The server can stop returning
6732 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6733 * appended to this compound to identify the client ID which is
6734 * performing recovery.
6735 */
6736static int _nfs40_proc_get_locations(struct inode *inode,
6737 struct nfs4_fs_locations *locations,
6738 struct page *page, struct rpc_cred *cred)
6739{
6740 struct nfs_server *server = NFS_SERVER(inode);
6741 struct rpc_clnt *clnt = server->client;
6742 u32 bitmask[2] = {
6743 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6744 };
6745 struct nfs4_fs_locations_arg args = {
6746 .clientid = server->nfs_client->cl_clientid,
6747 .fh = NFS_FH(inode),
6748 .page = page,
6749 .bitmask = bitmask,
6750 .migration = 1, /* skip LOOKUP */
6751 .renew = 1, /* append RENEW */
6752 };
6753 struct nfs4_fs_locations_res res = {
6754 .fs_locations = locations,
6755 .migration = 1,
6756 .renew = 1,
6757 };
6758 struct rpc_message msg = {
6759 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6760 .rpc_argp = &args,
6761 .rpc_resp = &res,
6762 .rpc_cred = cred,
6763 };
6764 unsigned long now = jiffies;
6765 int status;
6766
6767 nfs_fattr_init(&locations->fattr);
6768 locations->server = server;
6769 locations->nlocations = 0;
6770
6771 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6772 nfs4_set_sequence_privileged(&args.seq_args);
6773 status = nfs4_call_sync_sequence(clnt, server, &msg,
6774 &args.seq_args, &res.seq_res);
6775 if (status)
6776 return status;
6777
6778 renew_lease(server, now);
6779 return 0;
6780}
6781
6782#ifdef CONFIG_NFS_V4_1
6783
6784/*
6785 * This operation also signals the server that this client is
6786 * performing migration recovery. The server can stop asserting
6787 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6788 * performing this operation is identified in the SEQUENCE
6789 * operation in this compound.
6790 *
6791 * When the client supports GETATTR(fs_locations_info), it can
6792 * be plumbed in here.
6793 */
6794static int _nfs41_proc_get_locations(struct inode *inode,
6795 struct nfs4_fs_locations *locations,
6796 struct page *page, struct rpc_cred *cred)
6797{
6798 struct nfs_server *server = NFS_SERVER(inode);
6799 struct rpc_clnt *clnt = server->client;
6800 u32 bitmask[2] = {
6801 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6802 };
6803 struct nfs4_fs_locations_arg args = {
6804 .fh = NFS_FH(inode),
6805 .page = page,
6806 .bitmask = bitmask,
6807 .migration = 1, /* skip LOOKUP */
6808 };
6809 struct nfs4_fs_locations_res res = {
6810 .fs_locations = locations,
6811 .migration = 1,
6812 };
6813 struct rpc_message msg = {
6814 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6815 .rpc_argp = &args,
6816 .rpc_resp = &res,
6817 .rpc_cred = cred,
6818 };
6819 int status;
6820
6821 nfs_fattr_init(&locations->fattr);
6822 locations->server = server;
6823 locations->nlocations = 0;
6824
6825 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6826 nfs4_set_sequence_privileged(&args.seq_args);
6827 status = nfs4_call_sync_sequence(clnt, server, &msg,
6828 &args.seq_args, &res.seq_res);
6829 if (status == NFS4_OK &&
6830 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6831 status = -NFS4ERR_LEASE_MOVED;
6832 return status;
6833}
6834
6835#endif /* CONFIG_NFS_V4_1 */
6836
6837/**
6838 * nfs4_proc_get_locations - discover locations for a migrated FSID
6839 * @inode: inode on FSID that is migrating
6840 * @locations: result of query
6841 * @page: buffer
6842 * @cred: credential to use for this operation
6843 *
6844 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6845 * operation failed, or a negative errno if a local error occurred.
6846 *
6847 * On success, "locations" is filled in, but if the server has
6848 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6849 * asserted.
6850 *
6851 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6852 * from this client that require migration recovery.
6853 */
6854int nfs4_proc_get_locations(struct inode *inode,
6855 struct nfs4_fs_locations *locations,
6856 struct page *page, struct rpc_cred *cred)
6857{
6858 struct nfs_server *server = NFS_SERVER(inode);
6859 struct nfs_client *clp = server->nfs_client;
6860 const struct nfs4_mig_recovery_ops *ops =
6861 clp->cl_mvops->mig_recovery_ops;
6862 struct nfs4_exception exception = { };
6863 int status;
6864
6865 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6866 (unsigned long long)server->fsid.major,
6867 (unsigned long long)server->fsid.minor,
6868 clp->cl_hostname);
6869 nfs_display_fhandle(NFS_FH(inode), __func__);
6870
6871 do {
6872 status = ops->get_locations(inode, locations, page, cred);
6873 if (status != -NFS4ERR_DELAY)
6874 break;
6875 nfs4_handle_exception(server, status, &exception);
6876 } while (exception.retry);
6877 return status;
6878}
6879
Chuck Lever44c99932013-10-17 14:13:30 -04006880/*
6881 * This operation also signals the server that this client is
6882 * performing "lease moved" recovery. The server can stop
6883 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6884 * is appended to this compound to identify the client ID which is
6885 * performing recovery.
6886 */
6887static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6888{
6889 struct nfs_server *server = NFS_SERVER(inode);
6890 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6891 struct rpc_clnt *clnt = server->client;
6892 struct nfs4_fsid_present_arg args = {
6893 .fh = NFS_FH(inode),
6894 .clientid = clp->cl_clientid,
6895 .renew = 1, /* append RENEW */
6896 };
6897 struct nfs4_fsid_present_res res = {
6898 .renew = 1,
6899 };
6900 struct rpc_message msg = {
6901 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6902 .rpc_argp = &args,
6903 .rpc_resp = &res,
6904 .rpc_cred = cred,
6905 };
6906 unsigned long now = jiffies;
6907 int status;
6908
6909 res.fh = nfs_alloc_fhandle();
6910 if (res.fh == NULL)
6911 return -ENOMEM;
6912
6913 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6914 nfs4_set_sequence_privileged(&args.seq_args);
6915 status = nfs4_call_sync_sequence(clnt, server, &msg,
6916 &args.seq_args, &res.seq_res);
6917 nfs_free_fhandle(res.fh);
6918 if (status)
6919 return status;
6920
6921 do_renew_lease(clp, now);
6922 return 0;
6923}
6924
6925#ifdef CONFIG_NFS_V4_1
6926
6927/*
6928 * This operation also signals the server that this client is
6929 * performing "lease moved" recovery. The server can stop asserting
6930 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6931 * this operation is identified in the SEQUENCE operation in this
6932 * compound.
6933 */
6934static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6935{
6936 struct nfs_server *server = NFS_SERVER(inode);
6937 struct rpc_clnt *clnt = server->client;
6938 struct nfs4_fsid_present_arg args = {
6939 .fh = NFS_FH(inode),
6940 };
6941 struct nfs4_fsid_present_res res = {
6942 };
6943 struct rpc_message msg = {
6944 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6945 .rpc_argp = &args,
6946 .rpc_resp = &res,
6947 .rpc_cred = cred,
6948 };
6949 int status;
6950
6951 res.fh = nfs_alloc_fhandle();
6952 if (res.fh == NULL)
6953 return -ENOMEM;
6954
6955 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6956 nfs4_set_sequence_privileged(&args.seq_args);
6957 status = nfs4_call_sync_sequence(clnt, server, &msg,
6958 &args.seq_args, &res.seq_res);
6959 nfs_free_fhandle(res.fh);
6960 if (status == NFS4_OK &&
6961 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6962 status = -NFS4ERR_LEASE_MOVED;
6963 return status;
6964}
6965
6966#endif /* CONFIG_NFS_V4_1 */
6967
6968/**
6969 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6970 * @inode: inode on FSID to check
6971 * @cred: credential to use for this operation
6972 *
6973 * Server indicates whether the FSID is present, moved, or not
6974 * recognized. This operation is necessary to clear a LEASE_MOVED
6975 * condition for this client ID.
6976 *
6977 * Returns NFS4_OK if the FSID is present on this server,
6978 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6979 * NFS4ERR code if some error occurred on the server, or a
6980 * negative errno if a local failure occurred.
6981 */
6982int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6983{
6984 struct nfs_server *server = NFS_SERVER(inode);
6985 struct nfs_client *clp = server->nfs_client;
6986 const struct nfs4_mig_recovery_ops *ops =
6987 clp->cl_mvops->mig_recovery_ops;
6988 struct nfs4_exception exception = { };
6989 int status;
6990
6991 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6992 (unsigned long long)server->fsid.major,
6993 (unsigned long long)server->fsid.minor,
6994 clp->cl_hostname);
6995 nfs_display_fhandle(NFS_FH(inode), __func__);
6996
6997 do {
6998 status = ops->fsid_present(inode, cred);
6999 if (status != -NFS4ERR_DELAY)
7000 break;
7001 nfs4_handle_exception(server, status, &exception);
7002 } while (exception.retry);
7003 return status;
7004}
7005
Andy Adamson5ec16a82013-08-08 10:57:55 -04007006/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007007 * If 'use_integrity' is true and the state managment nfs_client
7008 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7009 * and the machine credential as per RFC3530bis and RFC5661 Security
7010 * Considerations sections. Otherwise, just use the user cred with the
7011 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007012 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007013static 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 +00007014{
7015 int status;
7016 struct nfs4_secinfo_arg args = {
7017 .dir_fh = NFS_FH(dir),
7018 .name = name,
7019 };
7020 struct nfs4_secinfo_res res = {
7021 .flavors = flavors,
7022 };
7023 struct rpc_message msg = {
7024 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7025 .rpc_argp = &args,
7026 .rpc_resp = &res,
7027 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007028 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007029 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007030
7031 if (use_integrity) {
7032 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007033 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7034 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007035 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007036
7037 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007038
7039 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7040 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7041
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007042 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7043 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007044 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007045
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007046 if (cred)
7047 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007048
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007049 return status;
7050}
7051
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007052int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7053 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007054{
7055 struct nfs4_exception exception = { };
7056 int err;
7057 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007058 err = -NFS4ERR_WRONGSEC;
7059
7060 /* try to use integrity protection with machine cred */
7061 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7062 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7063
7064 /*
7065 * if unable to use integrity protection, or SECINFO with
7066 * integrity protection returns NFS4ERR_WRONGSEC (which is
7067 * disallowed by spec, but exists in deployed servers) use
7068 * the current filesystem's rpc_client and the user cred.
7069 */
7070 if (err == -NFS4ERR_WRONGSEC)
7071 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7072
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007073 trace_nfs4_secinfo(dir, name, err);
7074 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007075 &exception);
7076 } while (exception.retry);
7077 return err;
7078}
7079
Andy Adamson557134a2009-04-01 09:21:53 -04007080#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007081/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007082 * Check the exchange flags returned by the server for invalid flags, having
7083 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7084 * DS flags set.
7085 */
7086static int nfs4_check_cl_exchange_flags(u32 flags)
7087{
7088 if (flags & ~EXCHGID4_FLAG_MASK_R)
7089 goto out_inval;
7090 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7091 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7092 goto out_inval;
7093 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7094 goto out_inval;
7095 return NFS_OK;
7096out_inval:
7097 return -NFS4ERR_INVAL;
7098}
7099
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007100static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007101nfs41_same_server_scope(struct nfs41_server_scope *a,
7102 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007103{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007104 if (a->server_scope_sz != b->server_scope_sz)
7105 return false;
7106 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007107}
7108
Andy Adamson02a95de2016-02-05 16:08:37 -05007109static void
7110nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7111{
7112}
7113
7114static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7115 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7116};
7117
Andy Adamson357f54d2010-12-14 10:11:57 -05007118/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007119 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007120 *
7121 * The 4.1 client currently uses the same TCP connection for the
7122 * fore and backchannel.
7123 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007124static
7125int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7126 struct rpc_xprt *xprt,
7127 struct nfs_client *clp,
7128 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007129{
7130 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007131 struct nfs41_bind_conn_to_session_args args = {
7132 .client = clp,
7133 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7134 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007135 struct nfs41_bind_conn_to_session_res res;
7136 struct rpc_message msg = {
7137 .rpc_proc =
7138 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007139 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007140 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007141 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007142 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007143 struct rpc_task_setup task_setup_data = {
7144 .rpc_client = clnt,
7145 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007146 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007147 .rpc_message = &msg,
7148 .flags = RPC_TASK_TIMEOUT,
7149 };
7150 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007151
Trond Myklebust71a097c2015-02-18 09:27:18 -08007152 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7153 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7154 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007155
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007156 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7157 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7158 args.dir = NFS4_CDFC4_FORE;
7159
7160 task = rpc_run_task(&task_setup_data);
7161 if (!IS_ERR(task)) {
7162 status = task->tk_status;
7163 rpc_put_task(task);
7164 } else
7165 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007166 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007167 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007168 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007169 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7170 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007171 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007172 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007173 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007174 dprintk("NFS: %s: Unexpected direction from server\n",
7175 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007176 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007177 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007178 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007179 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7180 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007181 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007182 }
7183 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007184
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007185 return status;
7186}
7187
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007188struct rpc_bind_conn_calldata {
7189 struct nfs_client *clp;
7190 struct rpc_cred *cred;
7191};
7192
7193static int
7194nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7195 struct rpc_xprt *xprt,
7196 void *calldata)
7197{
7198 struct rpc_bind_conn_calldata *p = calldata;
7199
7200 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7201}
7202
7203int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7204{
7205 struct rpc_bind_conn_calldata data = {
7206 .clp = clp,
7207 .cred = cred,
7208 };
7209 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7210 nfs4_proc_bind_conn_to_session_callback, &data);
7211}
7212
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007213/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007214 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7215 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007216 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007217static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7218 .how = SP4_MACH_CRED,
7219 .enforce.u.words = {
7220 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7221 1 << (OP_EXCHANGE_ID - 32) |
7222 1 << (OP_CREATE_SESSION - 32) |
7223 1 << (OP_DESTROY_SESSION - 32) |
7224 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007225 },
7226 .allow.u.words = {
7227 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007228 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007229 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007230 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007231 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007232 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007233 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007234 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007235 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007236 1 << (OP_FREE_STATEID - 32) |
7237 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007238 }
7239};
7240
7241/*
7242 * Select the state protection mode for client `clp' given the server results
7243 * from exchange_id in `sp'.
7244 *
7245 * Returns 0 on success, negative errno otherwise.
7246 */
7247static int nfs4_sp4_select_mode(struct nfs_client *clp,
7248 struct nfs41_state_protection *sp)
7249{
7250 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7251 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7252 1 << (OP_EXCHANGE_ID - 32) |
7253 1 << (OP_CREATE_SESSION - 32) |
7254 1 << (OP_DESTROY_SESSION - 32) |
7255 1 << (OP_DESTROY_CLIENTID - 32)
7256 };
7257 unsigned int i;
7258
7259 if (sp->how == SP4_MACH_CRED) {
7260 /* Print state protect result */
7261 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7262 for (i = 0; i <= LAST_NFS4_OP; i++) {
7263 if (test_bit(i, sp->enforce.u.longs))
7264 dfprintk(MOUNT, " enforce op %d\n", i);
7265 if (test_bit(i, sp->allow.u.longs))
7266 dfprintk(MOUNT, " allow op %d\n", i);
7267 }
7268
7269 /* make sure nothing is on enforce list that isn't supported */
7270 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7271 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7272 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7273 return -EINVAL;
7274 }
7275 }
7276
7277 /*
7278 * Minimal mode - state operations are allowed to use machine
7279 * credential. Note this already happens by default, so the
7280 * client doesn't have to do anything more than the negotiation.
7281 *
7282 * NOTE: we don't care if EXCHANGE_ID is in the list -
7283 * we're already using the machine cred for exchange_id
7284 * and will never use a different cred.
7285 */
7286 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7287 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7288 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7289 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7290 dfprintk(MOUNT, "sp4_mach_cred:\n");
7291 dfprintk(MOUNT, " minimal mode enabled\n");
7292 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7293 } else {
7294 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7295 return -EINVAL;
7296 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007297
7298 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007299 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7300 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007301 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7302 dfprintk(MOUNT, " cleanup mode enabled\n");
7303 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7304 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007305
Andrew Elble99ade3c2015-12-02 09:39:51 -05007306 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7307 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7308 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7309 &clp->cl_sp4_flags);
7310 }
7311
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007312 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7313 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7314 dfprintk(MOUNT, " secinfo mode enabled\n");
7315 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7316 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007317
7318 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7319 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7320 dfprintk(MOUNT, " stateid mode enabled\n");
7321 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7322 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007323
7324 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7325 dfprintk(MOUNT, " write mode enabled\n");
7326 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7327 }
7328
7329 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7330 dfprintk(MOUNT, " commit mode enabled\n");
7331 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7332 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007333 }
7334
7335 return 0;
7336}
7337
Andy Adamson8d89bd72016-09-09 09:22:18 -04007338struct nfs41_exchange_id_data {
7339 struct nfs41_exchange_id_res res;
7340 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007341 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007342 int rpc_status;
7343};
7344
7345static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007346{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007347 struct nfs41_exchange_id_data *cdata =
7348 (struct nfs41_exchange_id_data *)data;
7349 struct nfs_client *clp = cdata->args.client;
7350 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007351
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007352 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007353
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007354 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007355 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007356
7357 if (cdata->xprt && status == 0) {
7358 status = nfs4_detect_session_trunking(clp, &cdata->res,
7359 cdata->xprt);
7360 goto out;
7361 }
7362
Andy Adamson8d89bd72016-09-09 09:22:18 -04007363 if (status == 0)
7364 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007365
Chuck Lever177313f2012-05-21 22:44:58 -04007366 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007367 clp->cl_clientid = cdata->res.clientid;
7368 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007369 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007370 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007371 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007372 &clp->cl_session->session_state);
7373 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007374 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007375
Chuck Leveracdeb692012-05-21 22:46:16 -04007376 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007377 clp->cl_serverowner = cdata->res.server_owner;
7378 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007379
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007380 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007381 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007382 clp->cl_implid = cdata->res.impl_id;
7383 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007384
Chuck Lever177313f2012-05-21 22:44:58 -04007385 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007386 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007387 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007388 dprintk("%s: server_scope mismatch detected\n",
7389 __func__);
7390 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007391 kfree(clp->cl_serverscope);
7392 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007393 }
7394
Chuck Lever177313f2012-05-21 22:44:58 -04007395 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007396 clp->cl_serverscope = cdata->res.server_scope;
7397 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007398 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007399 /* Save the EXCHANGE_ID verifier session trunk tests */
7400 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7401 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007402 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007403out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007404 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007405 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007406}
7407
7408static void nfs4_exchange_id_release(void *data)
7409{
7410 struct nfs41_exchange_id_data *cdata =
7411 (struct nfs41_exchange_id_data *)data;
7412
Andy Adamsonad0849a2016-09-09 09:22:28 -04007413 if (cdata->xprt) {
7414 xprt_put(cdata->xprt);
7415 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7416 }
Olga Kornievskaia63513232017-03-13 10:36:19 -04007417 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007418 kfree(cdata->res.impl_id);
7419 kfree(cdata->res.server_scope);
7420 kfree(cdata->res.server_owner);
7421 kfree(cdata);
7422}
7423
7424static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7425 .rpc_call_done = nfs4_exchange_id_done,
7426 .rpc_release = nfs4_exchange_id_release,
7427};
7428
Benny Halevy99fe60d2009-04-01 09:22:29 -04007429/*
7430 * _nfs4_proc_exchange_id()
7431 *
7432 * Wrapper for EXCHANGE_ID operation.
7433 */
7434static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007435 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007436{
7437 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007438 struct rpc_message msg = {
7439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007440 .rpc_cred = cred,
7441 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007442 struct rpc_task_setup task_setup_data = {
7443 .rpc_client = clp->cl_rpcclient,
7444 .callback_ops = &nfs4_exchange_id_call_ops,
7445 .rpc_message = &msg,
7446 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7447 };
7448 struct nfs41_exchange_id_data *calldata;
7449 struct rpc_task *task;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007450 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007451
7452 if (!atomic_inc_not_zero(&clp->cl_count))
Anna Schumakere917f0d2017-04-07 14:15:22 -04007453 return -EIO;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007454
Andy Adamson8d89bd72016-09-09 09:22:18 -04007455 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7456 if (!calldata)
Anna Schumakere917f0d2017-04-07 14:15:22 -04007457 return -ENOMEM;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007458
Andy Adamsonad0849a2016-09-09 09:22:28 -04007459 if (!xprt)
7460 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007461
7462 status = nfs4_init_uniform_client_string(clp);
7463 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007464 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007465
Andy Adamson8d89bd72016-09-09 09:22:18 -04007466 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7467 GFP_NOFS);
7468 status = -ENOMEM;
7469 if (unlikely(calldata->res.server_owner == NULL))
7470 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007471
Andy Adamson8d89bd72016-09-09 09:22:18 -04007472 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007473 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007474 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007475 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007476
Andy Adamson8d89bd72016-09-09 09:22:18 -04007477 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7478 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007479 goto out_server_scope;
7480
7481 switch (sp4_how) {
7482 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007483 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007484 break;
7485
7486 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007487 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007488 break;
7489
7490 default:
7491 /* unsupported! */
7492 WARN_ON_ONCE(1);
7493 status = -EINVAL;
7494 goto out_impl_id;
7495 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007496 if (xprt) {
7497 calldata->xprt = xprt;
7498 task_setup_data.rpc_xprt = xprt;
7499 task_setup_data.flags =
7500 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7501 calldata->args.verifier = &clp->cl_confirm;
7502 } else {
7503 calldata->args.verifier = &verifier;
7504 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007505 calldata->args.client = clp;
7506#ifdef CONFIG_NFS_V4_1_MIGRATION
7507 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7508 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7509 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7510#else
7511 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7512 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7513#endif
7514 msg.rpc_argp = &calldata->args;
7515 msg.rpc_resp = &calldata->res;
7516 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007517
Andy Adamson8d89bd72016-09-09 09:22:18 -04007518 task = rpc_run_task(&task_setup_data);
Olga Kornievskaia63513232017-03-13 10:36:19 -04007519 if (IS_ERR(task))
7520 return PTR_ERR(task);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007521
Andy Adamsonad0849a2016-09-09 09:22:28 -04007522 if (!xprt) {
7523 status = rpc_wait_for_completion_task(task);
7524 if (!status)
7525 status = calldata->rpc_status;
7526 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007527 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007528
Andy Adamson8d89bd72016-09-09 09:22:18 -04007529 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007530out:
Benny Halevy99fe60d2009-04-01 09:22:29 -04007531 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007532
7533out_impl_id:
7534 kfree(calldata->res.impl_id);
7535out_server_scope:
7536 kfree(calldata->res.server_scope);
7537out_server_owner:
7538 kfree(calldata->res.server_owner);
7539out_calldata:
7540 kfree(calldata);
Olga Kornievskaia63513232017-03-13 10:36:19 -04007541 nfs_put_client(clp);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007542 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007543}
7544
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007545/*
7546 * nfs4_proc_exchange_id()
7547 *
7548 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7549 *
7550 * Since the clientid has expired, all compounds using sessions
7551 * associated with the stale clientid will be returning
7552 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7553 * be in some phase of session reset.
7554 *
7555 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7556 */
7557int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7558{
7559 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7560 int status;
7561
7562 /* try SP4_MACH_CRED if krb5i/p */
7563 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7564 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007565 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007566 if (!status)
7567 return 0;
7568 }
7569
7570 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007571 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007572}
7573
Andy Adamson04fa2c62016-09-09 09:22:29 -04007574/**
7575 * nfs4_test_session_trunk
7576 *
7577 * This is an add_xprt_test() test function called from
7578 * rpc_clnt_setup_test_and_add_xprt.
7579 *
7580 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7581 * and is dereferrenced in nfs4_exchange_id_release
7582 *
7583 * Upon success, add the new transport to the rpc_clnt
7584 *
7585 * @clnt: struct rpc_clnt to get new transport
7586 * @xprt: the rpc_xprt to test
7587 * @data: call data for _nfs4_proc_exchange_id.
7588 */
7589int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7590 void *data)
7591{
7592 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7593 u32 sp4_how;
7594
7595 dprintk("--> %s try %s\n", __func__,
7596 xprt->address_strings[RPC_DISPLAY_ADDR]);
7597
7598 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7599
7600 /* Test connection for session trunking. Async exchange_id call */
7601 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7602}
7603EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7604
Trond Myklebust66245532012-05-25 17:18:09 -04007605static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7606 struct rpc_cred *cred)
7607{
7608 struct rpc_message msg = {
7609 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7610 .rpc_argp = clp,
7611 .rpc_cred = cred,
7612 };
7613 int status;
7614
7615 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007616 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007617 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007618 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007619 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7620 return status;
7621}
7622
7623static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7624 struct rpc_cred *cred)
7625{
7626 unsigned int loop;
7627 int ret;
7628
7629 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7630 ret = _nfs4_proc_destroy_clientid(clp, cred);
7631 switch (ret) {
7632 case -NFS4ERR_DELAY:
7633 case -NFS4ERR_CLIENTID_BUSY:
7634 ssleep(1);
7635 break;
7636 default:
7637 return ret;
7638 }
7639 }
7640 return 0;
7641}
7642
7643int nfs4_destroy_clientid(struct nfs_client *clp)
7644{
7645 struct rpc_cred *cred;
7646 int ret = 0;
7647
7648 if (clp->cl_mvops->minor_version < 1)
7649 goto out;
7650 if (clp->cl_exchange_flags == 0)
7651 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007652 if (clp->cl_preserve_clid)
7653 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007654 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007655 ret = nfs4_proc_destroy_clientid(clp, cred);
7656 if (cred)
7657 put_rpccred(cred);
7658 switch (ret) {
7659 case 0:
7660 case -NFS4ERR_STALE_CLIENTID:
7661 clp->cl_exchange_flags = 0;
7662 }
7663out:
7664 return ret;
7665}
7666
Andy Adamson2050f0c2009-04-01 09:22:30 -04007667struct nfs4_get_lease_time_data {
7668 struct nfs4_get_lease_time_args *args;
7669 struct nfs4_get_lease_time_res *res;
7670 struct nfs_client *clp;
7671};
7672
7673static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7674 void *calldata)
7675{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007676 struct nfs4_get_lease_time_data *data =
7677 (struct nfs4_get_lease_time_data *)calldata;
7678
7679 dprintk("--> %s\n", __func__);
7680 /* just setup sequence, do not trigger session recovery
7681 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007682 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007683 &data->args->la_seq_args,
7684 &data->res->lr_seq_res,
7685 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007686 dprintk("<-- %s\n", __func__);
7687}
7688
7689/*
7690 * Called from nfs4_state_manager thread for session setup, so don't recover
7691 * from sequence operation or clientid errors.
7692 */
7693static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7694{
7695 struct nfs4_get_lease_time_data *data =
7696 (struct nfs4_get_lease_time_data *)calldata;
7697
7698 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007699 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7700 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007701 switch (task->tk_status) {
7702 case -NFS4ERR_DELAY:
7703 case -NFS4ERR_GRACE:
7704 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7705 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7706 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007707 /* fall through */
7708 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007709 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007710 return;
7711 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007712 dprintk("<-- %s\n", __func__);
7713}
7714
Trond Myklebust17280172012-03-11 13:11:00 -04007715static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007716 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7717 .rpc_call_done = nfs4_get_lease_time_done,
7718};
7719
7720int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7721{
7722 struct rpc_task *task;
7723 struct nfs4_get_lease_time_args args;
7724 struct nfs4_get_lease_time_res res = {
7725 .lr_fsinfo = fsinfo,
7726 };
7727 struct nfs4_get_lease_time_data data = {
7728 .args = &args,
7729 .res = &res,
7730 .clp = clp,
7731 };
7732 struct rpc_message msg = {
7733 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7734 .rpc_argp = &args,
7735 .rpc_resp = &res,
7736 };
7737 struct rpc_task_setup task_setup = {
7738 .rpc_client = clp->cl_rpcclient,
7739 .rpc_message = &msg,
7740 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007741 .callback_data = &data,
7742 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007743 };
7744 int status;
7745
Chuck Levera9c92d62013-08-09 12:48:18 -04007746 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007747 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007748 task = rpc_run_task(&task_setup);
7749
7750 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04007751 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007752
Anna Schumakerf6148712017-04-07 14:15:23 -04007753 status = task->tk_status;
7754 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007755 return status;
7756}
7757
Andy Adamsonfc931582009-04-01 09:22:31 -04007758/*
7759 * Initialize the values to be used by the client in CREATE_SESSION
7760 * If nfs4_init_session set the fore channel request and response sizes,
7761 * use them.
7762 *
7763 * Set the back channel max_resp_sz_cached to zero to force the client to
7764 * always set csa_cachethis to FALSE because the current implementation
7765 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7766 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007767static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7768 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007769{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007770 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007771 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007772
Andy Adamson18aad3d2013-06-26 12:21:49 -04007773 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7774 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7775
Andy Adamsonfc931582009-04-01 09:22:31 -04007776 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007777 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7778 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007779 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007780 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007781
7782 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007783 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007784 __func__,
7785 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007786 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007787
7788 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007789 args->bc_attrs.max_rqst_sz = max_bc_payload;
7790 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007791 args->bc_attrs.max_resp_sz_cached = 0;
7792 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007793 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007794
7795 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7796 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7797 __func__,
7798 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7799 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7800 args->bc_attrs.max_reqs);
7801}
7802
Trond Myklebust79969dd2015-02-18 11:30:18 -08007803static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7804 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007805{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007806 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007807 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007808
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007809 if (rcvd->max_resp_sz > sent->max_resp_sz)
7810 return -EINVAL;
7811 /*
7812 * Our requested max_ops is the minimum we need; we're not
7813 * prepared to break up compounds into smaller pieces than that.
7814 * So, no point even trying to continue if the server won't
7815 * cooperate:
7816 */
7817 if (rcvd->max_ops < sent->max_ops)
7818 return -EINVAL;
7819 if (rcvd->max_reqs == 0)
7820 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007821 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7822 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007823 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007824}
7825
Trond Myklebust79969dd2015-02-18 11:30:18 -08007826static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7827 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007828{
7829 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007830 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007831
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007832 if (!(res->flags & SESSION4_BACK_CHAN))
7833 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007834 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7835 return -EINVAL;
7836 if (rcvd->max_resp_sz < sent->max_resp_sz)
7837 return -EINVAL;
7838 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7839 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007840 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007841 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007842 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007843 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007844out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007845 return 0;
7846}
Andy Adamson8d353012009-04-01 09:22:32 -04007847
Andy Adamson8d353012009-04-01 09:22:32 -04007848static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007849 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007850{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007851 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007852
Trond Myklebust79969dd2015-02-18 11:30:18 -08007853 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007854 if (ret)
7855 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007856 return nfs4_verify_back_channel_attrs(args, res);
7857}
7858
7859static void nfs4_update_session(struct nfs4_session *session,
7860 struct nfs41_create_session_res *res)
7861{
7862 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007863 /* Mark client id and session as being confirmed */
7864 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7865 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007866 session->flags = res->flags;
7867 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007868 if (res->flags & SESSION4_BACK_CHAN)
7869 memcpy(&session->bc_attrs, &res->bc_attrs,
7870 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007871}
7872
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007873static int _nfs4_proc_create_session(struct nfs_client *clp,
7874 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007875{
7876 struct nfs4_session *session = clp->cl_session;
7877 struct nfs41_create_session_args args = {
7878 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007879 .clientid = clp->cl_clientid,
7880 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007881 .cb_program = NFS4_CALLBACK,
7882 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007883 struct nfs41_create_session_res res;
7884
Andy Adamsonfc931582009-04-01 09:22:31 -04007885 struct rpc_message msg = {
7886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7887 .rpc_argp = &args,
7888 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007889 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007890 };
7891 int status;
7892
Chuck Lever6b26cc82016-05-02 14:40:40 -04007893 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007894 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007895
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007896 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007897 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007898
Trond Myklebustb519d402016-09-11 14:50:01 -04007899 switch (status) {
7900 case -NFS4ERR_STALE_CLIENTID:
7901 case -NFS4ERR_DELAY:
7902 case -ETIMEDOUT:
7903 case -EACCES:
7904 case -EAGAIN:
7905 goto out;
7906 };
7907
7908 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007909 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007910 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007911 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007912 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007913 if (status)
7914 goto out;
7915 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007916 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007917out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007918 return status;
7919}
7920
7921/*
7922 * Issues a CREATE_SESSION operation to the server.
7923 * It is the responsibility of the caller to verify the session is
7924 * expired before calling this routine.
7925 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007926int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007927{
7928 int status;
7929 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007930 struct nfs4_session *session = clp->cl_session;
7931
7932 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7933
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007934 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007935 if (status)
7936 goto out;
7937
Andy Adamsonaacd5532011-11-09 13:58:21 -05007938 /* Init or reset the session slot tables */
7939 status = nfs4_setup_session_slot_tables(session);
7940 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007941 if (status)
7942 goto out;
7943
7944 ptr = (unsigned *)&session->sess_id.data[0];
7945 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7946 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007947out:
7948 dprintk("<-- %s\n", __func__);
7949 return status;
7950}
7951
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007952/*
7953 * Issue the over-the-wire RPC DESTROY_SESSION.
7954 * The caller must serialize access to this routine.
7955 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007956int nfs4_proc_destroy_session(struct nfs4_session *session,
7957 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007958{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007959 struct rpc_message msg = {
7960 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7961 .rpc_argp = session,
7962 .rpc_cred = cred,
7963 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007964 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007965
7966 dprintk("--> nfs4_proc_destroy_session\n");
7967
7968 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007969 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7970 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007971
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007972 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007973 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007974
7975 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007976 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007977 "Session has been destroyed regardless...\n", status);
7978
7979 dprintk("<-- nfs4_proc_destroy_session\n");
7980 return status;
7981}
7982
Trond Myklebust7b38c362012-05-23 13:23:31 -04007983/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007984 * Renew the cl_session lease.
7985 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007986struct nfs4_sequence_data {
7987 struct nfs_client *clp;
7988 struct nfs4_sequence_args args;
7989 struct nfs4_sequence_res res;
7990};
7991
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007992static void nfs41_sequence_release(void *data)
7993{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007994 struct nfs4_sequence_data *calldata = data;
7995 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007996
Alexandros Batsakis71358402010-02-05 03:45:05 -08007997 if (atomic_read(&clp->cl_count) > 1)
7998 nfs4_schedule_state_renewal(clp);
7999 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008000 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008001}
8002
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008003static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8004{
8005 switch(task->tk_status) {
8006 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008007 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8008 return -EAGAIN;
8009 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008010 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008011 }
8012 return 0;
8013}
8014
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008015static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008016{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008017 struct nfs4_sequence_data *calldata = data;
8018 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008019
Trond Myklebust14516c32010-07-31 14:29:06 -04008020 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8021 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008022
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008023 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008024 if (task->tk_status < 0) {
8025 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008026 if (atomic_read(&clp->cl_count) == 1)
8027 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008028
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008029 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8030 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008031 return;
8032 }
8033 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008034 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008035out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008036 dprintk("<-- %s\n", __func__);
8037}
8038
8039static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8040{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008041 struct nfs4_sequence_data *calldata = data;
8042 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008043 struct nfs4_sequence_args *args;
8044 struct nfs4_sequence_res *res;
8045
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008046 args = task->tk_msg.rpc_argp;
8047 res = task->tk_msg.rpc_resp;
8048
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008049 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008050}
8051
8052static const struct rpc_call_ops nfs41_sequence_ops = {
8053 .rpc_call_done = nfs41_sequence_call_done,
8054 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008055 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008056};
8057
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008058static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8059 struct rpc_cred *cred,
8060 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008061{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008062 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008063 struct rpc_message msg = {
8064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8065 .rpc_cred = cred,
8066 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008067 struct rpc_task_setup task_setup_data = {
8068 .rpc_client = clp->cl_rpcclient,
8069 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008070 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008071 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008072 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008073
Alexandros Batsakis71358402010-02-05 03:45:05 -08008074 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008075 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008076 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008077 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008078 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008079 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008080 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008081 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008082 if (is_privileged)
8083 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008084 msg.rpc_argp = &calldata->args;
8085 msg.rpc_resp = &calldata->res;
8086 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008087 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008088
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008089 return rpc_run_task(&task_setup_data);
8090}
8091
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008092static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008093{
8094 struct rpc_task *task;
8095 int ret = 0;
8096
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008097 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008098 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008099 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008100 if (IS_ERR(task))
8101 ret = PTR_ERR(task);
8102 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008103 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008104 dprintk("<-- %s status=%d\n", __func__, ret);
8105 return ret;
8106}
8107
8108static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8109{
8110 struct rpc_task *task;
8111 int ret;
8112
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008113 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008114 if (IS_ERR(task)) {
8115 ret = PTR_ERR(task);
8116 goto out;
8117 }
8118 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008119 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008120 ret = task->tk_status;
8121 rpc_put_task(task);
8122out:
8123 dprintk("<-- %s status=%d\n", __func__, ret);
8124 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008125}
8126
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008127struct nfs4_reclaim_complete_data {
8128 struct nfs_client *clp;
8129 struct nfs41_reclaim_complete_args arg;
8130 struct nfs41_reclaim_complete_res res;
8131};
8132
8133static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8134{
8135 struct nfs4_reclaim_complete_data *calldata = data;
8136
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008137 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008138 &calldata->arg.seq_args,
8139 &calldata->res.seq_res,
8140 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008141}
8142
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008143static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8144{
8145 switch(task->tk_status) {
8146 case 0:
8147 case -NFS4ERR_COMPLETE_ALREADY:
8148 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8149 break;
8150 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008151 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008152 /* fall through */
8153 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008154 return -EAGAIN;
8155 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008156 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008157 }
8158 return 0;
8159}
8160
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008161static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8162{
8163 struct nfs4_reclaim_complete_data *calldata = data;
8164 struct nfs_client *clp = calldata->clp;
8165 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8166
8167 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008168 if (!nfs41_sequence_done(task, res))
8169 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008170
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008171 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008172 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8173 rpc_restart_call_prepare(task);
8174 return;
8175 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008176 dprintk("<-- %s\n", __func__);
8177}
8178
8179static void nfs4_free_reclaim_complete_data(void *data)
8180{
8181 struct nfs4_reclaim_complete_data *calldata = data;
8182
8183 kfree(calldata);
8184}
8185
8186static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8187 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8188 .rpc_call_done = nfs4_reclaim_complete_done,
8189 .rpc_release = nfs4_free_reclaim_complete_data,
8190};
8191
8192/*
8193 * Issue a global reclaim complete.
8194 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008195static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8196 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008197{
8198 struct nfs4_reclaim_complete_data *calldata;
8199 struct rpc_task *task;
8200 struct rpc_message msg = {
8201 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008202 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008203 };
8204 struct rpc_task_setup task_setup_data = {
8205 .rpc_client = clp->cl_rpcclient,
8206 .rpc_message = &msg,
8207 .callback_ops = &nfs4_reclaim_complete_call_ops,
8208 .flags = RPC_TASK_ASYNC,
8209 };
8210 int status = -ENOMEM;
8211
8212 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008213 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008214 if (calldata == NULL)
8215 goto out;
8216 calldata->clp = clp;
8217 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008218
Chuck Levera9c92d62013-08-09 12:48:18 -04008219 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008220 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008221 msg.rpc_argp = &calldata->arg;
8222 msg.rpc_resp = &calldata->res;
8223 task_setup_data.callback_data = calldata;
8224 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008225 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008226 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008227 goto out;
8228 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008229 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008230 if (status == 0)
8231 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008232 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008233 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008234out:
8235 dprintk("<-- %s status=%d\n", __func__, status);
8236 return status;
8237}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008238
8239static void
8240nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8241{
8242 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008243 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008244
8245 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008246 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008247 &lgp->res.seq_res, task);
8248 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008249}
8250
8251static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8252{
8253 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008254
8255 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008256 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008257 dprintk("<-- %s\n", __func__);
8258}
8259
8260static int
8261nfs4_layoutget_handle_exception(struct rpc_task *task,
8262 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8263{
Trond Myklebustee314c22012-10-01 17:25:48 -07008264 struct inode *inode = lgp->args.inode;
8265 struct nfs_server *server = NFS_SERVER(inode);
8266 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008267 int nfs4err = task->tk_status;
8268 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008269 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008270
Boaz Harroshed7e5422014-01-22 20:34:54 +02008271 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008272
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008273 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008274 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008275 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008276
8277 /*
8278 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8279 * on the file. set tk_status to -ENODATA to tell upper layer to
8280 * retry go inband.
8281 */
8282 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008283 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008284 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008285 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008286 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8287 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8288 */
8289 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008290 status = -EOVERFLOW;
8291 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008292 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008293 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008294 * (or clients) writing to the same RAID stripe except when
8295 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008296 *
8297 * Treat it like we would RECALLCONFLICT -- we retry for a little
8298 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008299 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008300 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008301 if (lgp->args.minlength == 0) {
8302 status = -EOVERFLOW;
8303 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008304 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008305 status = -EBUSY;
8306 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008307 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008308 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008309 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008310 case -NFS4ERR_DELEG_REVOKED:
8311 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008312 case -NFS4ERR_EXPIRED:
8313 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008314 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008315 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008316 lo = NFS_I(inode)->layout;
8317 /* If the open stateid was bad, then recover it. */
8318 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8319 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008320 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008321 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008322 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008323 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008324 break;
8325 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008326
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008327 /*
8328 * Mark the bad layout state as invalid, then retry
8329 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008330 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008331 spin_unlock(&inode->i_lock);
8332 pnfs_free_lseg_list(&head);
8333 status = -EAGAIN;
8334 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008335 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008336
Trond Myklebust8ac09252017-01-23 22:44:12 -05008337 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008338 err = nfs4_handle_exception(server, nfs4err, exception);
8339 if (!status) {
8340 if (exception->retry)
8341 status = -EAGAIN;
8342 else
8343 status = err;
8344 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008345out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008346 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008347 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008348}
8349
Idan Kedar85541162012-08-02 11:47:10 +03008350static size_t max_response_pages(struct nfs_server *server)
8351{
8352 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8353 return nfs_page_array_len(0, max_resp_sz);
8354}
8355
8356static void nfs4_free_pages(struct page **pages, size_t size)
8357{
8358 int i;
8359
8360 if (!pages)
8361 return;
8362
8363 for (i = 0; i < size; i++) {
8364 if (!pages[i])
8365 break;
8366 __free_page(pages[i]);
8367 }
8368 kfree(pages);
8369}
8370
8371static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8372{
8373 struct page **pages;
8374 int i;
8375
8376 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8377 if (!pages) {
8378 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8379 return NULL;
8380 }
8381
8382 for (i = 0; i < size; i++) {
8383 pages[i] = alloc_page(gfp_flags);
8384 if (!pages[i]) {
8385 dprintk("%s: failed to allocate page\n", __func__);
8386 nfs4_free_pages(pages, size);
8387 return NULL;
8388 }
8389 }
8390
8391 return pages;
8392}
8393
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008394static void nfs4_layoutget_release(void *calldata)
8395{
8396 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008397 struct inode *inode = lgp->args.inode;
8398 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008399 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008400
8401 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008402 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008403 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008404 put_nfs_open_context(lgp->args.ctx);
8405 kfree(calldata);
8406 dprintk("<-- %s\n", __func__);
8407}
8408
8409static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8410 .rpc_call_prepare = nfs4_layoutget_prepare,
8411 .rpc_call_done = nfs4_layoutget_done,
8412 .rpc_release = nfs4_layoutget_release,
8413};
8414
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008415struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008416nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008417{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008418 struct inode *inode = lgp->args.inode;
8419 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008420 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008421 struct rpc_task *task;
8422 struct rpc_message msg = {
8423 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8424 .rpc_argp = &lgp->args,
8425 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008426 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008427 };
8428 struct rpc_task_setup task_setup_data = {
8429 .rpc_client = server->client,
8430 .rpc_message = &msg,
8431 .callback_ops = &nfs4_layoutget_call_ops,
8432 .callback_data = lgp,
8433 .flags = RPC_TASK_ASYNC,
8434 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008435 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008436 struct nfs4_exception exception = {
8437 .inode = inode,
8438 .timeout = *timeout,
8439 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008440 int status = 0;
8441
8442 dprintk("--> %s\n", __func__);
8443
Peng Tao4bd5a9802014-11-17 11:05:17 +08008444 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8445 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8446
Idan Kedar85541162012-08-02 11:47:10 +03008447 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8448 if (!lgp->args.layout.pages) {
8449 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008450 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008451 }
8452 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8453
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008454 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008455 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008456 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008457
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008458 task = rpc_run_task(&task_setup_data);
8459 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008460 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008461 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008462 if (status == 0) {
8463 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8464 *timeout = exception.timeout;
8465 }
8466
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008467 trace_nfs4_layoutget(lgp->args.ctx,
8468 &lgp->args.range,
8469 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008470 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008471 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008472
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008473 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8474 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008475 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008476 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008477 rpc_put_task(task);
8478 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008479 if (status)
8480 return ERR_PTR(status);
8481 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008482}
8483
Benny Halevycbe82602011-05-22 19:52:37 +03008484static void
8485nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8486{
8487 struct nfs4_layoutreturn *lrp = calldata;
8488
8489 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008490 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008491 &lrp->args.seq_args,
8492 &lrp->res.seq_res,
8493 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008494}
8495
8496static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8497{
8498 struct nfs4_layoutreturn *lrp = calldata;
8499 struct nfs_server *server;
8500
8501 dprintk("--> %s\n", __func__);
8502
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008503 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008504 return;
8505
8506 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008507 switch (task->tk_status) {
8508 default:
8509 task->tk_status = 0;
8510 case 0:
8511 break;
8512 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008513 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008514 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008515 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008516 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008517 return;
8518 }
Benny Halevycbe82602011-05-22 19:52:37 +03008519 dprintk("<-- %s\n", __func__);
8520}
8521
8522static void nfs4_layoutreturn_release(void *calldata)
8523{
8524 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008525 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008526
8527 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008528 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008529 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008530 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008531 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8532 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008533 pnfs_put_layout_hdr(lrp->args.layout);
8534 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008535 kfree(calldata);
8536 dprintk("<-- %s\n", __func__);
8537}
8538
8539static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8540 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8541 .rpc_call_done = nfs4_layoutreturn_done,
8542 .rpc_release = nfs4_layoutreturn_release,
8543};
8544
Peng Tao6c166052014-11-17 09:30:40 +08008545int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008546{
8547 struct rpc_task *task;
8548 struct rpc_message msg = {
8549 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8550 .rpc_argp = &lrp->args,
8551 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008552 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008553 };
8554 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008555 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008556 .rpc_message = &msg,
8557 .callback_ops = &nfs4_layoutreturn_call_ops,
8558 .callback_data = lrp,
8559 };
Peng Tao6c166052014-11-17 09:30:40 +08008560 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008561
Andrew Elble99ade3c2015-12-02 09:39:51 -05008562 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8563 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8564 &task_setup_data.rpc_client, &msg);
8565
Benny Halevycbe82602011-05-22 19:52:37 +03008566 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008567 if (!sync) {
8568 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8569 if (!lrp->inode) {
8570 nfs4_layoutreturn_release(lrp);
8571 return -EAGAIN;
8572 }
8573 task_setup_data.flags |= RPC_TASK_ASYNC;
8574 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008575 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008576 task = rpc_run_task(&task_setup_data);
8577 if (IS_ERR(task))
8578 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008579 if (sync)
8580 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008581 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008582 dprintk("<-- %s status=%d\n", __func__, status);
8583 rpc_put_task(task);
8584 return status;
8585}
8586
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008587static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008588_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8589 struct pnfs_device *pdev,
8590 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008591{
8592 struct nfs4_getdeviceinfo_args args = {
8593 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008594 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8595 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008596 };
8597 struct nfs4_getdeviceinfo_res res = {
8598 .pdev = pdev,
8599 };
8600 struct rpc_message msg = {
8601 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8602 .rpc_argp = &args,
8603 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008604 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008605 };
8606 int status;
8607
8608 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008609 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008610 if (res.notification & ~args.notify_types)
8611 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008612 if (res.notification != args.notify_types)
8613 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008614
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008615 dprintk("<-- %s status=%d\n", __func__, status);
8616
8617 return status;
8618}
8619
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008620int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8621 struct pnfs_device *pdev,
8622 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008623{
8624 struct nfs4_exception exception = { };
8625 int err;
8626
8627 do {
8628 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008629 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008630 &exception);
8631 } while (exception.retry);
8632 return err;
8633}
8634EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8635
Andy Adamson863a3c62011-03-23 13:27:54 +00008636static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8637{
8638 struct nfs4_layoutcommit_data *data = calldata;
8639 struct nfs_server *server = NFS_SERVER(data->args.inode);
8640
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008641 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008642 &data->args.seq_args,
8643 &data->res.seq_res,
8644 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008645}
8646
8647static void
8648nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8649{
8650 struct nfs4_layoutcommit_data *data = calldata;
8651 struct nfs_server *server = NFS_SERVER(data->args.inode);
8652
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008653 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008654 return;
8655
8656 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008657 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8658 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8659 case -NFS4ERR_BADLAYOUT: /* no layout */
8660 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008661 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008662 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008663 break;
8664 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008665 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008666 rpc_restart_call_prepare(task);
8667 return;
8668 }
8669 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008670}
8671
8672static void nfs4_layoutcommit_release(void *calldata)
8673{
8674 struct nfs4_layoutcommit_data *data = calldata;
8675
Andy Adamsondb29c082011-07-30 20:52:38 -04008676 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008677 nfs_post_op_update_inode_force_wcc(data->args.inode,
8678 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008679 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008680 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008681 kfree(data);
8682}
8683
8684static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8685 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8686 .rpc_call_done = nfs4_layoutcommit_done,
8687 .rpc_release = nfs4_layoutcommit_release,
8688};
8689
8690int
Andy Adamsonef311532011-03-12 02:58:10 -05008691nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008692{
8693 struct rpc_message msg = {
8694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8695 .rpc_argp = &data->args,
8696 .rpc_resp = &data->res,
8697 .rpc_cred = data->cred,
8698 };
8699 struct rpc_task_setup task_setup_data = {
8700 .task = &data->task,
8701 .rpc_client = NFS_CLIENT(data->args.inode),
8702 .rpc_message = &msg,
8703 .callback_ops = &nfs4_layoutcommit_ops,
8704 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008705 };
8706 struct rpc_task *task;
8707 int status = 0;
8708
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008709 dprintk("NFS: initiating layoutcommit call. sync %d "
8710 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008711 data->args.lastbytewritten,
8712 data->args.inode->i_ino);
8713
Trond Myklebust472e2592015-02-05 16:50:30 -05008714 if (!sync) {
8715 data->inode = nfs_igrab_and_active(data->args.inode);
8716 if (data->inode == NULL) {
8717 nfs4_layoutcommit_release(data);
8718 return -EAGAIN;
8719 }
8720 task_setup_data.flags = RPC_TASK_ASYNC;
8721 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008722 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008723 task = rpc_run_task(&task_setup_data);
8724 if (IS_ERR(task))
8725 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008726 if (sync)
8727 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008728 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008729 dprintk("%s: status %d\n", __func__, status);
8730 rpc_put_task(task);
8731 return status;
8732}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008733
Andy Adamson97431202013-08-08 10:57:56 -04008734/**
8735 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8736 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8737 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008738static int
8739_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008740 struct nfs_fsinfo *info,
8741 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008742{
8743 struct nfs41_secinfo_no_name_args args = {
8744 .style = SECINFO_STYLE_CURRENT_FH,
8745 };
8746 struct nfs4_secinfo_res res = {
8747 .flavors = flavors,
8748 };
8749 struct rpc_message msg = {
8750 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8751 .rpc_argp = &args,
8752 .rpc_resp = &res,
8753 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008754 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008755 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008756 int status;
8757
8758 if (use_integrity) {
8759 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008760 cred = nfs4_get_clid_cred(server->nfs_client);
8761 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008762 }
8763
8764 dprintk("--> %s\n", __func__);
8765 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8766 &res.seq_res, 0);
8767 dprintk("<-- %s status=%d\n", __func__, status);
8768
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008769 if (cred)
8770 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008771
8772 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008773}
8774
8775static int
8776nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8777 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8778{
8779 struct nfs4_exception exception = { };
8780 int err;
8781 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008782 /* first try using integrity protection */
8783 err = -NFS4ERR_WRONGSEC;
8784
8785 /* try to use integrity protection with machine cred */
8786 if (_nfs4_is_integrity_protected(server->nfs_client))
8787 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8788 flavors, true);
8789
8790 /*
8791 * if unable to use integrity protection, or SECINFO with
8792 * integrity protection returns NFS4ERR_WRONGSEC (which is
8793 * disallowed by spec, but exists in deployed servers) use
8794 * the current filesystem's rpc_client and the user cred.
8795 */
8796 if (err == -NFS4ERR_WRONGSEC)
8797 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8798 flavors, false);
8799
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008800 switch (err) {
8801 case 0:
8802 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008803 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008804 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008805 default:
8806 err = nfs4_handle_exception(server, err, &exception);
8807 }
8808 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008809out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008810 return err;
8811}
8812
8813static int
8814nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8815 struct nfs_fsinfo *info)
8816{
8817 int err;
8818 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008819 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008820 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008821 struct nfs4_secinfo4 *secinfo;
8822 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008823
8824 page = alloc_page(GFP_KERNEL);
8825 if (!page) {
8826 err = -ENOMEM;
8827 goto out;
8828 }
8829
8830 flavors = page_address(page);
8831 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8832
8833 /*
8834 * Fall back on "guess and check" method if
8835 * the server doesn't support SECINFO_NO_NAME
8836 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008837 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008838 err = nfs4_find_root_sec(server, fhandle, info);
8839 goto out_freepage;
8840 }
8841 if (err)
8842 goto out_freepage;
8843
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008844 for (i = 0; i < flavors->num_flavors; i++) {
8845 secinfo = &flavors->flavors[i];
8846
8847 switch (secinfo->flavor) {
8848 case RPC_AUTH_NULL:
8849 case RPC_AUTH_UNIX:
8850 case RPC_AUTH_GSS:
8851 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8852 &secinfo->flavor_info);
8853 break;
8854 default:
8855 flavor = RPC_AUTH_MAXFLAVOR;
8856 break;
8857 }
8858
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008859 if (!nfs_auth_info_match(&server->auth_info, flavor))
8860 flavor = RPC_AUTH_MAXFLAVOR;
8861
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008862 if (flavor != RPC_AUTH_MAXFLAVOR) {
8863 err = nfs4_lookup_root_sec(server, fhandle,
8864 info, flavor);
8865 if (!err)
8866 break;
8867 }
8868 }
8869
8870 if (flavor == RPC_AUTH_MAXFLAVOR)
8871 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008872
8873out_freepage:
8874 put_page(page);
8875 if (err == -EACCES)
8876 return -EPERM;
8877out:
8878 return err;
8879}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008880
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008881static int _nfs41_test_stateid(struct nfs_server *server,
8882 nfs4_stateid *stateid,
8883 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008884{
8885 int status;
8886 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008887 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008888 };
8889 struct nfs41_test_stateid_res res;
8890 struct rpc_message msg = {
8891 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8892 .rpc_argp = &args,
8893 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008894 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008895 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008896 struct rpc_clnt *rpc_client = server->client;
8897
8898 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8899 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008900
Chuck Lever38527b12012-07-11 16:30:23 -04008901 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008902 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008903 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008904 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008905 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008906 if (status != NFS_OK) {
8907 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008908 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008909 }
8910 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008911 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008912}
8913
Trond Myklebust43912bb2016-09-22 13:38:56 -04008914static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8915 int err, struct nfs4_exception *exception)
8916{
8917 exception->retry = 0;
8918 switch(err) {
8919 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008920 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008921 nfs4_handle_exception(server, err, exception);
8922 break;
8923 case -NFS4ERR_BADSESSION:
8924 case -NFS4ERR_BADSLOT:
8925 case -NFS4ERR_BAD_HIGH_SLOT:
8926 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8927 case -NFS4ERR_DEADSESSION:
8928 nfs4_do_handle_exception(server, err, exception);
8929 }
8930}
8931
Chuck Lever38527b12012-07-11 16:30:23 -04008932/**
8933 * nfs41_test_stateid - perform a TEST_STATEID operation
8934 *
8935 * @server: server / transport on which to perform the operation
8936 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008937 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008938 *
8939 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8940 * Otherwise a negative NFS4ERR value is returned if the operation
8941 * failed or the state ID is not currently valid.
8942 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008943static int nfs41_test_stateid(struct nfs_server *server,
8944 nfs4_stateid *stateid,
8945 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008946{
8947 struct nfs4_exception exception = { };
8948 int err;
8949 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008950 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04008951 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008952 } while (exception.retry);
8953 return err;
8954}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008955
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008956struct nfs_free_stateid_data {
8957 struct nfs_server *server;
8958 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008959 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008960};
8961
8962static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8963{
8964 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008965 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008966 &data->args.seq_args,
8967 &data->res.seq_res,
8968 task);
8969}
8970
8971static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8972{
8973 struct nfs_free_stateid_data *data = calldata;
8974
8975 nfs41_sequence_done(task, &data->res.seq_res);
8976
8977 switch (task->tk_status) {
8978 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008979 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008980 rpc_restart_call_prepare(task);
8981 }
8982}
8983
8984static void nfs41_free_stateid_release(void *calldata)
8985{
8986 kfree(calldata);
8987}
8988
Trond Myklebust17f26b12013-08-21 15:48:42 -04008989static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008990 .rpc_call_prepare = nfs41_free_stateid_prepare,
8991 .rpc_call_done = nfs41_free_stateid_done,
8992 .rpc_release = nfs41_free_stateid_release,
8993};
8994
8995static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04008996 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008997 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008998 bool privileged)
8999{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009000 struct rpc_message msg = {
9001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009002 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009003 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009004 struct rpc_task_setup task_setup = {
9005 .rpc_client = server->client,
9006 .rpc_message = &msg,
9007 .callback_ops = &nfs41_free_stateid_ops,
9008 .flags = RPC_TASK_ASYNC,
9009 };
9010 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009011
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009012 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9013 &task_setup.rpc_client, &msg);
9014
Chuck Lever38527b12012-07-11 16:30:23 -04009015 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009016 data = kmalloc(sizeof(*data), GFP_NOFS);
9017 if (!data)
9018 return ERR_PTR(-ENOMEM);
9019 data->server = server;
9020 nfs4_stateid_copy(&data->args.stateid, stateid);
9021
9022 task_setup.callback_data = data;
9023
9024 msg.rpc_argp = &data->args;
9025 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009026 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009027 if (privileged)
9028 nfs4_set_sequence_privileged(&data->args.seq_args);
9029
9030 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009031}
9032
Chuck Lever38527b12012-07-11 16:30:23 -04009033/**
9034 * nfs41_free_stateid - perform a FREE_STATEID operation
9035 *
9036 * @server: server / transport on which to perform the operation
9037 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009038 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009039 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009040 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009041 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009042 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009043static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009044 const nfs4_stateid *stateid,
9045 struct rpc_cred *cred,
9046 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009047{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009048 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009049
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009050 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009051 if (IS_ERR(task))
9052 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009053 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009054 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009055}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009056
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009057static void
9058nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009059{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009060 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009061
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009062 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009063 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009064}
9065
Trond Myklebust36281ca2012-03-04 18:13:56 -05009066static bool nfs41_match_stateid(const nfs4_stateid *s1,
9067 const nfs4_stateid *s2)
9068{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009069 if (s1->type != s2->type)
9070 return false;
9071
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009072 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009073 return false;
9074
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009075 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009076 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009077
Anna Schumaker045c5512017-01-11 16:59:48 -05009078 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009079}
9080
Andy Adamson557134a2009-04-01 09:21:53 -04009081#endif /* CONFIG_NFS_V4_1 */
9082
Trond Myklebust36281ca2012-03-04 18:13:56 -05009083static bool nfs4_match_stateid(const nfs4_stateid *s1,
9084 const nfs4_stateid *s2)
9085{
Trond Myklebustf597c532012-03-04 18:13:56 -05009086 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009087}
9088
9089
Trond Myklebust17280172012-03-11 13:11:00 -04009090static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009091 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009092 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009093 .recover_open = nfs4_open_reclaim,
9094 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009095 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009096 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009097};
9098
Andy Adamson591d71c2009-04-01 09:22:47 -04009099#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009100static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009101 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9102 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9103 .recover_open = nfs4_open_reclaim,
9104 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009105 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009106 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009107 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009108};
9109#endif /* CONFIG_NFS_V4_1 */
9110
Trond Myklebust17280172012-03-11 13:11:00 -04009111static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009112 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009113 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009114 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009115 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009116 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009117};
9118
Andy Adamson591d71c2009-04-01 09:22:47 -04009119#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009120static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009121 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9122 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009123 .recover_open = nfs41_open_expired,
9124 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009125 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009126};
9127#endif /* CONFIG_NFS_V4_1 */
9128
Trond Myklebust17280172012-03-11 13:11:00 -04009129static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009130 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009131 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009132 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009133};
9134
9135#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009136static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009137 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009138 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009139 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009140};
9141#endif
9142
Chuck Leverec011fe2013-10-17 14:12:39 -04009143static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009144 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009145 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009146};
9147
9148#if defined(CONFIG_NFS_V4_1)
9149static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009150 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009151 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009152};
9153#endif /* CONFIG_NFS_V4_1 */
9154
Trond Myklebust97dc1352010-06-16 09:52:26 -04009155static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9156 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009157 .init_caps = NFS_CAP_READDIRPLUS
9158 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009159 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009160 .init_client = nfs40_init_client,
9161 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009162 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009163 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009164 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009165 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009166 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009167 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009168 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9169 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9170 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009171 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009172};
9173
9174#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009175static struct nfs_seqid *
9176nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9177{
9178 return NULL;
9179}
9180
Trond Myklebust97dc1352010-06-16 09:52:26 -04009181static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9182 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009183 .init_caps = NFS_CAP_READDIRPLUS
9184 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009185 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009186 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009187 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009188 .init_client = nfs41_init_client,
9189 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009190 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009191 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009192 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009193 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009194 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009195 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009196 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009197 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9198 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9199 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009200 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009201};
9202#endif
9203
Steve Dickson42c2c422013-05-22 12:50:38 -04009204#if defined(CONFIG_NFS_V4_2)
9205static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9206 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009207 .init_caps = NFS_CAP_READDIRPLUS
9208 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009209 | NFS_CAP_POSIX_LOCK
9210 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009211 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009212 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009213 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009214 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009215 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009216 | NFS_CAP_LAYOUTSTATS
9217 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009218 .init_client = nfs41_init_client,
9219 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009220 .match_stateid = nfs41_match_stateid,
9221 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009222 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009223 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009224 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009225 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009226 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009227 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9228 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9229 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009230 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009231};
9232#endif
9233
Trond Myklebust97dc1352010-06-16 09:52:26 -04009234const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9235 [0] = &nfs_v4_0_minor_ops,
9236#if defined(CONFIG_NFS_V4_1)
9237 [1] = &nfs_v4_1_minor_ops,
9238#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009239#if defined(CONFIG_NFS_V4_2)
9240 [2] = &nfs_v4_2_minor_ops,
9241#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009242};
9243
Trond Myklebust13997822016-07-24 17:10:52 -04009244static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009245{
9246 ssize_t error, error2;
9247
9248 error = generic_listxattr(dentry, list, size);
9249 if (error < 0)
9250 return error;
9251 if (list) {
9252 list += error;
9253 size -= error;
9254 }
9255
9256 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9257 if (error2 < 0)
9258 return error2;
9259 return error + error2;
9260}
9261
Trond Myklebust17f26b12013-08-21 15:48:42 -04009262static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009263 .create = nfs_create,
9264 .lookup = nfs_lookup,
9265 .atomic_open = nfs_atomic_open,
9266 .link = nfs_link,
9267 .unlink = nfs_unlink,
9268 .symlink = nfs_symlink,
9269 .mkdir = nfs_mkdir,
9270 .rmdir = nfs_rmdir,
9271 .mknod = nfs_mknod,
9272 .rename = nfs_rename,
9273 .permission = nfs_permission,
9274 .getattr = nfs_getattr,
9275 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009276 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009277};
9278
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009279static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009280 .permission = nfs_permission,
9281 .getattr = nfs_getattr,
9282 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009283 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009284};
9285
David Howells509de812006-08-22 20:06:11 -04009286const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009287 .version = 4, /* protocol version */
9288 .dentry_ops = &nfs4_dentry_operations,
9289 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009290 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009291 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009292 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009293 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009294 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009295 .getattr = nfs4_proc_getattr,
9296 .setattr = nfs4_proc_setattr,
9297 .lookup = nfs4_proc_lookup,
9298 .access = nfs4_proc_access,
9299 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009300 .create = nfs4_proc_create,
9301 .remove = nfs4_proc_remove,
9302 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009303 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009304 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009305 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009306 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009307 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009308 .link = nfs4_proc_link,
9309 .symlink = nfs4_proc_symlink,
9310 .mkdir = nfs4_proc_mkdir,
9311 .rmdir = nfs4_proc_remove,
9312 .readdir = nfs4_proc_readdir,
9313 .mknod = nfs4_proc_mknod,
9314 .statfs = nfs4_proc_statfs,
9315 .fsinfo = nfs4_proc_fsinfo,
9316 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009317 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009318 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009319 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009320 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009321 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009322 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009323 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009324 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009325 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009326 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009327 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009328 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009329 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009330 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009331 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009332 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009333 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009334 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009335 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009336 .create_server = nfs4_create_server,
9337 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009338};
9339
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009340static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009341 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009342 .list = nfs4_xattr_list_nfs4_acl,
9343 .get = nfs4_xattr_get_nfs4_acl,
9344 .set = nfs4_xattr_set_nfs4_acl,
9345};
9346
9347const struct xattr_handler *nfs4_xattr_handlers[] = {
9348 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009349#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9350 &nfs4_xattr_nfs4_label_handler,
9351#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009352 NULL
9353};
9354
Linus Torvalds1da177e2005-04-16 15:20:36 -07009355/*
9356 * Local variables:
9357 * c-basic-offset: 8
9358 * End:
9359 */