blob: a74e1a4e32648631f71bc7cdbe9b17c9b79e605d [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;
580
581 if (flavor == RPC_AUTH_GSS_KRB5I ||
582 flavor == RPC_AUTH_GSS_KRB5P)
583 return true;
584
585 return false;
586}
Trond Myklebust65de8722008-12-23 15:21:44 -0500587
Trond Myklebust452e9352010-07-31 14:29:06 -0400588static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 spin_lock(&clp->cl_lock);
591 if (time_before(clp->cl_last_renewal,timestamp))
592 clp->cl_last_renewal = timestamp;
593 spin_unlock(&clp->cl_lock);
594}
595
Trond Myklebust452e9352010-07-31 14:29:06 -0400596static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
597{
Trond Myklebustbe824162015-07-05 14:50:46 -0400598 struct nfs_client *clp = server->nfs_client;
599
600 if (!nfs4_has_session(clp))
601 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400602}
603
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400604struct nfs4_call_sync_data {
605 const struct nfs_server *seq_server;
606 struct nfs4_sequence_args *seq_args;
607 struct nfs4_sequence_res *seq_res;
608};
609
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800610void nfs4_init_sequence(struct nfs4_sequence_args *args,
611 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400612{
613 args->sa_slot = NULL;
614 args->sa_cache_this = cache_reply;
615 args->sa_privileged = 0;
616
617 res->sr_slot = NULL;
618}
619
620static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
621{
622 args->sa_privileged = 1;
623}
624
Anna Schumaker6de7e122017-01-09 16:51:52 -0500625static int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
626 struct nfs4_sequence_args *args,
627 struct nfs4_sequence_res *res,
628 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400629{
Chuck Lever3bd23842013-08-09 12:49:19 -0400630 struct nfs4_slot *slot;
631
Chuck Lever3bd23842013-08-09 12:49:19 -0400632 slot = nfs4_alloc_slot(tbl);
633 if (IS_ERR(slot)) {
634 if (slot == ERR_PTR(-ENOMEM))
635 task->tk_timeout = HZ >> 2;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500636 return -EAGAIN;
Chuck Lever3bd23842013-08-09 12:49:19 -0400637 }
Chuck Lever3bd23842013-08-09 12:49:19 -0400638
Trond Myklebust0a014a42016-09-22 13:38:51 -0400639 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400640 args->sa_slot = slot;
641 res->sr_slot = slot;
Chuck Lever3bd23842013-08-09 12:49:19 -0400642 return 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400643}
644
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400645static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400646{
647 struct nfs4_slot *slot = res->sr_slot;
648 struct nfs4_slot_table *tbl;
649
Chuck Lever3bd23842013-08-09 12:49:19 -0400650 tbl = slot->table;
651 spin_lock(&tbl->slot_tbl_lock);
652 if (!nfs41_wake_and_assign_slot(tbl, slot))
653 nfs4_free_slot(tbl, slot);
654 spin_unlock(&tbl->slot_tbl_lock);
655
656 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400657}
658
659static int nfs40_sequence_done(struct rpc_task *task,
660 struct nfs4_sequence_res *res)
661{
662 if (res->sr_slot != NULL)
663 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400664 return 1;
665}
666
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400667#if defined(CONFIG_NFS_V4_1)
668
Trond Myklebustd185a332010-06-16 09:52:25 -0400669static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400670{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500671 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400672 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500673 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500674 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400675
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500676 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500677 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500678
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400679 /* Bump the slot sequence number */
680 if (slot->seq_done)
681 slot->seq_nr++;
682 slot->seq_done = 0;
683
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500684 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500685 /* Be nice to the server: try to ensure that the last transmitted
686 * value for highest_user_slotid <= target_highest_slotid
687 */
688 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
689 send_new_highest_used_slotid = true;
690
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500691 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500692 send_new_highest_used_slotid = false;
693 goto out_unlock;
694 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500695 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500696
697 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
698 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500699out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500700 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400701 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500702 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400703 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400704 if (waitqueue_active(&tbl->slot_waitq))
705 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400706}
707
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400708static int nfs41_sequence_process(struct rpc_task *task,
709 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400710{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500711 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500712 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400713 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500714 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500715 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400716
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500717 if (slot == NULL)
718 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400719 /* don't increment the sequence number if the task wasn't sent */
720 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400721 goto out;
722
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500723 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500724
Trond Myklebustac20d162012-12-15 15:36:07 -0500725 if (slot->interrupted) {
726 slot->interrupted = 0;
727 interrupted = true;
728 }
729
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400730 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500731 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400732 switch (res->sr_status) {
733 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400734 /* If previous op on slot was interrupted and we reused
735 * the seq# and got a reply from the cache, then retry
736 */
737 if (task->tk_status == -EREMOTEIO && interrupted) {
738 ++slot->seq_nr;
739 goto retry_nowait;
740 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400741 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400742 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500743 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500744 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500745 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400746 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
747 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500748 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400749 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500750 case 1:
751 /*
752 * sr_status remains 1 if an RPC level error occurred.
753 * The server may or may not have processed the sequence
754 * operation..
755 * Mark the slot as having hosted an interrupted RPC call.
756 */
757 slot->interrupted = 1;
758 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400759 case -NFS4ERR_DELAY:
760 /* The server detected a resend of the RPC call and
761 * returned NFS4ERR_DELAY as per Section 2.10.6.2
762 * of RFC5661.
763 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500764 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400765 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500766 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500767 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400768 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500769 case -NFS4ERR_BADSLOT:
770 /*
771 * The slot id we used was probably retired. Try again
772 * using a different slot id.
773 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500774 goto retry_nowait;
775 case -NFS4ERR_SEQ_MISORDERED:
776 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500777 * Was the last operation on this sequence interrupted?
778 * If so, retry after bumping the sequence number.
779 */
780 if (interrupted) {
781 ++slot->seq_nr;
782 goto retry_nowait;
783 }
784 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500785 * Could this slot have been previously retired?
786 * If so, then the server may be expecting seq_nr = 1!
787 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500788 if (slot->seq_nr != 1) {
789 slot->seq_nr = 1;
790 goto retry_nowait;
791 }
792 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500793 case -NFS4ERR_SEQ_FALSE_RETRY:
794 ++slot->seq_nr;
795 goto retry_nowait;
Trond Myklebust2cf10cd2016-12-04 19:26:40 -0500796 case -NFS4ERR_DEADSESSION:
797 case -NFS4ERR_BADSESSION:
798 nfs4_schedule_session_recovery(session, res->sr_status);
799 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400800 default:
801 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400802 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400803 }
804out:
805 /* The session may be reset by one of the error handlers. */
806 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500807out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500808 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500809retry_nowait:
810 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400811 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500812 task->tk_status = 0;
813 ret = 0;
814 }
815 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400816out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400817 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400818 goto out;
819 rpc_delay(task, NFS4_POLL_RETRY_MAX);
820 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400821}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400822
823int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
824{
825 if (!nfs41_sequence_process(task, res))
826 return 0;
827 if (res->sr_slot != NULL)
828 nfs41_sequence_free_slot(res);
829 return 1;
830
831}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500832EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400833
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400834static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
835{
836 if (res->sr_slot == NULL)
837 return 1;
838 if (res->sr_slot->table->session != NULL)
839 return nfs41_sequence_process(task, res);
840 return nfs40_sequence_done(task, res);
841}
842
843static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
844{
845 if (res->sr_slot != NULL) {
846 if (res->sr_slot->table->session != NULL)
847 nfs41_sequence_free_slot(res);
848 else
849 nfs40_sequence_free_slot(res);
850 }
851}
852
Peng Tao2c4b1312014-06-11 05:24:16 +0800853int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400854{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500855 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400856 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400857 if (!res->sr_slot->table->session)
858 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400859 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400860}
Peng Tao2c4b1312014-06-11 05:24:16 +0800861EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400862
Anna Schumaker6de7e122017-01-09 16:51:52 -0500863static int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400864 struct nfs4_sequence_args *args,
865 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400866 struct rpc_task *task)
867{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400868 struct nfs4_slot *slot;
869 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400870
871 dprintk("--> %s\n", __func__);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400872 tbl = &session->fc_slot_table;
873
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500874 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500875 if (IS_ERR(slot)) {
876 /* If out of memory, try again in 1/4 second */
877 if (slot == ERR_PTR(-ENOMEM))
878 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400879 dprintk("<-- %s: no free slots\n", __func__);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500880 return -EAGAIN;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400881 }
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400882
Trond Myklebust0a014a42016-09-22 13:38:51 -0400883 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500884 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400885
Chuck Levere8d92382013-08-09 12:47:51 -0400886 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500887 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400888
Benny Halevydfb4f3092010-09-24 09:17:01 -0400889 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500890 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400891 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400892 /*
893 * sr_status is only set in decode_sequence, and so will remain
894 * set to 1 if an rpc level failure occurs.
895 */
896 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400897 trace_nfs4_setup_sequence(session, args);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400898 return 0;
899}
900
Andy Adamsonce5039c2009-04-01 09:22:13 -0400901static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
902{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400903 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400904
Trond Myklebust035168ab2010-06-16 09:52:26 -0400905 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
906
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500907 nfs4_setup_sequence(data->seq_server->nfs_client,
908 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400909}
910
Andy Adamson69ab40c2009-04-01 09:22:19 -0400911static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
912{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400913 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400914
Trond Myklebust14516c32010-07-31 14:29:06 -0400915 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400916}
917
Trond Myklebust17280172012-03-11 13:11:00 -0400918static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400919 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400920 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400921};
922
Chuck Lever3bd23842013-08-09 12:49:19 -0400923#else /* !CONFIG_NFS_V4_1 */
924
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400925static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
926{
927 return nfs40_sequence_done(task, res);
928}
929
930static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
931{
932 if (res->sr_slot != NULL)
933 nfs40_sequence_free_slot(res);
934}
935
Peng Tao2c4b1312014-06-11 05:24:16 +0800936int nfs4_sequence_done(struct rpc_task *task,
937 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400938{
Chuck Lever3bd23842013-08-09 12:49:19 -0400939 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400940}
Peng Tao2c4b1312014-06-11 05:24:16 +0800941EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400942
943#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400944
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500945int nfs4_setup_sequence(const struct nfs_client *client,
946 struct nfs4_sequence_args *args,
947 struct nfs4_sequence_res *res,
948 struct rpc_task *task)
949{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500950 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500951 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500952 int ret;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500953
Anna Schumaker9dd91072017-01-10 12:01:46 -0500954 /* slot already allocated? */
955 if (res->sr_slot != NULL)
956 goto out_start;
957
Anna Schumaker76ee0352017-01-10 16:49:31 -0500958 if (session) {
959 tbl = &session->fc_slot_table;
960 task->tk_timeout = 0;
961 }
962
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500963 spin_lock(&tbl->slot_tbl_lock);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500964 /* The state manager will wait until the slot table is empty */
965 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
966 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500967
Anna Schumaker9dd91072017-01-10 12:01:46 -0500968#if defined(CONFIG_NFS_V4_1)
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500969 if (session)
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500970 ret = nfs41_setup_sequence(session, args, res, task);
971 else
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500972#endif /* CONFIG_NFS_V4_1 */
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500973 ret = nfs40_setup_sequence(client->cl_slot_tbl, args, res, task);
974
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500975 if (ret == -EAGAIN)
976 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500977 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500978
Anna Schumaker9dd91072017-01-10 12:01:46 -0500979out_start:
980 rpc_call_start(task);
981 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500982
983out_sleep:
984 if (args->sa_privileged)
985 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
986 NULL, RPC_PRIORITY_PRIVILEGED);
987 else
988 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
989 spin_unlock(&tbl->slot_tbl_lock);
990 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500991}
992EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
993
Chuck Lever9915ea72013-08-09 12:48:27 -0400994static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
995{
996 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500997 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -0400998 data->seq_args, data->seq_res, task);
999}
1000
1001static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1002{
1003 struct nfs4_call_sync_data *data = calldata;
1004 nfs4_sequence_done(task, data->seq_res);
1005}
1006
1007static const struct rpc_call_ops nfs40_call_sync_ops = {
1008 .rpc_call_prepare = nfs40_call_sync_prepare,
1009 .rpc_call_done = nfs40_call_sync_done,
1010};
1011
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001012static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001013 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001014 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001015 struct nfs4_sequence_args *args,
1016 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001017{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001018 int ret;
1019 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001020 struct nfs_client *clp = server->nfs_client;
1021 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001022 .seq_server = server,
1023 .seq_args = args,
1024 .seq_res = res,
1025 };
1026 struct rpc_task_setup task_setup = {
1027 .rpc_client = clnt,
1028 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001029 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001030 .callback_data = &data
1031 };
1032
1033 task = rpc_run_task(&task_setup);
1034 if (IS_ERR(task))
1035 ret = PTR_ERR(task);
1036 else {
1037 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001038 rpc_put_task(task);
1039 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001040 return ret;
1041}
1042
Bryan Schumaker7c513052011-03-24 17:12:24 +00001043int nfs4_call_sync(struct rpc_clnt *clnt,
1044 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001045 struct rpc_message *msg,
1046 struct nfs4_sequence_args *args,
1047 struct nfs4_sequence_res *res,
1048 int cache_reply)
1049{
Chuck Levera9c92d62013-08-09 12:48:18 -04001050 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001051 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001052}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001054static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1055 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
1057 struct nfs_inode *nfsi = NFS_I(dir);
1058
1059 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001060 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001061 if (cinfo->atomic && cinfo->before == dir->i_version) {
1062 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1063 nfsi->attrtimeo_timestamp = jiffies;
1064 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 nfs_force_lookup_revalidate(dir);
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001066 if (cinfo->before != dir->i_version)
1067 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1068 NFS_INO_INVALID_ACL;
1069 }
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001070 dir->i_version = cinfo->after;
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001071 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001072 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001073 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 spin_unlock(&dir->i_lock);
1075}
1076
1077struct nfs4_opendata {
1078 struct kref kref;
1079 struct nfs_openargs o_arg;
1080 struct nfs_openres o_res;
1081 struct nfs_open_confirmargs c_arg;
1082 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001083 struct nfs4_string owner_name;
1084 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001085 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001087 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001089 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 struct nfs4_state_owner *owner;
1091 struct nfs4_state *state;
1092 struct iattr attrs;
1093 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001094 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001095 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001096 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001097 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 int cancelled;
1099};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001100
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001101static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1102 int err, struct nfs4_exception *exception)
1103{
1104 if (err != -EINVAL)
1105 return false;
1106 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1107 return false;
1108 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1109 exception->retry = 1;
1110 return true;
1111}
1112
Trond Myklebust6ae37332015-01-30 14:21:14 -05001113static u32
1114nfs4_map_atomic_open_share(struct nfs_server *server,
1115 fmode_t fmode, int openflags)
1116{
1117 u32 res = 0;
1118
1119 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1120 case FMODE_READ:
1121 res = NFS4_SHARE_ACCESS_READ;
1122 break;
1123 case FMODE_WRITE:
1124 res = NFS4_SHARE_ACCESS_WRITE;
1125 break;
1126 case FMODE_READ|FMODE_WRITE:
1127 res = NFS4_SHARE_ACCESS_BOTH;
1128 }
1129 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1130 goto out;
1131 /* Want no delegation if we're using O_DIRECT */
1132 if (openflags & O_DIRECT)
1133 res |= NFS4_SHARE_WANT_NO_DELEG;
1134out:
1135 return res;
1136}
1137
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001138static enum open_claim_type4
1139nfs4_map_atomic_open_claim(struct nfs_server *server,
1140 enum open_claim_type4 claim)
1141{
1142 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1143 return claim;
1144 switch (claim) {
1145 default:
1146 return claim;
1147 case NFS4_OPEN_CLAIM_FH:
1148 return NFS4_OPEN_CLAIM_NULL;
1149 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1150 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1151 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1152 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1153 }
1154}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001157{
1158 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001159 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001160 p->o_res.seqid = p->o_arg.seqid;
1161 p->c_res.seqid = p->c_arg.seqid;
1162 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001163 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001164 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001165 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001166}
1167
Al Viro82a2c1b2011-06-22 18:30:55 -04001168static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001169 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001170 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001171 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001172 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001173 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001174{
Al Viro82a2c1b2011-06-22 18:30:55 -04001175 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001176 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001177 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001178 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001179 struct nfs4_opendata *p;
1180
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001181 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001182 if (p == NULL)
1183 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001184
1185 p->f_label = nfs4_label_alloc(server, gfp_mask);
1186 if (IS_ERR(p->f_label))
1187 goto err_free_p;
1188
Kinglong Meea49c2692015-07-27 15:31:38 +08001189 p->a_label = nfs4_label_alloc(server, gfp_mask);
1190 if (IS_ERR(p->a_label))
1191 goto err_free_f;
1192
Trond Myklebust63f5f792015-01-23 19:19:25 -05001193 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1194 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001195 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001196 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001197 nfs_sb_active(dentry->d_sb);
1198 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001199 p->dir = parent;
1200 p->owner = sp;
1201 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001202 p->o_arg.open_flags = flags;
1203 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001204 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001205 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001206 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1207 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001208 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1209 * will return permission denied for all bits until close */
1210 if (!(flags & O_EXCL)) {
1211 /* ask server to check for all possible rights as results
1212 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001213 switch (p->o_arg.claim) {
1214 default:
1215 break;
1216 case NFS4_OPEN_CLAIM_NULL:
1217 case NFS4_OPEN_CLAIM_FH:
1218 p->o_arg.access = NFS4_ACCESS_READ |
1219 NFS4_ACCESS_MODIFY |
1220 NFS4_ACCESS_EXTEND |
1221 NFS4_ACCESS_EXECUTE;
1222 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001223 }
David Howells7539bba2006-08-22 20:06:09 -04001224 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001225 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1226 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001227 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001228 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001229 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001230 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001231 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001232 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001233 case NFS4_OPEN_CLAIM_NULL:
1234 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1235 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1236 p->o_arg.fh = NFS_FH(dir);
1237 break;
1238 case NFS4_OPEN_CLAIM_PREVIOUS:
1239 case NFS4_OPEN_CLAIM_FH:
1240 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1241 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001242 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001243 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001244 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001245 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001246
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001247 p->o_arg.u.attrs = &p->attrs;
1248 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001249
1250 verf[0] = jiffies;
1251 verf[1] = current->pid;
1252 memcpy(p->o_arg.u.verifier.data, verf,
1253 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001254 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001255 p->c_arg.fh = &p->o_res.fh;
1256 p->c_arg.stateid = &p->o_res.stateid;
1257 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001258 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001259 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001260 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001261
1262err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001263 nfs4_label_free(p->a_label);
1264err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001265 nfs4_label_free(p->f_label);
1266err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001267 kfree(p);
1268err:
1269 dput(parent);
1270 return NULL;
1271}
1272
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001273static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001274{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001275 struct nfs4_opendata *p = container_of(kref,
1276 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001277 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001278
1279 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001280 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001281 if (p->state != NULL)
1282 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001283 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001284
Kinglong Meea49c2692015-07-27 15:31:38 +08001285 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001286 nfs4_label_free(p->f_label);
1287
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001288 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001289 dput(p->dentry);
1290 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001291 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001292 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001293 kfree(p);
1294}
1295
1296static void nfs4_opendata_put(struct nfs4_opendata *p)
1297{
1298 if (p != NULL)
1299 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001300}
1301
Trond Myklebust06f814a2006-01-03 09:55:07 +01001302static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1303{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001304 int ret;
1305
Trond Myklebust06f814a2006-01-03 09:55:07 +01001306 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001307 return ret;
1308}
1309
Trond Myklebust24311f82015-09-20 10:50:17 -04001310static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1311 fmode_t fmode)
1312{
1313 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1314 case FMODE_READ|FMODE_WRITE:
1315 return state->n_rdwr != 0;
1316 case FMODE_WRITE:
1317 return state->n_wronly != 0;
1318 case FMODE_READ:
1319 return state->n_rdonly != 0;
1320 }
1321 WARN_ON_ONCE(1);
1322 return false;
1323}
1324
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001325static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001326{
1327 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001328
Trond Myklebust536e43d2012-01-17 22:04:26 -05001329 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001330 goto out;
1331 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001332 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001333 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1334 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001335 break;
1336 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001337 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1338 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001339 break;
1340 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001341 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1342 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001343 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001344out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001345 return ret;
1346}
1347
Trond Myklebust2a606182015-08-19 22:30:00 -05001348static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1349 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001350{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001351 if (delegation == NULL)
1352 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001353 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001354 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001355 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1356 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001357 switch (claim) {
1358 case NFS4_OPEN_CLAIM_NULL:
1359 case NFS4_OPEN_CLAIM_FH:
1360 break;
1361 case NFS4_OPEN_CLAIM_PREVIOUS:
1362 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1363 break;
1364 default:
1365 return 0;
1366 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001367 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001368 return 1;
1369}
1370
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001371static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001372{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001373 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001374 case FMODE_WRITE:
1375 state->n_wronly++;
1376 break;
1377 case FMODE_READ:
1378 state->n_rdonly++;
1379 break;
1380 case FMODE_READ|FMODE_WRITE:
1381 state->n_rdwr++;
1382 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001383 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001384}
1385
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001386#ifdef CONFIG_NFS_V4_1
1387static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1388{
1389 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1390 return true;
1391 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1392 return true;
1393 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1394 return true;
1395 return false;
1396}
1397#endif /* CONFIG_NFS_V4_1 */
1398
Trond Myklebust4f14c192014-02-12 19:15:06 -05001399static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001400{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001401 struct nfs_client *clp = state->owner->so_server->nfs_client;
1402 bool need_recover = false;
1403
1404 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1405 need_recover = true;
1406 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1407 need_recover = true;
1408 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1409 need_recover = true;
1410 if (need_recover)
1411 nfs4_state_mark_reclaim_nograce(clp, state);
1412}
1413
Trond Myklebuste999e802014-02-10 18:20:47 -05001414static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001415 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001416{
1417 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1418 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001419 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001420 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001421 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001422 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001423 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001424 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1425 return true;
1426 return false;
1427}
1428
Trond Myklebustf95549c2015-01-23 18:06:09 -05001429static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1430{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001431 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1432 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001433 if (state->n_wronly)
1434 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1435 if (state->n_rdonly)
1436 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1437 if (state->n_rdwr)
1438 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001439 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001440}
1441
Trond Myklebust226056c2014-02-11 10:41:07 -05001442static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1443 nfs4_stateid *stateid, fmode_t fmode)
1444{
1445 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1446 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1447 case FMODE_WRITE:
1448 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1449 break;
1450 case FMODE_READ:
1451 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1452 break;
1453 case 0:
1454 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1455 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1456 clear_bit(NFS_OPEN_STATE, &state->flags);
1457 }
1458 if (stateid == NULL)
1459 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001460 /* Handle OPEN+OPEN_DOWNGRADE races */
1461 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1462 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001463 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001464 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001465 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001466 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001467 nfs4_stateid_copy(&state->stateid, stateid);
1468 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001469}
1470
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001471static void nfs_clear_open_stateid(struct nfs4_state *state,
1472 nfs4_stateid *arg_stateid,
1473 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001474{
1475 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001476 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1477 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1478 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001479 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001480 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1481 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001482}
1483
Trond Myklebust1393d962016-09-22 13:39:13 -04001484static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1485 const nfs4_stateid *stateid, fmode_t fmode,
1486 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001487{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001488 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001489 case FMODE_READ:
1490 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1491 break;
1492 case FMODE_WRITE:
1493 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1494 break;
1495 case FMODE_READ|FMODE_WRITE:
1496 set_bit(NFS_O_RDWR_STATE, &state->flags);
1497 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001498 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001499 return;
1500 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1501 nfs4_stateid_copy(&state->stateid, stateid);
1502 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001503}
1504
Trond Myklebust1393d962016-09-22 13:39:13 -04001505static void __update_open_stateid(struct nfs4_state *state,
1506 const nfs4_stateid *open_stateid,
1507 const nfs4_stateid *deleg_stateid,
1508 fmode_t fmode,
1509 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001511 /*
1512 * Protect the call to nfs4_state_set_mode_locked and
1513 * serialise the stateid update
1514 */
Andrew Elble361cad32015-12-02 09:20:57 -05001515 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001516 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001517 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001518 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001519 set_bit(NFS_DELEGATED_STATE, &state->flags);
1520 }
1521 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001522 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001523 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001524 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001525 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527
Trond Myklebust1393d962016-09-22 13:39:13 -04001528static int update_open_stateid(struct nfs4_state *state,
1529 const nfs4_stateid *open_stateid,
1530 const nfs4_stateid *delegation,
1531 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001532{
Trond Myklebust1393d962016-09-22 13:39:13 -04001533 struct nfs_server *server = NFS_SERVER(state->inode);
1534 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001535 struct nfs_inode *nfsi = NFS_I(state->inode);
1536 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001537 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001538 int ret = 0;
1539
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001540 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001541
1542 rcu_read_lock();
1543 deleg_cur = rcu_dereference(nfsi->delegation);
1544 if (deleg_cur == NULL)
1545 goto no_delegation;
1546
1547 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001548 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001549 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001550 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001551 goto no_delegation_unlock;
1552
1553 if (delegation == NULL)
1554 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001555 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001556 goto no_delegation_unlock;
1557
Trond Myklebustb7391f42008-12-23 15:21:52 -05001558 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001559 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1560 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001561 ret = 1;
1562no_delegation_unlock:
1563 spin_unlock(&deleg_cur->lock);
1564no_delegation:
1565 rcu_read_unlock();
1566
1567 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001568 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001569 ret = 1;
1570 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001571 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001572 nfs4_schedule_state_manager(clp);
1573 if (freeme.type != 0)
1574 nfs4_test_and_free_stateid(server, &freeme,
1575 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001576
1577 return ret;
1578}
1579
Trond Myklebust39071e62015-01-24 15:07:56 -05001580static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1581 const nfs4_stateid *stateid)
1582{
1583 struct nfs4_state *state = lsp->ls_state;
1584 bool ret = false;
1585
1586 spin_lock(&state->state_lock);
1587 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1588 goto out_noupdate;
1589 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1590 goto out_noupdate;
1591 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1592 ret = true;
1593out_noupdate:
1594 spin_unlock(&state->state_lock);
1595 return ret;
1596}
Trond Myklebust34310432008-12-23 15:21:38 -05001597
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001598static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001599{
1600 struct nfs_delegation *delegation;
1601
1602 rcu_read_lock();
1603 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001604 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001605 rcu_read_unlock();
1606 return;
1607 }
1608 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001609 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001610}
1611
Trond Myklebust6ee41262007-07-08 14:11:36 -04001612static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001613{
1614 struct nfs4_state *state = opendata->state;
1615 struct nfs_inode *nfsi = NFS_I(state->inode);
1616 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001617 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001618 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001619 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001620 nfs4_stateid stateid;
1621 int ret = -EAGAIN;
1622
Trond Myklebustaac00a82007-07-05 19:02:21 -04001623 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001624 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001625 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001626 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001627 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001628 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001629 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001630 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001631 rcu_read_lock();
1632 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001633 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001634 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001635 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001636 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001637 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001638 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001639 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001640 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001641 if (!opendata->is_recover) {
1642 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1643 if (ret != 0)
1644 goto out;
1645 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001646 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001647
1648 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001649 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001650 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001651 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001652out:
1653 return ERR_PTR(ret);
1654out_return_state:
1655 atomic_inc(&state->count);
1656 return state;
1657}
1658
Andy Adamsone23008e2012-10-02 21:07:32 -04001659static void
1660nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1661{
1662 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1663 struct nfs_delegation *delegation;
1664 int delegation_flags = 0;
1665
1666 rcu_read_lock();
1667 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1668 if (delegation)
1669 delegation_flags = delegation->flags;
1670 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001671 switch (data->o_arg.claim) {
1672 default:
1673 break;
1674 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1675 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001676 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1677 "returning a delegation for "
1678 "OPEN(CLAIM_DELEGATE_CUR)\n",
1679 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001680 return;
1681 }
1682 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001683 nfs_inode_set_delegation(state->inode,
1684 data->owner->so_cred,
1685 &data->o_res);
1686 else
1687 nfs_inode_reclaim_delegation(state->inode,
1688 data->owner->so_cred,
1689 &data->o_res);
1690}
1691
1692/*
1693 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1694 * and update the nfs4_state.
1695 */
1696static struct nfs4_state *
1697_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1698{
1699 struct inode *inode = data->state->inode;
1700 struct nfs4_state *state = data->state;
1701 int ret;
1702
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001703 if (!data->rpc_done) {
1704 if (data->rpc_status) {
1705 ret = data->rpc_status;
1706 goto err;
1707 }
1708 /* cached opens have already been processed */
1709 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001710 }
1711
Andy Adamsone23008e2012-10-02 21:07:32 -04001712 ret = nfs_refresh_inode(inode, &data->f_attr);
1713 if (ret)
1714 goto err;
1715
1716 if (data->o_res.delegation_type != 0)
1717 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001718update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001719 update_open_stateid(state, &data->o_res.stateid, NULL,
1720 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001721 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001722
1723 return state;
1724err:
1725 return ERR_PTR(ret);
1726
1727}
1728
1729static struct nfs4_state *
1730_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001731{
1732 struct inode *inode;
1733 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001734 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001735
Trond Myklebustaac00a82007-07-05 19:02:21 -04001736 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001737 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001738 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001739 goto out;
1740 }
1741
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001742 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001743 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001744 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001745 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001746 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001747 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001748 goto err;
1749 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001750 state = nfs4_get_open_state(inode, data->owner);
1751 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001752 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001753 if (data->o_res.delegation_type != 0)
1754 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001755 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001756 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001757 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001758out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001759 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001760 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001761err_put_inode:
1762 iput(inode);
1763err:
1764 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001765}
1766
Andy Adamsone23008e2012-10-02 21:07:32 -04001767static struct nfs4_state *
1768nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1769{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001770 struct nfs4_state *ret;
1771
Andy Adamsone23008e2012-10-02 21:07:32 -04001772 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001773 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1774 else
1775 ret = _nfs4_opendata_to_nfs4_state(data);
1776 nfs4_sequence_free_slot(&data->o_res.seq_res);
1777 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001778}
1779
Trond Myklebust864472e2006-01-03 09:55:15 +01001780static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1781{
1782 struct nfs_inode *nfsi = NFS_I(state->inode);
1783 struct nfs_open_context *ctx;
1784
1785 spin_lock(&state->inode->i_lock);
1786 list_for_each_entry(ctx, &nfsi->open_files, list) {
1787 if (ctx->state != state)
1788 continue;
1789 get_nfs_open_context(ctx);
1790 spin_unlock(&state->inode->i_lock);
1791 return ctx;
1792 }
1793 spin_unlock(&state->inode->i_lock);
1794 return ERR_PTR(-ENOENT);
1795}
1796
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001797static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1798 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001799{
1800 struct nfs4_opendata *opendata;
1801
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001802 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001803 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001804 if (opendata == NULL)
1805 return ERR_PTR(-ENOMEM);
1806 opendata->state = state;
1807 atomic_inc(&state->count);
1808 return opendata;
1809}
1810
Trond Myklebust24311f82015-09-20 10:50:17 -04001811static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1812 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001813{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001814 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001815 int ret;
1816
Trond Myklebust24311f82015-09-20 10:50:17 -04001817 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001818 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001819 opendata->o_arg.open_flags = 0;
1820 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001821 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1822 NFS_SB(opendata->dentry->d_sb),
1823 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001824 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1825 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1826 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001827 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001828 if (ret != 0)
1829 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001830 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001831 if (IS_ERR(newstate))
1832 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001833 if (newstate != opendata->state)
1834 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001835 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001836 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001837}
1838
1839static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1840{
Trond Myklebust864472e2006-01-03 09:55:15 +01001841 int ret;
1842
Trond Myklebust4f14c192014-02-12 19:15:06 -05001843 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1844 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1845 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1846 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001847 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001848 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001849 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001850 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001851 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1852 if (ret != 0)
1853 return ret;
1854 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1855 if (ret != 0)
1856 return ret;
1857 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1858 if (ret != 0)
1859 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001860 /*
1861 * We may have performed cached opens for all three recoveries.
1862 * Check if we need to update the current stateid.
1863 */
1864 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001865 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001866 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001867 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001868 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001869 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001870 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001871 return 0;
1872}
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874/*
1875 * OPEN_RECLAIM:
1876 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001878static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001880 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001881 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001882 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 int status;
1884
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001885 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1886 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001887 if (IS_ERR(opendata))
1888 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001889 rcu_read_lock();
1890 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001891 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001892 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001893 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001894 opendata->o_arg.u.delegation_type = delegation_type;
1895 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001896 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 return status;
1898}
1899
Trond Myklebust539cd032007-06-05 11:46:42 -04001900static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
1902 struct nfs_server *server = NFS_SERVER(state->inode);
1903 struct nfs4_exception exception = { };
1904 int err;
1905 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001906 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001907 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001908 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1909 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001910 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001911 break;
1912 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 } while (exception.retry);
1914 return err;
1915}
1916
Trond Myklebust864472e2006-01-03 09:55:15 +01001917static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1918{
1919 struct nfs_open_context *ctx;
1920 int ret;
1921
1922 ctx = nfs4_state_find_open_context(state);
1923 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001924 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001925 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001926 put_nfs_open_context(ctx);
1927 return ret;
1928}
1929
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001930static 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 -07001931{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001932 switch (err) {
1933 default:
1934 printk(KERN_ERR "NFS: %s: unhandled error "
1935 "%d.\n", __func__, err);
1936 case 0:
1937 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001938 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001939 case -ESTALE:
1940 break;
1941 case -NFS4ERR_BADSESSION:
1942 case -NFS4ERR_BADSLOT:
1943 case -NFS4ERR_BAD_HIGH_SLOT:
1944 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1945 case -NFS4ERR_DEADSESSION:
1946 set_bit(NFS_DELEGATED_STATE, &state->flags);
1947 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1948 return -EAGAIN;
1949 case -NFS4ERR_STALE_CLIENTID:
1950 case -NFS4ERR_STALE_STATEID:
1951 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001952 /* Don't recall a delegation if it was lost */
1953 nfs4_schedule_lease_recovery(server->nfs_client);
1954 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001955 case -NFS4ERR_MOVED:
1956 nfs4_schedule_migration_recovery(server);
1957 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001958 case -NFS4ERR_LEASE_MOVED:
1959 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1960 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001961 case -NFS4ERR_DELEG_REVOKED:
1962 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001963 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001964 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001965 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001966 nfs_inode_find_state_and_recover(state->inode,
1967 stateid);
1968 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001969 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001970 case -NFS4ERR_DELAY:
1971 case -NFS4ERR_GRACE:
1972 set_bit(NFS_DELEGATED_STATE, &state->flags);
1973 ssleep(1);
1974 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001975 case -ENOMEM:
1976 case -NFS4ERR_DENIED:
1977 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1978 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 return err;
1981}
1982
Trond Myklebust24311f82015-09-20 10:50:17 -04001983int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1984 struct nfs4_state *state, const nfs4_stateid *stateid,
1985 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001986{
1987 struct nfs_server *server = NFS_SERVER(state->inode);
1988 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001989 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001990
1991 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1992 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1993 if (IS_ERR(opendata))
1994 return PTR_ERR(opendata);
1995 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001996 write_seqlock(&state->seqlock);
1997 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1998 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001999 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2000 switch (type & (FMODE_READ|FMODE_WRITE)) {
2001 case FMODE_READ|FMODE_WRITE:
2002 case FMODE_WRITE:
2003 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2004 if (err)
2005 break;
2006 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2007 if (err)
2008 break;
2009 case FMODE_READ:
2010 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2011 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002012 nfs4_opendata_put(opendata);
2013 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2014}
2015
Chuck Leverbe05c862013-08-09 12:49:47 -04002016static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2017{
2018 struct nfs4_opendata *data = calldata;
2019
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002020 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2021 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002022}
2023
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002024static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2025{
2026 struct nfs4_opendata *data = calldata;
2027
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002028 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002029
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002030 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002031 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002032 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002033 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002034 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002035 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002036 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002037}
2038
2039static void nfs4_open_confirm_release(void *calldata)
2040{
2041 struct nfs4_opendata *data = calldata;
2042 struct nfs4_state *state = NULL;
2043
2044 /* If this request hasn't been cancelled, do nothing */
2045 if (data->cancelled == 0)
2046 goto out_free;
2047 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002048 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002049 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002050 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002051 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002052 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002053out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002054 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002055}
2056
2057static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002058 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002059 .rpc_call_done = nfs4_open_confirm_done,
2060 .rpc_release = nfs4_open_confirm_release,
2061};
2062
2063/*
2064 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2065 */
2066static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2067{
David Howells2b0143b2015-03-17 22:25:59 +00002068 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002069 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002070 struct rpc_message msg = {
2071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2072 .rpc_argp = &data->c_arg,
2073 .rpc_resp = &data->c_res,
2074 .rpc_cred = data->owner->so_cred,
2075 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002076 struct rpc_task_setup task_setup_data = {
2077 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002078 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002079 .callback_ops = &nfs4_open_confirm_ops,
2080 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002081 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002082 .flags = RPC_TASK_ASYNC,
2083 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 int status;
2085
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002086 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002087 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002088 data->rpc_done = 0;
2089 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002090 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002091 if (data->is_recover)
2092 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002093 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002094 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002095 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002096 status = nfs4_wait_for_completion_rpc_task(task);
2097 if (status != 0) {
2098 data->cancelled = 1;
2099 smp_wmb();
2100 } else
2101 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002102 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 return status;
2104}
2105
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002106static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002108 struct nfs4_opendata *data = calldata;
2109 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002110 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002111 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002112
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002113 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002114 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002115 /*
2116 * Check if we still need to send an OPEN call, or if we can use
2117 * a delegation instead.
2118 */
2119 if (data->state != NULL) {
2120 struct nfs_delegation *delegation;
2121
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002122 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002123 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002124 rcu_read_lock();
2125 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002126 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002127 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002128 rcu_read_unlock();
2129 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002130 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002131 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002132 switch (claim) {
2133 default:
2134 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002135 case NFS4_OPEN_CLAIM_PREVIOUS:
2136 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2137 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002138 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002139 case NFS4_OPEN_CLAIM_FH:
2140 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002141 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2142 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002143 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002144 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002145 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002146 &data->o_res.seq_res,
2147 task) != 0)
2148 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002149
2150 /* Set the create mode (note dependency on the session type) */
2151 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2152 if (data->o_arg.open_flags & O_EXCL) {
2153 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2154 if (nfs4_has_persistent_session(clp))
2155 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2156 else if (clp->cl_mvops->minor_version > 0)
2157 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2158 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002159 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002160unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002161 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002162 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002163out_no_action:
2164 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002165out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002166 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002167}
2168
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002169static void nfs4_open_done(struct rpc_task *task, void *calldata)
2170{
2171 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002173 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002174
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002175 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002176 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002177
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002178 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002179 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2180 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002181 case S_IFREG:
2182 break;
2183 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002184 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002185 break;
2186 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002187 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002188 break;
2189 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002190 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002191 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002192 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002193 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002194 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2195 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002196 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002197 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002198}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002199
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002200static void nfs4_open_release(void *calldata)
2201{
2202 struct nfs4_opendata *data = calldata;
2203 struct nfs4_state *state = NULL;
2204
2205 /* If this request hasn't been cancelled, do nothing */
2206 if (data->cancelled == 0)
2207 goto out_free;
2208 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002209 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002210 goto out_free;
2211 /* In case we need an open_confirm, no cleanup! */
2212 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2213 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002214 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002215 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002216 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002217out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002218 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002219}
2220
2221static const struct rpc_call_ops nfs4_open_ops = {
2222 .rpc_call_prepare = nfs4_open_prepare,
2223 .rpc_call_done = nfs4_open_done,
2224 .rpc_release = nfs4_open_release,
2225};
2226
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002227static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002228{
David Howells2b0143b2015-03-17 22:25:59 +00002229 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002230 struct nfs_server *server = NFS_SERVER(dir);
2231 struct nfs_openargs *o_arg = &data->o_arg;
2232 struct nfs_openres *o_res = &data->o_res;
2233 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002234 struct rpc_message msg = {
2235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2236 .rpc_argp = o_arg,
2237 .rpc_resp = o_res,
2238 .rpc_cred = data->owner->so_cred,
2239 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002240 struct rpc_task_setup task_setup_data = {
2241 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002242 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002243 .callback_ops = &nfs4_open_ops,
2244 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002245 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002246 .flags = RPC_TASK_ASYNC,
2247 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002248 int status;
2249
Chuck Levera9c92d62013-08-09 12:48:18 -04002250 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002251 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002252 data->rpc_done = 0;
2253 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002254 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002255 data->is_recover = 0;
2256 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002257 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002258 data->is_recover = 1;
2259 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002260 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002261 if (IS_ERR(task))
2262 return PTR_ERR(task);
2263 status = nfs4_wait_for_completion_rpc_task(task);
2264 if (status != 0) {
2265 data->cancelled = 1;
2266 smp_wmb();
2267 } else
2268 status = data->rpc_status;
2269 rpc_put_task(task);
2270
2271 return status;
2272}
2273
2274static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2275{
David Howells2b0143b2015-03-17 22:25:59 +00002276 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002277 struct nfs_openres *o_res = &data->o_res;
2278 int status;
2279
2280 status = nfs4_run_open_task(data, 1);
2281 if (status != 0 || !data->rpc_done)
2282 return status;
2283
Trond Myklebust6926afd2012-01-07 13:22:46 -05002284 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2285
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002286 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2287 status = _nfs4_proc_open_confirm(data);
2288 if (status != 0)
2289 return status;
2290 }
2291
2292 return status;
2293}
2294
Trond Myklebustf3792d62014-07-10 08:54:32 -04002295/*
2296 * Additional permission checks in order to distinguish between an
2297 * open for read, and an open for execute. This works around the
2298 * fact that NFSv4 OPEN treats read and execute permissions as being
2299 * the same.
2300 * Note that in the non-execute case, we want to turn off permission
2301 * checking if we just created a new file (POSIX open() semantics).
2302 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002303static int nfs4_opendata_access(struct rpc_cred *cred,
2304 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002305 struct nfs4_state *state, fmode_t fmode,
2306 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002307{
2308 struct nfs_access_entry cache;
2309 u32 mask;
2310
2311 /* access call failed or for some reason the server doesn't
2312 * support any access modes -- defer access call until later */
2313 if (opendata->o_res.access_supported == 0)
2314 return 0;
2315
2316 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002317 /*
2318 * Use openflags to check for exec, because fmode won't
2319 * always have FMODE_EXEC set when file open for exec.
2320 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002321 if (openflags & __FMODE_EXEC) {
2322 /* ONLY check for exec rights */
2323 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002324 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002325 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002326
2327 cache.cred = cred;
2328 cache.jiffies = jiffies;
2329 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2330 nfs_access_add_cache(state->inode, &cache);
2331
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002332 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002333 return 0;
2334
2335 /* even though OPEN succeeded, access is denied. Close the file */
2336 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002337 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002338}
2339
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002340/*
2341 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2342 */
2343static int _nfs4_proc_open(struct nfs4_opendata *data)
2344{
David Howells2b0143b2015-03-17 22:25:59 +00002345 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002346 struct nfs_server *server = NFS_SERVER(dir);
2347 struct nfs_openargs *o_arg = &data->o_arg;
2348 struct nfs_openres *o_res = &data->o_res;
2349 int status;
2350
2351 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002352 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002353 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002354 if (status != 0) {
2355 if (status == -NFS4ERR_BADNAME &&
2356 !(o_arg->open_flags & O_CREAT))
2357 return -ENOENT;
2358 return status;
2359 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002360
Trond Myklebust6926afd2012-01-07 13:22:46 -05002361 nfs_fattr_map_and_free_names(server, &data->f_attr);
2362
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002363 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002364 if (o_arg->open_flags & O_EXCL)
2365 data->file_created = 1;
2366 else if (o_res->cinfo.before != o_res->cinfo.after)
2367 data->file_created = 1;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002368 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002369 update_changeattr(dir, &o_res->cinfo,
2370 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002371 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002372 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2373 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002375 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002377 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
2379 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002380 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002381 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
2383
Andy Adamsond83217c2011-03-01 01:34:17 +00002384static int nfs4_recover_expired_lease(struct nfs_server *server)
2385{
2386 return nfs4_client_recover_expired_lease(server->nfs_client);
2387}
2388
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389/*
2390 * OPEN_EXPIRED:
2391 * reclaim state on the server after a network partition.
2392 * Assumes caller holds the appropriate lock
2393 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002394static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002396 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002397 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002399 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002400 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002401 if (IS_ERR(opendata))
2402 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002403 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002404 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002405 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002406 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002407 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002410static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002411{
Trond Myklebust539cd032007-06-05 11:46:42 -04002412 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002413 struct nfs4_exception exception = { };
2414 int err;
2415
2416 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002417 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002418 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002419 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2420 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002421 switch (err) {
2422 default:
2423 goto out;
2424 case -NFS4ERR_GRACE:
2425 case -NFS4ERR_DELAY:
2426 nfs4_handle_exception(server, err, &exception);
2427 err = 0;
2428 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002429 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002430out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002431 return err;
2432}
2433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2435{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002437 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Trond Myklebust864472e2006-01-03 09:55:15 +01002439 ctx = nfs4_state_find_open_context(state);
2440 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002441 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002442 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002443 put_nfs_open_context(ctx);
2444 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445}
2446
Trond Myklebust41020b62016-09-22 13:38:58 -04002447static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2448 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002449{
Trond Myklebust41020b62016-09-22 13:38:58 -04002450 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002451 write_seqlock(&state->seqlock);
2452 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2453 write_sequnlock(&state->seqlock);
2454 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2455}
2456
2457static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2458{
2459 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002460 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002461}
2462
2463static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2464{
2465 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2466 nfs40_clear_delegation_stateid(state);
2467 return nfs4_open_expired(sp, state);
2468}
2469
Trond Myklebust45870d62016-09-22 13:38:59 -04002470static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2471 nfs4_stateid *stateid,
2472 struct rpc_cred *cred)
2473{
2474 return -NFS4ERR_BAD_STATEID;
2475}
2476
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002477#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002478static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2479 nfs4_stateid *stateid,
2480 struct rpc_cred *cred)
2481{
2482 int status;
2483
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002484 switch (stateid->type) {
2485 default:
2486 break;
2487 case NFS4_INVALID_STATEID_TYPE:
2488 case NFS4_SPECIAL_STATEID_TYPE:
2489 return -NFS4ERR_BAD_STATEID;
2490 case NFS4_REVOKED_STATEID_TYPE:
2491 goto out_free;
2492 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002493
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002494 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002495 switch (status) {
2496 case -NFS4ERR_EXPIRED:
2497 case -NFS4ERR_ADMIN_REVOKED:
2498 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002499 break;
2500 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002501 return status;
2502 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002503out_free:
2504 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002505 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002506 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002507}
2508
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002509static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002510{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002511 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002512 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002513 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002514 struct rpc_cred *cred;
2515 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002516
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002517 /* Get the delegation credential for use by test/free_stateid */
2518 rcu_read_lock();
2519 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002520 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002521 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002522 return;
2523 }
2524
2525 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002526 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2527 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002528 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002529 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002530 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002531
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002532 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2533 rcu_read_unlock();
2534 return;
2535 }
2536
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002537 cred = get_rpccred(delegation->cred);
2538 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002539 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002540 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002541 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002542 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002543
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002544 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002545}
2546
2547/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002548 * nfs41_check_expired_locks - possibly free a lock stateid
2549 *
2550 * @state: NFSv4 state for an inode
2551 *
2552 * Returns NFS_OK if recovery for this stateid is now finished.
2553 * Otherwise a negative NFS4ERR value is returned.
2554 */
2555static int nfs41_check_expired_locks(struct nfs4_state *state)
2556{
2557 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002558 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002559 struct nfs_server *server = NFS_SERVER(state->inode);
2560
2561 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2562 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002563
2564 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002565 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2566 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2567 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2568
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002569 atomic_inc(&lsp->ls_count);
2570 spin_unlock(&state->state_lock);
2571
2572 nfs4_put_lock_state(prev);
2573 prev = lsp;
2574
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002575 status = nfs41_test_and_free_expired_stateid(server,
2576 &lsp->ls_stateid,
2577 cred);
2578 trace_nfs4_test_lock_stateid(state, lsp, status);
2579 if (status == -NFS4ERR_EXPIRED ||
2580 status == -NFS4ERR_BAD_STATEID) {
2581 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002582 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002583 if (!recover_lost_locks)
2584 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2585 } else if (status != NFS_OK) {
2586 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002587 nfs4_put_lock_state(prev);
2588 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002589 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002590 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002591 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002592 }
2593 spin_unlock(&state->state_lock);
2594 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002595out:
2596 return ret;
2597}
2598
2599/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002600 * nfs41_check_open_stateid - possibly free an open stateid
2601 *
2602 * @state: NFSv4 state for an inode
2603 *
2604 * Returns NFS_OK if recovery for this stateid is now finished.
2605 * Otherwise a negative NFS4ERR value is returned.
2606 */
2607static int nfs41_check_open_stateid(struct nfs4_state *state)
2608{
2609 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002610 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002611 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002612 int status;
2613
Trond Myklebustb134fc42016-09-22 13:39:16 -04002614 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002615 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2616 if (nfs4_have_delegation(state->inode, state->state))
2617 return NFS_OK;
2618 return -NFS4ERR_OPENMODE;
2619 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002620 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002621 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002622 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002623 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002624 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002625 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2626 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2627 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002628 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002629 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002630 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002631 if (status != NFS_OK)
2632 return status;
2633 if (nfs_open_stateid_recover_openmode(state))
2634 return -NFS4ERR_OPENMODE;
2635 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002636}
2637
2638static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2639{
Chuck Levereb64cf92012-07-11 16:30:05 -04002640 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002641
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002642 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002643 status = nfs41_check_expired_locks(state);
2644 if (status != NFS_OK)
2645 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002646 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002647 if (status != NFS_OK)
2648 status = nfs4_open_expired(sp, state);
2649 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002650}
2651#endif
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002654 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2655 * fields corresponding to attributes that were used to store the verifier.
2656 * Make sure we clobber those fields in the later setattr call
2657 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002658static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2659 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002660{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002661 const u32 *attrset = opendata->o_res.attrset;
2662
2663 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002664 !(sattr->ia_valid & ATTR_ATIME_SET))
2665 sattr->ia_valid |= ATTR_ATIME;
2666
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002667 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002668 !(sattr->ia_valid & ATTR_MTIME_SET))
2669 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002670
2671 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002672 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2673 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002674 sattr->ia_valid &= ~ATTR_MODE;
2675
2676 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2677 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002678}
2679
Trond Myklebustc21443c2013-02-07 14:26:21 -05002680static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2681 fmode_t fmode,
2682 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002683 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002684{
2685 struct nfs4_state_owner *sp = opendata->owner;
2686 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002687 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002688 struct nfs4_state *state;
2689 unsigned int seq;
2690 int ret;
2691
2692 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2693
2694 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002695 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002696 goto out;
2697
2698 state = nfs4_opendata_to_nfs4_state(opendata);
2699 ret = PTR_ERR(state);
2700 if (IS_ERR(state))
2701 goto out;
2702 if (server->caps & NFS_CAP_POSIX_LOCK)
2703 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002704 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2705 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002706
Trond Myklebust275bb302013-05-29 13:11:28 -04002707 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002708 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002709 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002710 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002711 alias = d_exact_alias(dentry, state->inode);
2712 if (!alias)
2713 alias = d_splice_alias(igrab(state->inode), dentry);
2714 /* d_splice_alias() can't fail here - it's a non-directory */
2715 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002716 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002717 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002718 }
2719 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002720 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002721 }
2722
Trond Myklebustc21443c2013-02-07 14:26:21 -05002723 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2724 if (ret != 0)
2725 goto out;
2726
Trond Myklebust3efb9722013-05-29 13:17:04 -04002727 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002728 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002729 nfs_inode_attach_open_context(ctx);
2730 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2731 nfs4_schedule_stateid_recovery(server, state);
2732 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002733out:
2734 return ret;
2735}
2736
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002737/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002738 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 */
Andy Adamson82be4172012-05-23 05:02:35 -04002740static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002741 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002742 int flags,
2743 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002744 struct nfs4_label *label,
2745 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746{
2747 struct nfs4_state_owner *sp;
2748 struct nfs4_state *state = NULL;
2749 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002750 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002751 struct dentry *dentry = ctx->dentry;
2752 struct rpc_cred *cred = ctx->cred;
2753 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2754 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002755 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002756 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002757 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
2759 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002761 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2762 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2764 goto out_err;
2765 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002766 status = nfs4_recover_expired_lease(server);
2767 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002768 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002769 if (d_really_is_positive(dentry))
2770 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002771 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002772 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002773 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002774 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002775 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002776 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002777 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
David Quigley14c43f72013-05-22 12:50:43 -04002779 if (label) {
2780 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2781 if (IS_ERR(olabel)) {
2782 status = PTR_ERR(olabel);
2783 goto err_opendata_put;
2784 }
2785 }
2786
Trond Myklebuste911b812014-03-26 13:24:37 -07002787 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2788 if (!opendata->f_attr.mdsthreshold) {
2789 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2790 if (!opendata->f_attr.mdsthreshold)
2791 goto err_free_label;
2792 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002793 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002794 }
David Howells2b0143b2015-03-17 22:25:59 +00002795 if (d_really_is_positive(dentry))
2796 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002797
Trond Myklebust3efb9722013-05-29 13:17:04 -04002798 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002799 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002800 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002801 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002802
NeilBrownefcbc042015-07-30 13:00:56 +10002803 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002804 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002805 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002806 /*
2807 * send create attributes which was not set by open
2808 * with an extra setattr.
2809 */
2810 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2811 nfs_fattr_init(opendata->o_res.f_attr);
2812 status = nfs4_do_setattr(state->inode, cred,
2813 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002814 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002815 if (status == 0) {
2816 nfs_setattr_update_inode(state->inode, sattr,
2817 opendata->o_res.f_attr);
2818 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2819 }
David Quigley1775fd32013-05-22 12:50:42 -04002820 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002821 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002822 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002823 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002824
Trond Myklebuste911b812014-03-26 13:24:37 -07002825 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002826 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002827 opendata->f_attr.mdsthreshold = NULL;
2828 }
Andy Adamson82be4172012-05-23 05:02:35 -04002829
David Quigley14c43f72013-05-22 12:50:43 -04002830 nfs4_label_free(olabel);
2831
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002832 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002835err_free_label:
2836 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002837err_opendata_put:
2838 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002839err_put_state_owner:
2840 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 return status;
2843}
2844
2845
Andy Adamson82be4172012-05-23 05:02:35 -04002846static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002847 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002848 int flags,
2849 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002850 struct nfs4_label *label,
2851 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002853 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 struct nfs4_exception exception = { };
2855 struct nfs4_state *res;
2856 int status;
2857
2858 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002859 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002860 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002861 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 if (status == 0)
2863 break;
2864 /* NOTE: BAD_SEQID means the server and client disagree about the
2865 * book-keeping w.r.t. state-changing operations
2866 * (OPEN/CLOSE/LOCK/LOCKU...)
2867 * It is actually a sign of a bug on the client or on the server.
2868 *
2869 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002870 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 * have unhashed the old state_owner for us, and that we can
2872 * therefore safely retry using a new one. We should still warn
2873 * the user though...
2874 */
2875 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002876 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002877 " returned a bad sequence-id error!\n",
2878 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 exception.retry = 1;
2880 continue;
2881 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002882 /*
2883 * BAD_STATEID on OPEN means that the server cancelled our
2884 * state before it received the OPEN_CONFIRM.
2885 * Recover by retrying the request as per the discussion
2886 * on Page 181 of RFC3530.
2887 */
2888 if (status == -NFS4ERR_BAD_STATEID) {
2889 exception.retry = 1;
2890 continue;
2891 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002892 if (status == -EAGAIN) {
2893 /* We must have found a delegation */
2894 exception.retry = 1;
2895 continue;
2896 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002897 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2898 continue;
2899 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 status, &exception));
2901 } while (exception.retry);
2902 return res;
2903}
2904
Trond Myklebust8487c472016-06-26 08:44:35 -04002905static int _nfs4_do_setattr(struct inode *inode,
2906 struct nfs_setattrargs *arg,
2907 struct nfs_setattrres *res,
2908 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002909 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002910{
2911 struct nfs_server *server = NFS_SERVER(inode);
2912 struct rpc_message msg = {
2913 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2914 .rpc_argp = arg,
2915 .rpc_resp = res,
2916 .rpc_cred = cred,
2917 };
2918 struct rpc_cred *delegation_cred = NULL;
2919 unsigned long timestamp = jiffies;
2920 fmode_t fmode;
2921 bool truncate;
2922 int status;
2923
2924 nfs_fattr_init(res->fattr);
2925
2926 /* Servers should only apply open mode checks for file size changes */
2927 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2928 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2929
2930 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2931 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002932 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002933 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002934 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002935 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002936 l_ctx = nfs_get_lock_context(ctx);
2937 if (IS_ERR(l_ctx))
2938 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002939 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2940 &arg->stateid, &delegation_cred);
2941 nfs_put_lock_context(l_ctx);
2942 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002943 return -EBADF;
2944 } else
2945 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2946 if (delegation_cred)
2947 msg.rpc_cred = delegation_cred;
2948
2949 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2950
2951 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002952 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002953 renew_lease(server, timestamp);
2954 trace_nfs4_setattr(inode, &arg->stateid, status);
2955 return status;
2956}
2957
2958static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2959 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002960 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002961 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002963 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002964 struct nfs4_state *state = ctx ? ctx->state : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002966 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 .iap = sattr,
2968 .server = server,
2969 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002970 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 };
2972 struct nfs_setattrres res = {
2973 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002974 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 .server = server,
2976 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002977 struct nfs4_exception exception = {
2978 .state = state,
2979 .inode = inode,
2980 .stateid = &arg.stateid,
2981 };
2982 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
David Quigleyaa9c2662013-05-22 12:50:44 -04002984 arg.bitmask = nfs4_bitmask(server, ilabel);
2985 if (ilabel)
2986 arg.bitmask = nfs4_bitmask(server, olabel);
2987
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 do {
NeilBrown29b59f92016-10-13 15:26:47 +11002989 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04002990 switch (err) {
2991 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002992 if (!(sattr->ia_valid & ATTR_SIZE)) {
2993 pr_warn_once("NFSv4: server %s is incorrectly "
2994 "applying open mode checks to "
2995 "a SETATTR that is not "
2996 "changing file size.\n",
2997 server->nfs_client->cl_hostname);
2998 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002999 if (state && !(state->state & FMODE_WRITE)) {
3000 err = -EBADF;
3001 if (sattr->ia_valid & ATTR_OPEN)
3002 err = -EACCES;
3003 goto out;
3004 }
3005 }
3006 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003008out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 return err;
3010}
3011
Peng Tao500d7012015-09-22 11:35:22 +08003012static bool
3013nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3014{
3015 if (inode == NULL || !nfs_have_layout(inode))
3016 return false;
3017
3018 return pnfs_wait_on_layoutreturn(inode, task);
3019}
3020
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021struct nfs4_closedata {
3022 struct inode *inode;
3023 struct nfs4_state *state;
3024 struct nfs_closeargs arg;
3025 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003026 struct {
3027 struct nfs4_layoutreturn_args arg;
3028 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003029 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003030 u32 roc_barrier;
3031 bool roc;
3032 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003033 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003034 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035};
3036
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003037static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003038{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003039 struct nfs4_closedata *calldata = data;
3040 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003041 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003042
Trond Myklebustcf805162016-11-15 14:56:07 -05003043 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003044 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3045 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003046 nfs4_put_open_state(calldata->state);
3047 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003048 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003049 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003050 kfree(calldata);
3051}
3052
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003053static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003055 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003058 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
Chuck Levera3ca5652012-03-01 17:00:40 -05003060 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003061 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3062 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003063 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003064
3065 /* Handle Layoutreturn errors */
3066 if (calldata->arg.lr_args && task->tk_status != 0) {
3067 switch (calldata->res.lr_ret) {
3068 default:
3069 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3070 break;
3071 case 0:
3072 calldata->arg.lr_args = NULL;
3073 calldata->res.lr_res = NULL;
3074 break;
3075 case -NFS4ERR_ADMIN_REVOKED:
3076 case -NFS4ERR_DELEG_REVOKED:
3077 case -NFS4ERR_EXPIRED:
3078 case -NFS4ERR_BAD_STATEID:
3079 case -NFS4ERR_OLD_STATEID:
3080 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3081 case -NFS4ERR_WRONG_CRED:
3082 calldata->arg.lr_args = NULL;
3083 calldata->res.lr_res = NULL;
3084 calldata->res.lr_ret = 0;
3085 rpc_restart_call_prepare(task);
3086 return;
3087 }
3088 }
3089
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 /* hmm. we are done with the inode, and in the process of freeing
3091 * the state_owner. we keep this around to process errors
3092 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 switch (task->tk_status) {
3094 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003095 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003096 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003097 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003098 case -NFS4ERR_ACCESS:
3099 if (calldata->arg.bitmask != NULL) {
3100 calldata->arg.bitmask = NULL;
3101 calldata->res.fattr = NULL;
3102 task->tk_status = 0;
3103 rpc_restart_call_prepare(task);
3104 goto out_release;
3105
3106 }
3107 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003108 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003110 case -NFS4ERR_EXPIRED:
3111 nfs4_free_revoked_stateid(server,
3112 &calldata->arg.stateid,
3113 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003114 case -NFS4ERR_OLD_STATEID:
3115 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003116 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003117 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003118 rpc_restart_call_prepare(task);
3119 goto out_release;
3120 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003121 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003122 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003124 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003125 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003126 goto out_release;
3127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003129 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3130 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003131out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003132 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003133 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003134 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135}
3136
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003137static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003139 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003140 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003141 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003142 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003143 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003144
Chuck Levera3ca5652012-03-01 17:00:40 -05003145 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003146 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003147 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003148
Trond Myklebust88069f72009-12-08 08:33:16 -05003149 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003150 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003151 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3152 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3153 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003154 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003155 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003156 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003157 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003158 if (state->n_rdonly == 0)
3159 call_close |= is_rdonly;
3160 else if (is_rdonly)
3161 calldata->arg.fmode |= FMODE_READ;
3162 if (state->n_wronly == 0)
3163 call_close |= is_wronly;
3164 else if (is_wronly)
3165 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003166 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3167 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003168 } else if (is_rdwr)
3169 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3170
Trond Myklebust5cc78612016-11-14 11:19:56 -05003171 if (!nfs4_valid_open_stateid(state) ||
3172 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003173 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003174 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003175
3176 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003177 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003178 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003179 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003180
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003181 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003182 nfs_release_seqid(calldata->arg.seqid);
3183 goto out_wait;
3184 }
3185
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003186 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003187 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003188
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003189 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003190 /* Close-to-open cache consistency revalidation */
3191 if (!nfs4_have_delegation(inode, FMODE_READ))
3192 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3193 else
3194 calldata->arg.bitmask = NULL;
3195 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003196
Trond Myklebust6ae37332015-01-30 14:21:14 -05003197 calldata->arg.share_access =
3198 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3199 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003200
Trond Myklebustd8d84982016-12-19 12:14:44 -05003201 if (calldata->res.fattr == NULL)
3202 calldata->arg.bitmask = NULL;
3203 else if (calldata->arg.bitmask == NULL)
3204 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003205 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003206 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003207 &calldata->arg.seq_args,
3208 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003209 task) != 0)
3210 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003211 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003212 return;
3213out_no_action:
3214 task->tk_action = NULL;
3215out_wait:
3216 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217}
3218
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003219static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003220 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003221 .rpc_call_done = nfs4_close_done,
3222 .rpc_release = nfs4_free_closedata,
3223};
3224
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225/*
3226 * It is possible for data to be read/written from a mem-mapped file
3227 * after the sys_close call (which hits the vfs layer as a flush).
3228 * This means that we can't safely call nfsv4 close on a file until
3229 * the inode is cleared. This in turn means that we are not good
3230 * NFSv4 citizens - we do not indicate to the server to update the file's
3231 * share state even when we are done with one of the three share
3232 * stateid's in the inode.
3233 *
3234 * NOTE: Caller must be holding the sp->so_owner semaphore!
3235 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003236int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003238 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003239 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003241 struct nfs4_state_owner *sp = state->owner;
3242 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003243 struct rpc_message msg = {
3244 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3245 .rpc_cred = state->owner->so_cred,
3246 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003247 struct rpc_task_setup task_setup_data = {
3248 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003249 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003250 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003251 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003252 .flags = RPC_TASK_ASYNC,
3253 };
Trond Myklebust95121352005-10-18 14:20:12 -07003254 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003256 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3257 &task_setup_data.rpc_client, &msg);
3258
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003259 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003261 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003262 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003263 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003265 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003267 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3268 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003269 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003270 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003271 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003272 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003273 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003274 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003275 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003276 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003277 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003278 calldata->lr.roc = pnfs_roc(state->inode,
3279 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3280 if (calldata->lr.roc) {
3281 calldata->arg.lr_args = &calldata->lr.arg;
3282 calldata->res.lr_res = &calldata->lr.res;
3283 }
Al Viro643168c2011-06-22 18:20:23 -04003284 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003285
Trond Myklebust1174dd12010-12-21 10:52:24 -05003286 msg.rpc_argp = &calldata->arg;
3287 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003288 task_setup_data.callback_data = calldata;
3289 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003290 if (IS_ERR(task))
3291 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003292 status = 0;
3293 if (wait)
3294 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003295 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003296 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003297out_free_calldata:
3298 kfree(calldata);
3299out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003300 nfs4_put_open_state(state);
3301 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003302 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303}
3304
Trond Myklebust2b484292010-09-17 10:56:51 -04003305static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003306nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3307 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003310 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3311
3312 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313
Trond Myklebust565277f2007-10-15 18:17:53 -04003314 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003315 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003316
3317 nfs4_label_release_security(label);
3318
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003319 if (IS_ERR(state))
3320 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003321 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322}
3323
Trond Myklebust1185a552009-12-03 15:54:02 -05003324static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003325{
3326 if (ctx->state == NULL)
3327 return;
3328 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003329 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003330 else
Al Viro643168c2011-06-22 18:20:23 -04003331 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003332}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Trond Myklebustb944dba2013-11-04 15:20:20 -05003334#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3335#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003336#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003337
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3339{
Kinglong Mee8c612822015-08-26 21:12:58 +08003340 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003341 struct nfs4_server_caps_arg args = {
3342 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003343 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003344 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 struct nfs4_server_caps_res res = {};
3346 struct rpc_message msg = {
3347 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003348 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 .rpc_resp = &res,
3350 };
3351 int status;
3352
Kinglong Mee8c612822015-08-26 21:12:58 +08003353 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3354 FATTR4_WORD0_FH_EXPIRE_TYPE |
3355 FATTR4_WORD0_LINK_SUPPORT |
3356 FATTR4_WORD0_SYMLINK_SUPPORT |
3357 FATTR4_WORD0_ACLSUPPORT;
3358 if (minorversion)
3359 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3360
Bryan Schumaker7c513052011-03-24 17:12:24 +00003361 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003363 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003364 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003365 case 0:
3366 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3367 res.attr_bitmask[2] = 0;
3368 break;
3369 case 1:
3370 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3371 break;
3372 case 2:
3373 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003376 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3377 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3378 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3379 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003380 NFS_CAP_CTIME|NFS_CAP_MTIME|
3381 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003382 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3383 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 server->caps |= NFS_CAP_ACLS;
3385 if (res.has_links != 0)
3386 server->caps |= NFS_CAP_HARDLINKS;
3387 if (res.has_symlinks != 0)
3388 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003389 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3390 server->caps |= NFS_CAP_FILEID;
3391 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3392 server->caps |= NFS_CAP_MODE;
3393 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3394 server->caps |= NFS_CAP_NLINK;
3395 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3396 server->caps |= NFS_CAP_OWNER;
3397 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3398 server->caps |= NFS_CAP_OWNER_GROUP;
3399 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3400 server->caps |= NFS_CAP_ATIME;
3401 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3402 server->caps |= NFS_CAP_CTIME;
3403 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3404 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003405#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3406 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3407 server->caps |= NFS_CAP_SECURITY_LABEL;
3408#endif
3409 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3410 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003411 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003412
Trond Myklebusta65318b2009-03-11 14:10:28 -04003413 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3414 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3415 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003416 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003417 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3418 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003420 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003422
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 return status;
3424}
3425
Trond Myklebust55a97592006-06-09 09:34:19 -04003426int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427{
3428 struct nfs4_exception exception = { };
3429 int err;
3430 do {
3431 err = nfs4_handle_exception(server,
3432 _nfs4_server_capabilities(server, fhandle),
3433 &exception);
3434 } while (exception.retry);
3435 return err;
3436}
3437
3438static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3439 struct nfs_fsinfo *info)
3440{
David Quigleyaa9c2662013-05-22 12:50:44 -04003441 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003443 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 };
3445 struct nfs4_lookup_res res = {
3446 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003447 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 .fh = fhandle,
3449 };
3450 struct rpc_message msg = {
3451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3452 .rpc_argp = &args,
3453 .rpc_resp = &res,
3454 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003455
David Quigleyaa9c2662013-05-22 12:50:44 -04003456 bitmask[0] = nfs4_fattr_bitmap[0];
3457 bitmask[1] = nfs4_fattr_bitmap[1];
3458 /*
3459 * Process the label in the upcoming getfattr
3460 */
3461 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3462
Trond Myklebust0e574af2005-10-27 22:12:38 -04003463 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003464 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465}
3466
3467static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3468 struct nfs_fsinfo *info)
3469{
3470 struct nfs4_exception exception = { };
3471 int err;
3472 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003473 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003474 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003475 switch (err) {
3476 case 0:
3477 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003478 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003479 default:
3480 err = nfs4_handle_exception(server, err, &exception);
3481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003483out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 return err;
3485}
3486
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003487static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3488 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3489{
Trond Myklebustc2190662013-08-26 19:23:04 -04003490 struct rpc_auth_create_args auth_args = {
3491 .pseudoflavor = flavor,
3492 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003493 struct rpc_auth *auth;
3494 int ret;
3495
Trond Myklebustc2190662013-08-26 19:23:04 -04003496 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003497 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003498 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003499 goto out;
3500 }
3501 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003502out:
3503 return ret;
3504}
3505
Chuck Lever9a744ba2013-03-16 15:56:02 -04003506/*
3507 * Retry pseudoroot lookup with various security flavors. We do this when:
3508 *
3509 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3510 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3511 *
3512 * Returns zero on success, or a negative NFS4ERR value, or a
3513 * negative errno value.
3514 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003515static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003516 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003518 /* Per 3530bis 15.33.5 */
3519 static const rpc_authflavor_t flav_array[] = {
3520 RPC_AUTH_GSS_KRB5P,
3521 RPC_AUTH_GSS_KRB5I,
3522 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003523 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003524 RPC_AUTH_NULL,
3525 };
3526 int status = -EPERM;
3527 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003529 if (server->auth_info.flavor_len > 0) {
3530 /* try each flavor specified by user */
3531 for (i = 0; i < server->auth_info.flavor_len; i++) {
3532 status = nfs4_lookup_root_sec(server, fhandle, info,
3533 server->auth_info.flavors[i]);
3534 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3535 continue;
3536 break;
3537 }
3538 } else {
3539 /* no flavors specified by user, try default list */
3540 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3541 status = nfs4_lookup_root_sec(server, fhandle, info,
3542 flav_array[i]);
3543 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3544 continue;
3545 break;
3546 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003547 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003548
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003549 /*
3550 * -EACCESS could mean that the user doesn't have correct permissions
3551 * to access the mount. It could also mean that we tried to mount
3552 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3553 * existing mount programs don't handle -EACCES very well so it should
3554 * be mapped to -EPERM instead.
3555 */
3556 if (status == -EACCES)
3557 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003558 return status;
3559}
3560
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003561/**
3562 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3563 * @server: initialized nfs_server handle
3564 * @fhandle: we fill in the pseudo-fs root file handle
3565 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003566 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003567 *
3568 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003569 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003570int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003571 struct nfs_fsinfo *info,
3572 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003573{
Andre Przywarac7757072015-04-23 17:17:40 +01003574 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003575
Andre Przywarac7757072015-04-23 17:17:40 +01003576 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003577 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003578
3579 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003580 status = server->nfs_client->cl_mvops->find_root_sec(server,
3581 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003582
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 if (status == 0)
3584 status = nfs4_server_capabilities(server, fhandle);
3585 if (status == 0)
3586 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003587
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003588 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589}
3590
Bryan Schumakerbae36242012-05-10 15:07:31 -04003591static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3592 struct nfs_fsinfo *info)
3593{
3594 int error;
3595 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003596 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003597
3598 error = nfs4_server_capabilities(server, mntfh);
3599 if (error < 0) {
3600 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3601 return error;
3602 }
3603
David Quigley14c43f72013-05-22 12:50:43 -04003604 label = nfs4_label_alloc(server, GFP_KERNEL);
3605 if (IS_ERR(label))
3606 return PTR_ERR(label);
3607
David Quigley1775fd32013-05-22 12:50:42 -04003608 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003609 if (error < 0) {
3610 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003611 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003612 }
3613
3614 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3615 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3616 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3617
David Quigley14c43f72013-05-22 12:50:43 -04003618err_free_label:
3619 nfs4_label_free(label);
3620
Bryan Schumakerbae36242012-05-10 15:07:31 -04003621 return error;
3622}
3623
Manoj Naik6b97fd32006-06-09 09:34:29 -04003624/*
3625 * Get locations and (maybe) other attributes of a referral.
3626 * Note that we'll actually follow the referral later when
3627 * we detect fsid mismatch in inode revalidation
3628 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003629static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3630 const struct qstr *name, struct nfs_fattr *fattr,
3631 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003632{
3633 int status = -ENOMEM;
3634 struct page *page = NULL;
3635 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003636
3637 page = alloc_page(GFP_KERNEL);
3638 if (page == NULL)
3639 goto out;
3640 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3641 if (locations == NULL)
3642 goto out;
3643
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003644 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003645 if (status != 0)
3646 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003647
3648 /*
3649 * If the fsid didn't change, this is a migration event, not a
3650 * referral. Cause us to drop into the exception handler, which
3651 * will kick off migration recovery.
3652 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003653 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003654 dprintk("%s: server did not return a different fsid for"
3655 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003656 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003657 goto out;
3658 }
Andy Adamson533eb462011-06-13 18:25:56 -04003659 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3660 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003661
Andy Adamson533eb462011-06-13 18:25:56 -04003662 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003663 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003664 memset(fhandle, 0, sizeof(struct nfs_fh));
3665out:
3666 if (page)
3667 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003668 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003669 return status;
3670}
3671
David Quigley1775fd32013-05-22 12:50:42 -04003672static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3673 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674{
3675 struct nfs4_getattr_arg args = {
3676 .fh = fhandle,
3677 .bitmask = server->attr_bitmask,
3678 };
3679 struct nfs4_getattr_res res = {
3680 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003681 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 .server = server,
3683 };
3684 struct rpc_message msg = {
3685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3686 .rpc_argp = &args,
3687 .rpc_resp = &res,
3688 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003689
3690 args.bitmask = nfs4_bitmask(server, label);
3691
Trond Myklebust0e574af2005-10-27 22:12:38 -04003692 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003693 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694}
3695
David Quigley1775fd32013-05-22 12:50:42 -04003696static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3697 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698{
3699 struct nfs4_exception exception = { };
3700 int err;
3701 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003702 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3703 trace_nfs4_getattr(server, fhandle, fattr, err);
3704 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 &exception);
3706 } while (exception.retry);
3707 return err;
3708}
3709
3710/*
3711 * The file is not closed if it is opened due to the a request to change
3712 * the size of the file. The open call will not be needed once the
3713 * VFS layer lookup-intents are implemented.
3714 *
3715 * Close is called when the inode is destroyed.
3716 * If we haven't opened the file for O_WRONLY, we
3717 * need to in the size_change case to obtain a stateid.
3718 *
3719 * Got race?
3720 * Because OPEN is always done by name in nfsv4, it is
3721 * possible that we opened a different file by the same
3722 * name. We can recognize this race condition, but we
3723 * can't do anything about it besides returning an error.
3724 *
3725 * This will be fixed with VFS changes (lookup-intent).
3726 */
3727static int
3728nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3729 struct iattr *sattr)
3730{
David Howells2b0143b2015-03-17 22:25:59 +00003731 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003732 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003733 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003734 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 int status;
3736
Peng Tao88ac8152014-09-12 11:04:10 +08003737 if (pnfs_ld_layoutret_on_setattr(inode) &&
3738 sattr->ia_valid & ATTR_SIZE &&
3739 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003740 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003741
Trond Myklebust0e574af2005-10-27 22:12:38 -04003742 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
Andy Adamson26699402012-05-30 16:12:24 -04003744 /* Deal with open(O_TRUNC) */
3745 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003746 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003747
3748 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003749 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003750 return 0;
3751
Trond Myklebustd5308382005-11-04 15:33:38 -05003752 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003753 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003754
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003755 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003756 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003757 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003758 }
3759
David Quigley14c43f72013-05-22 12:50:43 -04003760 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3761 if (IS_ERR(label))
3762 return PTR_ERR(label);
3763
NeilBrown29b59f92016-10-13 15:26:47 +11003764 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003765 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003766 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003767 nfs_setsecurity(inode, fattr, label);
3768 }
David Quigley14c43f72013-05-22 12:50:43 -04003769 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 return status;
3771}
3772
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003773static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3774 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003775 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003776{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003777 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003778 int status;
3779 struct nfs4_lookup_arg args = {
3780 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003781 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003782 .name = name,
3783 };
3784 struct nfs4_lookup_res res = {
3785 .server = server,
3786 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003787 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003788 .fh = fhandle,
3789 };
3790 struct rpc_message msg = {
3791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3792 .rpc_argp = &args,
3793 .rpc_resp = &res,
3794 };
3795
David Quigleyaa9c2662013-05-22 12:50:44 -04003796 args.bitmask = nfs4_bitmask(server, label);
3797
David Howells2b3de442006-08-22 20:06:09 -04003798 nfs_fattr_init(fattr);
3799
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003801 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 dprintk("NFS reply lookup: %d\n", status);
3803 return status;
3804}
3805
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003806static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003807{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003808 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003809 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003810 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3811 fattr->nlink = 2;
3812}
3813
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003814static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003815 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003816 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003817{
3818 struct nfs4_exception exception = { };
3819 struct rpc_clnt *client = *clnt;
3820 int err;
3821 do {
David Quigley1775fd32013-05-22 12:50:42 -04003822 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003823 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003824 switch (err) {
3825 case -NFS4ERR_BADNAME:
3826 err = -ENOENT;
3827 goto out;
3828 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003829 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003830 if (err == -NFS4ERR_MOVED)
3831 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003832 goto out;
3833 case -NFS4ERR_WRONGSEC:
3834 err = -EPERM;
3835 if (client != *clnt)
3836 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003837 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003838 if (IS_ERR(client))
3839 return PTR_ERR(client);
3840
3841 exception.retry = 1;
3842 break;
3843 default:
3844 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3845 }
3846 } while (exception.retry);
3847
3848out:
3849 if (err == 0)
3850 *clnt = client;
3851 else if (client != *clnt)
3852 rpc_shutdown_client(client);
3853
3854 return err;
3855}
3856
Al Virobeffb8f2016-07-20 16:34:42 -04003857static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003858 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3859 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003861 int status;
3862 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003863
David Quigley1775fd32013-05-22 12:50:42 -04003864 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003865 if (client != NFS_CLIENT(dir)) {
3866 rpc_shutdown_client(client);
3867 nfs_fixup_secinfo_attributes(fattr);
3868 }
3869 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870}
3871
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003872struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003873nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003874 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3875{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003876 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003877 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003878
David Quigley1775fd32013-05-22 12:50:42 -04003879 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003880 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003881 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003882 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003883}
3884
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3886{
Trond Myklebust76b32992007-08-10 17:45:11 -04003887 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 struct nfs4_accessargs args = {
3889 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003890 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003892 struct nfs4_accessres res = {
3893 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003894 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 struct rpc_message msg = {
3896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3897 .rpc_argp = &args,
3898 .rpc_resp = &res,
3899 .rpc_cred = entry->cred,
3900 };
3901 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003902 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903
3904 /*
3905 * Determine which access bits we want to ask for...
3906 */
3907 if (mode & MAY_READ)
3908 args.access |= NFS4_ACCESS_READ;
3909 if (S_ISDIR(inode->i_mode)) {
3910 if (mode & MAY_WRITE)
3911 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3912 if (mode & MAY_EXEC)
3913 args.access |= NFS4_ACCESS_LOOKUP;
3914 } else {
3915 if (mode & MAY_WRITE)
3916 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3917 if (mode & MAY_EXEC)
3918 args.access |= NFS4_ACCESS_EXECUTE;
3919 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003920
3921 res.fattr = nfs_alloc_fattr();
3922 if (res.fattr == NULL)
3923 return -ENOMEM;
3924
Bryan Schumaker7c513052011-03-24 17:12:24 +00003925 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003927 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003928 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003930 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 return status;
3932}
3933
3934static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3935{
3936 struct nfs4_exception exception = { };
3937 int err;
3938 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003939 err = _nfs4_proc_access(inode, entry);
3940 trace_nfs4_access(inode, err);
3941 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 &exception);
3943 } while (exception.retry);
3944 return err;
3945}
3946
3947/*
3948 * TODO: For the time being, we don't try to get any attributes
3949 * along with any of the zero-copy operations READ, READDIR,
3950 * READLINK, WRITE.
3951 *
3952 * In the case of the first three, we want to put the GETATTR
3953 * after the read-type operation -- this is because it is hard
3954 * to predict the length of a GETATTR response in v4, and thus
3955 * align the READ data correctly. This means that the GETATTR
3956 * may end up partially falling into the page cache, and we should
3957 * shift it into the 'tail' of the xdr_buf before processing.
3958 * To do this efficiently, we need to know the total length
3959 * of data received, which doesn't seem to be available outside
3960 * of the RPC layer.
3961 *
3962 * In the case of WRITE, we also want to put the GETATTR after
3963 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003964 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 *
3966 * Both of these changes to the XDR layer would in fact be quite
3967 * minor, but I decided to leave them for a subsequent patch.
3968 */
3969static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3970 unsigned int pgbase, unsigned int pglen)
3971{
3972 struct nfs4_readlink args = {
3973 .fh = NFS_FH(inode),
3974 .pgbase = pgbase,
3975 .pglen = pglen,
3976 .pages = &page,
3977 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003978 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 struct rpc_message msg = {
3980 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3981 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003982 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 };
3984
Bryan Schumaker7c513052011-03-24 17:12:24 +00003985 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 -07003986}
3987
3988static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3989 unsigned int pgbase, unsigned int pglen)
3990{
3991 struct nfs4_exception exception = { };
3992 int err;
3993 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003994 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3995 trace_nfs4_readlink(inode, err);
3996 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 &exception);
3998 } while (exception.retry);
3999 return err;
4000}
4001
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004003 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005static int
4006nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004007 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004009 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004010 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004011 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 int status = 0;
4014
NeilBrown532d4de2016-10-13 15:26:47 +11004015 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004016 if (IS_ERR(ctx))
4017 return PTR_ERR(ctx);
4018
David Quigleyaa9c2662013-05-22 12:50:44 -04004019 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4020
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004021 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4022 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004023 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 if (IS_ERR(state)) {
4025 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004026 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004029 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004030 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 return status;
4032}
4033
Al Virobeffb8f2016-07-20 16:34:42 -04004034static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035{
Trond Myklebust16e42952005-10-27 22:12:44 -04004036 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004037 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004039 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004041 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004042 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004043 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004045 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4046 .rpc_argp = &args,
4047 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004049 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004050 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004051
Bryan Schumaker7c513052011-03-24 17:12:24 +00004052 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004053 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004054 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 return status;
4056}
4057
Al Virobeffb8f2016-07-20 16:34:42 -04004058static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059{
4060 struct nfs4_exception exception = { };
4061 int err;
4062 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004063 err = _nfs4_proc_remove(dir, name);
4064 trace_nfs4_remove(dir, name, err);
4065 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 &exception);
4067 } while (exception.retry);
4068 return err;
4069}
4070
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004071static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004073 struct nfs_server *server = NFS_SERVER(dir);
4074 struct nfs_removeargs *args = msg->rpc_argp;
4075 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004077 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004079 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004080
4081 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082}
4083
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004084static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4085{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004086 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004087 &data->args.seq_args,
4088 &data->res.seq_res,
4089 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090}
4091
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004092static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004094 struct nfs_unlinkdata *data = task->tk_calldata;
4095 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004096
Trond Myklebust14516c32010-07-31 14:29:06 -04004097 if (!nfs4_sequence_done(task, &res->seq_res))
4098 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004099 if (nfs4_async_handle_error(task, res->server, NULL,
4100 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004101 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004102 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004103 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004104 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105}
4106
Jeff Laytond3d41522010-09-17 17:31:57 -04004107static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4108{
4109 struct nfs_server *server = NFS_SERVER(dir);
4110 struct nfs_renameargs *arg = msg->rpc_argp;
4111 struct nfs_renameres *res = msg->rpc_resp;
4112
4113 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004114 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004115 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004116}
4117
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004118static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4119{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004120 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004121 &data->args.seq_args,
4122 &data->res.seq_res,
4123 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004124}
4125
4126static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4127 struct inode *new_dir)
4128{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004129 struct nfs_renamedata *data = task->tk_calldata;
4130 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004131
4132 if (!nfs4_sequence_done(task, &res->seq_res))
4133 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004134 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004135 return 0;
4136
Trond Myklebustc733c492017-01-11 12:32:26 -05004137 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004138 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004139 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004140 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004141 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004142 return 1;
4143}
4144
Al Virobeffb8f2016-07-20 16:34:42 -04004145static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004147 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 struct nfs4_link_arg arg = {
4149 .fh = NFS_FH(inode),
4150 .dir_fh = NFS_FH(dir),
4151 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004152 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004154 struct nfs4_link_res res = {
4155 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004156 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004157 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 struct rpc_message msg = {
4159 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4160 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004161 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004163 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
Trond Myklebust136f2622010-04-16 16:22:49 -04004165 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004166 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004167 goto out;
4168
David Quigley14c43f72013-05-22 12:50:43 -04004169 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4170 if (IS_ERR(res.label)) {
4171 status = PTR_ERR(res.label);
4172 goto out;
4173 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004174 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004175
Bryan Schumaker7c513052011-03-24 17:12:24 +00004176 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004177 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004178 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004179 status = nfs_post_op_update_inode(inode, res.fattr);
4180 if (!status)
4181 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004182 }
David Quigley14c43f72013-05-22 12:50:43 -04004183
4184
4185 nfs4_label_free(res.label);
4186
Trond Myklebust136f2622010-04-16 16:22:49 -04004187out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004188 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 return status;
4190}
4191
Al Virobeffb8f2016-07-20 16:34:42 -04004192static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193{
4194 struct nfs4_exception exception = { };
4195 int err;
4196 do {
4197 err = nfs4_handle_exception(NFS_SERVER(inode),
4198 _nfs4_proc_link(inode, dir, name),
4199 &exception);
4200 } while (exception.retry);
4201 return err;
4202}
4203
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004204struct nfs4_createdata {
4205 struct rpc_message msg;
4206 struct nfs4_create_arg arg;
4207 struct nfs4_create_res res;
4208 struct nfs_fh fh;
4209 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004210 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004211};
4212
4213static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004214 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004215{
4216 struct nfs4_createdata *data;
4217
4218 data = kzalloc(sizeof(*data), GFP_KERNEL);
4219 if (data != NULL) {
4220 struct nfs_server *server = NFS_SERVER(dir);
4221
David Quigley14c43f72013-05-22 12:50:43 -04004222 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4223 if (IS_ERR(data->label))
4224 goto out_free;
4225
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004226 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4227 data->msg.rpc_argp = &data->arg;
4228 data->msg.rpc_resp = &data->res;
4229 data->arg.dir_fh = NFS_FH(dir);
4230 data->arg.server = server;
4231 data->arg.name = name;
4232 data->arg.attrs = sattr;
4233 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004234 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004235 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004236 data->res.server = server;
4237 data->res.fh = &data->fh;
4238 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004239 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004240 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004241 }
4242 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004243out_free:
4244 kfree(data);
4245 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004246}
4247
4248static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4249{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004250 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004251 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004252 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004253 update_changeattr(dir, &data->res.dir_cinfo,
4254 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004255 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004256 }
4257 return status;
4258}
4259
4260static void nfs4_free_createdata(struct nfs4_createdata *data)
4261{
David Quigley14c43f72013-05-22 12:50:43 -04004262 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004263 kfree(data);
4264}
4265
Chuck Lever4f390c12006-08-22 20:06:22 -04004266static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004267 struct page *page, unsigned int len, struct iattr *sattr,
4268 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004270 struct nfs4_createdata *data;
4271 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
Chuck Lever94a6d752006-08-22 20:06:23 -04004273 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004274 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004275
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004276 status = -ENOMEM;
4277 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4278 if (data == NULL)
4279 goto out;
4280
4281 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4282 data->arg.u.symlink.pages = &page;
4283 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004284 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004286 status = nfs4_do_create(dir, dentry, data);
4287
4288 nfs4_free_createdata(data);
4289out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 return status;
4291}
4292
Chuck Lever4f390c12006-08-22 20:06:22 -04004293static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004294 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295{
4296 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004297 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004299
4300 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4301
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004303 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4304 trace_nfs4_symlink(dir, &dentry->d_name, err);
4305 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 &exception);
4307 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004308
4309 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 return err;
4311}
4312
4313static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004314 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004316 struct nfs4_createdata *data;
4317 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004319 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4320 if (data == NULL)
4321 goto out;
4322
David Quigley1775fd32013-05-22 12:50:42 -04004323 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004324 status = nfs4_do_create(dir, dentry, data);
4325
4326 nfs4_free_createdata(data);
4327out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 return status;
4329}
4330
4331static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4332 struct iattr *sattr)
4333{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004334 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004336 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004338
David Quigleyaa9c2662013-05-22 12:50:44 -04004339 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4340
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004341 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4342 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004344 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4345 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4346 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 &exception);
4348 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004349 nfs4_label_release_security(label);
4350
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 return err;
4352}
4353
4354static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004355 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356{
David Howells2b0143b2015-03-17 22:25:59 +00004357 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 struct nfs4_readdir_arg args = {
4359 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004360 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 .pgbase = 0,
4362 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004363 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004364 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 };
4366 struct nfs4_readdir_res res;
4367 struct rpc_message msg = {
4368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4369 .rpc_argp = &args,
4370 .rpc_resp = &res,
4371 .rpc_cred = cred,
4372 };
4373 int status;
4374
Al Viro6de14722013-09-16 10:53:17 -04004375 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4376 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004377 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004378 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004380 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 -05004381 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004382 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004383 status += args.pgbase;
4384 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004385
4386 nfs_invalidate_atime(dir);
4387
Harvey Harrison3110ff82008-05-02 13:42:44 -07004388 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 return status;
4390}
4391
4392static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004393 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394{
4395 struct nfs4_exception exception = { };
4396 int err;
4397 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004398 err = _nfs4_proc_readdir(dentry, cred, cookie,
4399 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004400 trace_nfs4_readdir(d_inode(dentry), err);
4401 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 &exception);
4403 } while (exception.retry);
4404 return err;
4405}
4406
4407static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004408 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004410 struct nfs4_createdata *data;
4411 int mode = sattr->ia_mode;
4412 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004414 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4415 if (data == NULL)
4416 goto out;
4417
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004419 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004421 data->arg.ftype = NF4BLK;
4422 data->arg.u.device.specdata1 = MAJOR(rdev);
4423 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 }
4425 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004426 data->arg.ftype = NF4CHR;
4427 data->arg.u.device.specdata1 = MAJOR(rdev);
4428 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004429 } else if (!S_ISSOCK(mode)) {
4430 status = -EINVAL;
4431 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 }
David Quigley1775fd32013-05-22 12:50:42 -04004433
David Quigleyaa9c2662013-05-22 12:50:44 -04004434 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004435 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004436out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004437 nfs4_free_createdata(data);
4438out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 return status;
4440}
4441
4442static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4443 struct iattr *sattr, dev_t rdev)
4444{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004445 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004447 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004449
David Quigleyaa9c2662013-05-22 12:50:44 -04004450 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4451
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004452 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4453 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004455 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4456 trace_nfs4_mknod(dir, &dentry->d_name, err);
4457 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 &exception);
4459 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004460
4461 nfs4_label_release_security(label);
4462
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 return err;
4464}
4465
4466static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4467 struct nfs_fsstat *fsstat)
4468{
4469 struct nfs4_statfs_arg args = {
4470 .fh = fhandle,
4471 .bitmask = server->attr_bitmask,
4472 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004473 struct nfs4_statfs_res res = {
4474 .fsstat = fsstat,
4475 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 struct rpc_message msg = {
4477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4478 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004479 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 };
4481
Trond Myklebust0e574af2005-10-27 22:12:38 -04004482 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004483 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484}
4485
4486static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4487{
4488 struct nfs4_exception exception = { };
4489 int err;
4490 do {
4491 err = nfs4_handle_exception(server,
4492 _nfs4_proc_statfs(server, fhandle, fsstat),
4493 &exception);
4494 } while (exception.retry);
4495 return err;
4496}
4497
4498static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4499 struct nfs_fsinfo *fsinfo)
4500{
4501 struct nfs4_fsinfo_arg args = {
4502 .fh = fhandle,
4503 .bitmask = server->attr_bitmask,
4504 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004505 struct nfs4_fsinfo_res res = {
4506 .fsinfo = fsinfo,
4507 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 struct rpc_message msg = {
4509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4510 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004511 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 };
4513
Bryan Schumaker7c513052011-03-24 17:12:24 +00004514 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515}
4516
4517static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4518{
4519 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004520 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 int err;
4522
4523 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004524 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004525 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004526 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004527 nfs4_set_lease_period(server->nfs_client,
4528 fsinfo->lease_time * HZ,
4529 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004530 break;
4531 }
4532 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 } while (exception.retry);
4534 return err;
4535}
4536
4537static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4538{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004539 int error;
4540
Trond Myklebust0e574af2005-10-27 22:12:38 -04004541 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004542 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004543 if (error == 0) {
4544 /* block layout checks this! */
4545 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004546 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004547 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004548
4549 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550}
4551
4552static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4553 struct nfs_pathconf *pathconf)
4554{
4555 struct nfs4_pathconf_arg args = {
4556 .fh = fhandle,
4557 .bitmask = server->attr_bitmask,
4558 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004559 struct nfs4_pathconf_res res = {
4560 .pathconf = pathconf,
4561 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 struct rpc_message msg = {
4563 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4564 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004565 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 };
4567
4568 /* None of the pathconf attributes are mandatory to implement */
4569 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4570 memset(pathconf, 0, sizeof(*pathconf));
4571 return 0;
4572 }
4573
Trond Myklebust0e574af2005-10-27 22:12:38 -04004574 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004575 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576}
4577
4578static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4579 struct nfs_pathconf *pathconf)
4580{
4581 struct nfs4_exception exception = { };
4582 int err;
4583
4584 do {
4585 err = nfs4_handle_exception(server,
4586 _nfs4_proc_pathconf(server, fhandle, pathconf),
4587 &exception);
4588 } while (exception.retry);
4589 return err;
4590}
4591
Trond Myklebust5521abf2013-03-16 20:54:34 -04004592int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004593 const struct nfs_open_context *ctx,
4594 const struct nfs_lock_context *l_ctx,
4595 fmode_t fmode)
4596{
NeilBrown17393472016-10-13 15:26:47 +11004597 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004598}
4599EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4600
Trond Myklebust5521abf2013-03-16 20:54:34 -04004601static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4602 const struct nfs_open_context *ctx,
4603 const struct nfs_lock_context *l_ctx,
4604 fmode_t fmode)
4605{
4606 nfs4_stateid current_stateid;
4607
Trond Myklebuste1253be2014-03-05 08:44:23 -05004608 /* If the current stateid represents a lost lock, then exit */
4609 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4610 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004611 return nfs4_stateid_match(stateid, &current_stateid);
4612}
4613
4614static bool nfs4_error_stateid_expired(int err)
4615{
4616 switch (err) {
4617 case -NFS4ERR_DELEG_REVOKED:
4618 case -NFS4ERR_ADMIN_REVOKED:
4619 case -NFS4ERR_BAD_STATEID:
4620 case -NFS4ERR_STALE_STATEID:
4621 case -NFS4ERR_OLD_STATEID:
4622 case -NFS4ERR_OPENMODE:
4623 case -NFS4ERR_EXPIRED:
4624 return true;
4625 }
4626 return false;
4627}
4628
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004629static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004631 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004633 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004634 if (task->tk_status < 0) {
4635 struct nfs4_exception exception = {
4636 .inode = hdr->inode,
4637 .state = hdr->args.context->state,
4638 .stateid = &hdr->args.stateid,
4639 };
4640 task->tk_status = nfs4_async_handle_exception(task,
4641 server, task->tk_status, &exception);
4642 if (exception.retry) {
4643 rpc_restart_call_prepare(task);
4644 return -EAGAIN;
4645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004647
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004649 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004650 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651}
4652
Trond Myklebust5521abf2013-03-16 20:54:34 -04004653static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004654 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004655{
4656
4657 if (!nfs4_error_stateid_expired(task->tk_status) ||
4658 nfs4_stateid_is_current(&args->stateid,
4659 args->context,
4660 args->lock_context,
4661 FMODE_READ))
4662 return false;
4663 rpc_restart_call_prepare(task);
4664 return true;
4665}
4666
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004667static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004668{
4669
4670 dprintk("--> %s\n", __func__);
4671
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004672 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004673 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004674 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004675 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004676 if (task->tk_status > 0)
4677 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004678 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4679 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004680}
4681
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004682static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4683 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004685 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004686 if (!hdr->pgio_done_cb)
4687 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004688 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004689 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690}
4691
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004692static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4693 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004694{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004695 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004696 &hdr->args.seq_args,
4697 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004698 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004699 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004700 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4701 hdr->args.lock_context,
4702 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004703 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004704 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004705 return -EIO;
4706 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707}
4708
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004709static int nfs4_write_done_cb(struct rpc_task *task,
4710 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004712 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004713
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004714 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004715 if (task->tk_status < 0) {
4716 struct nfs4_exception exception = {
4717 .inode = hdr->inode,
4718 .state = hdr->args.context->state,
4719 .stateid = &hdr->args.stateid,
4720 };
4721 task->tk_status = nfs4_async_handle_exception(task,
4722 NFS_SERVER(inode), task->tk_status,
4723 &exception);
4724 if (exception.retry) {
4725 rpc_restart_call_prepare(task);
4726 return -EAGAIN;
4727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004729 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004730 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004731 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004732 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004733 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734}
4735
Trond Myklebust5521abf2013-03-16 20:54:34 -04004736static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004737 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004738{
4739
4740 if (!nfs4_error_stateid_expired(task->tk_status) ||
4741 nfs4_stateid_is_current(&args->stateid,
4742 args->context,
4743 args->lock_context,
4744 FMODE_WRITE))
4745 return false;
4746 rpc_restart_call_prepare(task);
4747 return true;
4748}
4749
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004750static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004751{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004752 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004753 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004754 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004755 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004756 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4757 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004758}
4759
Trond Myklebust5a37f852012-04-28 14:55:16 -04004760static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004761bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004762{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004763 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004764 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004765 return false;
4766 /* Otherwise, request attributes if and only if we don't hold
4767 * a delegation
4768 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004769 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004770}
Fred Isamana69aef12011-03-03 15:13:47 +00004771
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004772static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4773 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004775 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004776
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004777 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4778 hdr->args.bitmask = NULL;
4779 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004780 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004781 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004782
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004783 if (!hdr->pgio_done_cb)
4784 hdr->pgio_done_cb = nfs4_write_done_cb;
4785 hdr->res.server = server;
4786 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004788 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004789 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790}
4791
Fred Isaman0b7c0152012-04-20 14:47:39 -04004792static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4793{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004794 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004795 &data->args.seq_args,
4796 &data->res.seq_res,
4797 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798}
4799
Fred Isaman0b7c0152012-04-20 14:47:39 -04004800static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004803
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004804 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004805 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4806 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004807 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004808 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004810 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811}
4812
Fred Isaman0b7c0152012-04-20 14:47:39 -04004813static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004814{
4815 if (!nfs4_sequence_done(task, &data->res.seq_res))
4816 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004817 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004818}
4819
Fred Isaman0b7c0152012-04-20 14:47:39 -04004820static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004822 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004823
Fred Isaman0b7c0152012-04-20 14:47:39 -04004824 if (data->commit_done_cb == NULL)
4825 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004826 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004827 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004828 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829}
4830
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004831struct nfs4_renewdata {
4832 struct nfs_client *client;
4833 unsigned long timestamp;
4834};
4835
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836/*
4837 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4838 * standalone procedure for queueing an asynchronous RENEW.
4839 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004840static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004841{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004842 struct nfs4_renewdata *data = calldata;
4843 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004844
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004845 if (atomic_read(&clp->cl_count) > 1)
4846 nfs4_schedule_state_renewal(clp);
4847 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004848 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004849}
4850
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004851static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004853 struct nfs4_renewdata *data = calldata;
4854 struct nfs_client *clp = data->client;
4855 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004857 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004858 switch (task->tk_status) {
4859 case 0:
4860 break;
4861 case -NFS4ERR_LEASE_MOVED:
4862 nfs4_schedule_lease_moved_recovery(clp);
4863 break;
4864 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004865 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004866 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4867 return;
4868 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004869 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004870 return;
4871 }
4872 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004874 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875}
4876
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004877static const struct rpc_call_ops nfs4_renew_ops = {
4878 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004879 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004880};
4881
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004882static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883{
4884 struct rpc_message msg = {
4885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4886 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004887 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004889 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004891 if (renew_flags == 0)
4892 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004893 if (!atomic_inc_not_zero(&clp->cl_count))
4894 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004895 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004896 if (data == NULL)
4897 return -ENOMEM;
4898 data->client = clp;
4899 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004900 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004901 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902}
4903
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004904static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905{
4906 struct rpc_message msg = {
4907 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4908 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004909 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 };
4911 unsigned long now = jiffies;
4912 int status;
4913
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004914 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 if (status < 0)
4916 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004917 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 return 0;
4919}
4920
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004921static inline int nfs4_server_supports_acls(struct nfs_server *server)
4922{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004923 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004924}
4925
Trond Myklebust21f498c2012-08-24 10:59:25 -04004926/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4927 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004928 * the stack.
4929 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004930#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004931
Neil Hormane9e3d722011-03-04 19:26:03 -05004932static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004933 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004934{
4935 struct page *newpage, **spages;
4936 int rc = 0;
4937 size_t len;
4938 spages = pages;
4939
4940 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004941 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004942 newpage = alloc_page(GFP_KERNEL);
4943
4944 if (newpage == NULL)
4945 goto unwind;
4946 memcpy(page_address(newpage), buf, len);
4947 buf += len;
4948 buflen -= len;
4949 *pages++ = newpage;
4950 rc++;
4951 } while (buflen != 0);
4952
4953 return rc;
4954
4955unwind:
4956 for(; rc > 0; rc--)
4957 __free_page(spages[rc-1]);
4958 return -ENOMEM;
4959}
4960
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004961struct nfs4_cached_acl {
4962 int cached;
4963 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004964 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004965};
4966
4967static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004968{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004969 struct nfs_inode *nfsi = NFS_I(inode);
4970
4971 spin_lock(&inode->i_lock);
4972 kfree(nfsi->nfs4_acl);
4973 nfsi->nfs4_acl = acl;
4974 spin_unlock(&inode->i_lock);
4975}
4976
4977static void nfs4_zap_acl_attr(struct inode *inode)
4978{
4979 nfs4_set_cached_acl(inode, NULL);
4980}
4981
4982static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4983{
4984 struct nfs_inode *nfsi = NFS_I(inode);
4985 struct nfs4_cached_acl *acl;
4986 int ret = -ENOENT;
4987
4988 spin_lock(&inode->i_lock);
4989 acl = nfsi->nfs4_acl;
4990 if (acl == NULL)
4991 goto out;
4992 if (buf == NULL) /* user is just asking for length */
4993 goto out_len;
4994 if (acl->cached == 0)
4995 goto out;
4996 ret = -ERANGE; /* see getxattr(2) man page */
4997 if (acl->len > buflen)
4998 goto out;
4999 memcpy(buf, acl->data, acl->len);
5000out_len:
5001 ret = acl->len;
5002out:
5003 spin_unlock(&inode->i_lock);
5004 return ret;
5005}
5006
Sachin Prabhu5794d212012-04-17 14:36:40 +01005007static 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 +00005008{
5009 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005010 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005011
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005012 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005013 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005014 if (acl == NULL)
5015 goto out;
5016 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005017 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005018 } else {
5019 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5020 if (acl == NULL)
5021 goto out;
5022 acl->cached = 0;
5023 }
5024 acl->len = acl_len;
5025out:
5026 nfs4_set_cached_acl(inode, acl);
5027}
5028
Andy Adamsonbf118a32011-12-07 11:55:27 -05005029/*
5030 * The getxattr API returns the required buffer length when called with a
5031 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5032 * the required buf. On a NULL buf, we send a page of data to the server
5033 * guessing that the ACL request can be serviced by a page. If so, we cache
5034 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5035 * the cache. If not so, we throw away the page, and cache the required
5036 * length. The next getxattr call will then produce another round trip to
5037 * the server, this time with the input buf of the required size.
5038 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005039static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005040{
Andy Adamsonbf118a32011-12-07 11:55:27 -05005041 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005042 struct nfs_getaclargs args = {
5043 .fh = NFS_FH(inode),
5044 .acl_pages = pages,
5045 .acl_len = buflen,
5046 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005047 struct nfs_getaclres res = {
5048 .acl_len = buflen,
5049 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005050 struct rpc_message msg = {
5051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5052 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005053 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005054 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005055 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5056 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005057
Andy Adamsonbf118a32011-12-07 11:55:27 -05005058 /* As long as we're doing a round trip to the server anyway,
5059 * let's be prepared for a page of acl data. */
5060 if (npages == 0)
5061 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005062 if (npages > ARRAY_SIZE(pages))
5063 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005064
Andy Adamsonbf118a32011-12-07 11:55:27 -05005065 for (i = 0; i < npages; i++) {
5066 pages[i] = alloc_page(GFP_KERNEL);
5067 if (!pages[i])
5068 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005069 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005070
5071 /* for decoding across pages */
5072 res.acl_scratch = alloc_page(GFP_KERNEL);
5073 if (!res.acl_scratch)
5074 goto out_free;
5075
Andy Adamsonbf118a32011-12-07 11:55:27 -05005076 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005077
Peng Taode040be2012-01-10 22:42:47 +08005078 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005079 __func__, buf, buflen, npages, args.acl_len);
5080 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5081 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005082 if (ret)
5083 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005084
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005085 /* Handle the case where the passed-in buffer is too short */
5086 if (res.acl_flags & NFS4_ACL_TRUNC) {
5087 /* Did the user only issue a request for the acl length? */
5088 if (buf == NULL)
5089 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005090 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005091 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005092 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005093 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005094 if (buf) {
5095 if (res.acl_len > buflen) {
5096 ret = -ERANGE;
5097 goto out_free;
5098 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005099 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005100 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005101out_ok:
5102 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005103out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005104 for (i = 0; i < npages; i++)
5105 if (pages[i])
5106 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005107 if (res.acl_scratch)
5108 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005109 return ret;
5110}
5111
Trond Myklebust16b42892006-08-24 12:27:15 -04005112static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5113{
5114 struct nfs4_exception exception = { };
5115 ssize_t ret;
5116 do {
5117 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005118 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005119 if (ret >= 0)
5120 break;
5121 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5122 } while (exception.retry);
5123 return ret;
5124}
5125
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005126static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5127{
5128 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005129 int ret;
5130
5131 if (!nfs4_server_supports_acls(server))
5132 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005133 ret = nfs_revalidate_inode(server, inode);
5134 if (ret < 0)
5135 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005136 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5137 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005138 ret = nfs4_read_cached_acl(inode, buf, buflen);
5139 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005140 /* -ENOENT is returned if there is no ACL or if there is an ACL
5141 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005142 return ret;
5143 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005144}
5145
Trond Myklebust16b42892006-08-24 12:27:15 -04005146static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005147{
5148 struct nfs_server *server = NFS_SERVER(inode);
5149 struct page *pages[NFS4ACL_MAXPAGES];
5150 struct nfs_setaclargs arg = {
5151 .fh = NFS_FH(inode),
5152 .acl_pages = pages,
5153 .acl_len = buflen,
5154 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005155 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005156 struct rpc_message msg = {
5157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5158 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005159 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005160 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005161 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005162 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005163
5164 if (!nfs4_server_supports_acls(server))
5165 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005166 if (npages > ARRAY_SIZE(pages))
5167 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005168 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005169 if (i < 0)
5170 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005171 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005172 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005173
5174 /*
5175 * Free each page after tx, so the only ref left is
5176 * held by the network stack
5177 */
5178 for (; i > 0; i--)
5179 put_page(pages[i-1]);
5180
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005181 /*
5182 * Acl update can result in inode attribute update.
5183 * so mark the attribute cache invalid.
5184 */
5185 spin_lock(&inode->i_lock);
5186 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5187 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005188 nfs_access_zap_cache(inode);
5189 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005190 return ret;
5191}
5192
Trond Myklebust16b42892006-08-24 12:27:15 -04005193static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5194{
5195 struct nfs4_exception exception = { };
5196 int err;
5197 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005198 err = __nfs4_proc_set_acl(inode, buf, buflen);
5199 trace_nfs4_set_acl(inode, err);
5200 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005201 &exception);
5202 } while (exception.retry);
5203 return err;
5204}
5205
David Quigleyaa9c2662013-05-22 12:50:44 -04005206#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5207static int _nfs4_get_security_label(struct inode *inode, void *buf,
5208 size_t buflen)
5209{
5210 struct nfs_server *server = NFS_SERVER(inode);
5211 struct nfs_fattr fattr;
5212 struct nfs4_label label = {0, 0, buflen, buf};
5213
5214 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005215 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005216 .fh = NFS_FH(inode),
5217 .bitmask = bitmask,
5218 };
5219 struct nfs4_getattr_res res = {
5220 .fattr = &fattr,
5221 .label = &label,
5222 .server = server,
5223 };
5224 struct rpc_message msg = {
5225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005226 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005227 .rpc_resp = &res,
5228 };
5229 int ret;
5230
5231 nfs_fattr_init(&fattr);
5232
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005233 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005234 if (ret)
5235 return ret;
5236 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5237 return -ENOENT;
5238 if (buflen < label.len)
5239 return -ERANGE;
5240 return 0;
5241}
5242
5243static int nfs4_get_security_label(struct inode *inode, void *buf,
5244 size_t buflen)
5245{
5246 struct nfs4_exception exception = { };
5247 int err;
5248
5249 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5250 return -EOPNOTSUPP;
5251
5252 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005253 err = _nfs4_get_security_label(inode, buf, buflen);
5254 trace_nfs4_get_security_label(inode, err);
5255 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005256 &exception);
5257 } while (exception.retry);
5258 return err;
5259}
5260
5261static int _nfs4_do_set_security_label(struct inode *inode,
5262 struct nfs4_label *ilabel,
5263 struct nfs_fattr *fattr,
5264 struct nfs4_label *olabel)
5265{
5266
5267 struct iattr sattr = {0};
5268 struct nfs_server *server = NFS_SERVER(inode);
5269 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005270 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005271 .fh = NFS_FH(inode),
5272 .iap = &sattr,
5273 .server = server,
5274 .bitmask = bitmask,
5275 .label = ilabel,
5276 };
5277 struct nfs_setattrres res = {
5278 .fattr = fattr,
5279 .label = olabel,
5280 .server = server,
5281 };
5282 struct rpc_message msg = {
5283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005284 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005285 .rpc_resp = &res,
5286 };
5287 int status;
5288
Jeff Layton12207f62013-11-01 10:49:32 -04005289 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005290
Jeff Layton12207f62013-11-01 10:49:32 -04005291 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005292 if (status)
5293 dprintk("%s failed: %d\n", __func__, status);
5294
5295 return status;
5296}
5297
5298static int nfs4_do_set_security_label(struct inode *inode,
5299 struct nfs4_label *ilabel,
5300 struct nfs_fattr *fattr,
5301 struct nfs4_label *olabel)
5302{
5303 struct nfs4_exception exception = { };
5304 int err;
5305
5306 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005307 err = _nfs4_do_set_security_label(inode, ilabel,
5308 fattr, olabel);
5309 trace_nfs4_set_security_label(inode, err);
5310 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005311 &exception);
5312 } while (exception.retry);
5313 return err;
5314}
5315
5316static int
Al Viro59301222016-05-27 10:19:30 -04005317nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005318{
5319 struct nfs4_label ilabel, *olabel = NULL;
5320 struct nfs_fattr fattr;
5321 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005322 int status;
5323
5324 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5325 return -EOPNOTSUPP;
5326
5327 nfs_fattr_init(&fattr);
5328
5329 ilabel.pi = 0;
5330 ilabel.lfs = 0;
5331 ilabel.label = (char *)buf;
5332 ilabel.len = buflen;
5333
5334 cred = rpc_lookup_cred();
5335 if (IS_ERR(cred))
5336 return PTR_ERR(cred);
5337
5338 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5339 if (IS_ERR(olabel)) {
5340 status = -PTR_ERR(olabel);
5341 goto out;
5342 }
5343
5344 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5345 if (status == 0)
5346 nfs_setsecurity(inode, &fattr, olabel);
5347
5348 nfs4_label_free(olabel);
5349out:
5350 put_rpccred(cred);
5351 return status;
5352}
5353#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5354
5355
Chuck Leverf0920752012-05-21 22:45:41 -04005356static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5357 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005358{
5359 __be32 verf[2];
5360
Chuck Lever2c820d92012-05-21 22:45:33 -04005361 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5362 /* An impossible timestamp guarantees this value
5363 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005364 verf[0] = cpu_to_be32(U32_MAX);
5365 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005366 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005367 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005368 u64 ns = ktime_to_ns(nn->boot_time);
5369
5370 verf[0] = cpu_to_be32(ns >> 32);
5371 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005372 }
Chuck Levercd937102012-03-02 17:14:31 -05005373 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5374}
5375
Jeff Laytona3192682015-06-09 19:43:59 -04005376static int
5377nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005378{
Jeff Laytona3192682015-06-09 19:43:59 -04005379 size_t len;
5380 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005381
Trond Myklebustceb3a162015-01-03 15:16:04 -05005382 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005383 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005384
Jeff Laytona3192682015-06-09 19:43:59 -04005385 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005386 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005387 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5388 1 +
5389 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5390 1;
5391 rcu_read_unlock();
5392
5393 if (len > NFS4_OPAQUE_LIMIT + 1)
5394 return -EINVAL;
5395
5396 /*
5397 * Since this string is allocated at mount time, and held until the
5398 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5399 * about a memory-reclaim deadlock.
5400 */
5401 str = kmalloc(len, GFP_KERNEL);
5402 if (!str)
5403 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005404
Chuck Levere984a552012-09-14 17:24:21 -04005405 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005406 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005407 clp->cl_ipaddr,
5408 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5409 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005410 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005411
Jeff Laytona3192682015-06-09 19:43:59 -04005412 clp->cl_owner_id = str;
5413 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005414}
5415
Jeff Layton873e3852015-06-09 19:44:00 -04005416static int
5417nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005418{
Jeff Layton873e3852015-06-09 19:44:00 -04005419 size_t len;
5420 char *str;
5421
5422 len = 10 + 10 + 1 + 10 + 1 +
5423 strlen(nfs4_client_id_uniquifier) + 1 +
5424 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5425
5426 if (len > NFS4_OPAQUE_LIMIT + 1)
5427 return -EINVAL;
5428
5429 /*
5430 * Since this string is allocated at mount time, and held until the
5431 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5432 * about a memory-reclaim deadlock.
5433 */
5434 str = kmalloc(len, GFP_KERNEL);
5435 if (!str)
5436 return -ENOMEM;
5437
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005438 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005439 clp->rpc_ops->version, clp->cl_minorversion,
5440 nfs4_client_id_uniquifier,
5441 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005442 clp->cl_owner_id = str;
5443 return 0;
5444}
5445
5446static int
5447nfs4_init_uniform_client_string(struct nfs_client *clp)
5448{
Jeff Layton873e3852015-06-09 19:44:00 -04005449 size_t len;
5450 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005451
5452 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005453 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005454
5455 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005456 return nfs4_init_uniquifier_client_string(clp);
5457
5458 len = 10 + 10 + 1 + 10 + 1 +
5459 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5460
5461 if (len > NFS4_OPAQUE_LIMIT + 1)
5462 return -EINVAL;
5463
5464 /*
5465 * Since this string is allocated at mount time, and held until the
5466 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5467 * about a memory-reclaim deadlock.
5468 */
5469 str = kmalloc(len, GFP_KERNEL);
5470 if (!str)
5471 return -ENOMEM;
5472
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005473 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005474 clp->rpc_ops->version, clp->cl_minorversion,
5475 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005476 clp->cl_owner_id = str;
5477 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005478}
5479
Chuck Lever706cb8d2014-03-12 12:51:47 -04005480/*
5481 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5482 * services. Advertise one based on the address family of the
5483 * clientaddr.
5484 */
5485static unsigned int
5486nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5487{
5488 if (strchr(clp->cl_ipaddr, ':') != NULL)
5489 return scnprintf(buf, len, "tcp6");
5490 else
5491 return scnprintf(buf, len, "tcp");
5492}
5493
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005494static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5495{
5496 struct nfs4_setclientid *sc = calldata;
5497
5498 if (task->tk_status == 0)
5499 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5500}
5501
5502static const struct rpc_call_ops nfs4_setclientid_ops = {
5503 .rpc_call_done = nfs4_setclientid_done,
5504};
5505
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005506/**
5507 * nfs4_proc_setclientid - Negotiate client ID
5508 * @clp: state data structure
5509 * @program: RPC program for NFSv4 callback service
5510 * @port: IP port number for NFS4 callback service
5511 * @cred: RPC credential to use for this call
5512 * @res: where to place the result
5513 *
5514 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5515 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005516int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5517 unsigned short port, struct rpc_cred *cred,
5518 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519{
5520 nfs4_verifier sc_verifier;
5521 struct nfs4_setclientid setclientid = {
5522 .sc_verifier = &sc_verifier,
5523 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005524 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 };
5526 struct rpc_message msg = {
5527 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5528 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005529 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005530 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005532 struct rpc_task *task;
5533 struct rpc_task_setup task_setup_data = {
5534 .rpc_client = clp->cl_rpcclient,
5535 .rpc_message = &msg,
5536 .callback_ops = &nfs4_setclientid_ops,
5537 .callback_data = &setclientid,
5538 .flags = RPC_TASK_TIMEOUT,
5539 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005540 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541
Chuck Leverde734832012-07-11 16:30:50 -04005542 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005543 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005544
5545 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5546 status = nfs4_init_uniform_client_string(clp);
5547 else
Jeff Laytona3192682015-06-09 19:43:59 -04005548 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005549
5550 if (status)
5551 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005552
Chuck Leverde734832012-07-11 16:30:50 -04005553 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005554 setclientid.sc_netid_len =
5555 nfs4_init_callback_netid(clp,
5556 setclientid.sc_netid,
5557 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005558 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005559 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 clp->cl_ipaddr, port >> 8, port & 255);
5561
Jeff Layton3a6bb732015-06-09 19:43:57 -04005562 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005563 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005564 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005565 task = rpc_run_task(&task_setup_data);
5566 if (IS_ERR(task)) {
5567 status = PTR_ERR(task);
5568 goto out;
5569 }
5570 status = task->tk_status;
5571 if (setclientid.sc_cred) {
5572 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5573 put_rpccred(setclientid.sc_cred);
5574 }
5575 rpc_put_task(task);
5576out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005577 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005578 dprintk("NFS reply setclientid: %d\n", status);
5579 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580}
5581
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005582/**
5583 * nfs4_proc_setclientid_confirm - Confirm client ID
5584 * @clp: state data structure
5585 * @res: result of a previous SETCLIENTID
5586 * @cred: RPC credential to use for this call
5587 *
5588 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5589 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005590int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005591 struct nfs4_setclientid_res *arg,
5592 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 struct rpc_message msg = {
5595 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005596 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005597 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 int status;
5600
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005601 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5602 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5603 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005604 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005605 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005606 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 return status;
5608}
5609
Trond Myklebustfe650402006-01-03 09:55:18 +01005610struct nfs4_delegreturndata {
5611 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005612 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005613 struct nfs_fh fh;
5614 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005615 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005616 struct {
5617 struct nfs4_layoutreturn_args arg;
5618 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005619 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005620 u32 roc_barrier;
5621 bool roc;
5622 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005623 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005624 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005625 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005626};
5627
Trond Myklebustfe650402006-01-03 09:55:18 +01005628static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5629{
5630 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005631
Trond Myklebust14516c32010-07-31 14:29:06 -04005632 if (!nfs4_sequence_done(task, &data->res.seq_res))
5633 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005634
Trond Myklebustca8acf82013-08-13 10:36:56 -04005635 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005636
5637 /* Handle Layoutreturn errors */
5638 if (data->args.lr_args && task->tk_status != 0) {
5639 switch(data->res.lr_ret) {
5640 default:
5641 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5642 break;
5643 case 0:
5644 data->args.lr_args = NULL;
5645 data->res.lr_res = NULL;
5646 break;
5647 case -NFS4ERR_ADMIN_REVOKED:
5648 case -NFS4ERR_DELEG_REVOKED:
5649 case -NFS4ERR_EXPIRED:
5650 case -NFS4ERR_BAD_STATEID:
5651 case -NFS4ERR_OLD_STATEID:
5652 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5653 case -NFS4ERR_WRONG_CRED:
5654 data->args.lr_args = NULL;
5655 data->res.lr_res = NULL;
5656 data->res.lr_ret = 0;
5657 rpc_restart_call_prepare(task);
5658 return;
5659 }
5660 }
5661
Ricardo Labiaga79708862009-12-07 09:23:21 -05005662 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005663 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005664 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005665 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005666 case -NFS4ERR_ADMIN_REVOKED:
5667 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005668 case -NFS4ERR_EXPIRED:
5669 nfs4_free_revoked_stateid(data->res.server,
5670 data->args.stateid,
5671 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005672 case -NFS4ERR_BAD_STATEID:
5673 case -NFS4ERR_OLD_STATEID:
5674 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005675 task->tk_status = 0;
5676 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005677 case -NFS4ERR_ACCESS:
5678 if (data->args.bitmask) {
5679 data->args.bitmask = NULL;
5680 data->res.fattr = NULL;
5681 task->tk_status = 0;
5682 rpc_restart_call_prepare(task);
5683 return;
5684 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005685 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005686 if (nfs4_async_handle_error(task, data->res.server,
5687 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005688 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005689 return;
5690 }
5691 }
5692 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005693}
5694
5695static void nfs4_delegreturn_release(void *calldata)
5696{
Peng Tao039b7562014-07-03 13:05:02 +08005697 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005698 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005699
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005700 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005701 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005702 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5703 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005704 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005705 nfs_iput_and_deactive(inode);
5706 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005707 kfree(calldata);
5708}
5709
Andy Adamson938e1012009-04-01 09:22:28 -04005710static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5711{
5712 struct nfs4_delegreturndata *d_data;
5713
5714 d_data = (struct nfs4_delegreturndata *)data;
5715
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005716 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005717 return;
5718
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005719 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005720 &d_data->args.seq_args,
5721 &d_data->res.seq_res,
5722 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005723}
Andy Adamson938e1012009-04-01 09:22:28 -04005724
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005725static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005726 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005727 .rpc_call_done = nfs4_delegreturn_done,
5728 .rpc_release = nfs4_delegreturn_release,
5729};
5730
Trond Myklebuste6f81072008-01-24 18:14:34 -05005731static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005732{
5733 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005734 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005735 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005736 struct rpc_message msg = {
5737 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5738 .rpc_cred = cred,
5739 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005740 struct rpc_task_setup task_setup_data = {
5741 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005742 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005743 .callback_ops = &nfs4_delegreturn_ops,
5744 .flags = RPC_TASK_ASYNC,
5745 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005746 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005747
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005748 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005749 if (data == NULL)
5750 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005751 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005752
5753 nfs4_state_protect(server->nfs_client,
5754 NFS_SP4_MACH_CRED_CLEANUP,
5755 &task_setup_data.rpc_client, &msg);
5756
Trond Myklebustfe650402006-01-03 09:55:18 +01005757 data->args.fhandle = &data->fh;
5758 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005759 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005760 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005761 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005762 data->res.fattr = &data->fattr;
5763 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005764 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005765 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005766 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005767 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005768 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005769 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005770 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005771 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005772 if (data->lr.roc) {
5773 data->args.lr_args = &data->lr.arg;
5774 data->res.lr_res = &data->lr.res;
5775 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005776 } else if (data->lr.roc) {
5777 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5778 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005779 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005780
Trond Myklebustc970aa82007-07-14 15:39:59 -04005781 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005782 msg.rpc_argp = &data->args;
5783 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005784 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005785 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005786 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005787 if (!issync)
5788 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005789 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005790 if (status != 0)
5791 goto out;
5792 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005793out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005794 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005795 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796}
5797
Trond Myklebuste6f81072008-01-24 18:14:34 -05005798int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799{
5800 struct nfs_server *server = NFS_SERVER(inode);
5801 struct nfs4_exception exception = { };
5802 int err;
5803 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005804 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005805 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 switch (err) {
5807 case -NFS4ERR_STALE_STATEID:
5808 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 case 0:
5810 return 0;
5811 }
5812 err = nfs4_handle_exception(server, err, &exception);
5813 } while (exception.retry);
5814 return err;
5815}
5816
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5818{
5819 struct inode *inode = state->inode;
5820 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005821 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005822 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005824 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005826 struct nfs_lockt_res res = {
5827 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 };
5829 struct rpc_message msg = {
5830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5831 .rpc_argp = &arg,
5832 .rpc_resp = &res,
5833 .rpc_cred = state->owner->so_cred,
5834 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 struct nfs4_lock_state *lsp;
5836 int status;
5837
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005838 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005839 status = nfs4_set_lock_state(state, request);
5840 if (status != 0)
5841 goto out;
5842 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005843 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005844 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005845 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005846 switch (status) {
5847 case 0:
5848 request->fl_type = F_UNLCK;
5849 break;
5850 case -NFS4ERR_DENIED:
5851 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005853 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005854 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005855out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 return status;
5857}
5858
5859static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5860{
5861 struct nfs4_exception exception = { };
5862 int err;
5863
5864 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005865 err = _nfs4_proc_getlk(state, cmd, request);
5866 trace_nfs4_get_lock(request, state, cmd, err);
5867 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 &exception);
5869 } while (exception.retry);
5870 return err;
5871}
5872
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005873struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005874 struct nfs_locku_args arg;
5875 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005876 struct nfs4_lock_state *lsp;
5877 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005878 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005879 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005880 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005881};
5882
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005883static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5884 struct nfs_open_context *ctx,
5885 struct nfs4_lock_state *lsp,
5886 struct nfs_seqid *seqid)
5887{
5888 struct nfs4_unlockdata *p;
5889 struct inode *inode = lsp->ls_state->inode;
5890
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005891 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005892 if (p == NULL)
5893 return NULL;
5894 p->arg.fh = NFS_FH(inode);
5895 p->arg.fl = &p->fl;
5896 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005897 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005898 p->lsp = lsp;
5899 atomic_inc(&lsp->ls_count);
5900 /* Ensure we don't close file until we're done freeing locks! */
5901 p->ctx = get_nfs_open_context(ctx);
5902 memcpy(&p->fl, fl, sizeof(p->fl));
5903 p->server = NFS_SERVER(inode);
5904 return p;
5905}
5906
Trond Myklebust06f814a2006-01-03 09:55:07 +01005907static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005908{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005909 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005910 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005911 nfs4_put_lock_state(calldata->lsp);
5912 put_nfs_open_context(calldata->ctx);
5913 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005914}
5915
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005916static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005917{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005918 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005919
Trond Myklebust14516c32010-07-31 14:29:06 -04005920 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5921 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005922 switch (task->tk_status) {
5923 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005924 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005925 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005926 if (nfs4_update_lock_stateid(calldata->lsp,
5927 &calldata->res.stateid))
5928 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005929 case -NFS4ERR_ADMIN_REVOKED:
5930 case -NFS4ERR_EXPIRED:
5931 nfs4_free_revoked_stateid(calldata->server,
5932 &calldata->arg.stateid,
5933 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005934 case -NFS4ERR_BAD_STATEID:
5935 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005936 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005937 if (!nfs4_stateid_match(&calldata->arg.stateid,
5938 &calldata->lsp->ls_stateid))
5939 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005940 break;
5941 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005942 if (nfs4_async_handle_error(task, calldata->server,
5943 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005944 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005945 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005946 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005947}
5948
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005949static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005950{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005951 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005953 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005954 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005955 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005956 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005957 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005958 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005959 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005960 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005961 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04005962 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005963 &calldata->res.seq_res,
5964 task) != 0)
5965 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005966 return;
5967out_no_action:
5968 task->tk_action = NULL;
5969out_wait:
5970 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971}
5972
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005973static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005974 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005975 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005976 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005977};
5978
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005979static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5980 struct nfs_open_context *ctx,
5981 struct nfs4_lock_state *lsp,
5982 struct nfs_seqid *seqid)
5983{
5984 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005985 struct rpc_message msg = {
5986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5987 .rpc_cred = ctx->cred,
5988 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005989 struct rpc_task_setup task_setup_data = {
5990 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005991 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005992 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005993 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005994 .flags = RPC_TASK_ASYNC,
5995 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005996
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005997 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5998 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5999
Frank Filz137d6ac2007-07-09 15:32:29 -07006000 /* Ensure this is an unlock - when canceling a lock, the
6001 * canceled lock is passed in, and it won't be an unlock.
6002 */
6003 fl->fl_type = F_UNLCK;
6004
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006005 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6006 if (data == NULL) {
6007 nfs_free_seqid(seqid);
6008 return ERR_PTR(-ENOMEM);
6009 }
6010
Chuck Levera9c92d62013-08-09 12:48:18 -04006011 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006012 msg.rpc_argp = &data->arg;
6013 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006014 task_setup_data.callback_data = data;
6015 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006016}
6017
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6019{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006020 struct inode *inode = state->inode;
6021 struct nfs4_state_owner *sp = state->owner;
6022 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006023 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006024 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006025 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006026 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006027 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006028 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029
Trond Myklebust9b073572006-06-29 16:38:34 -04006030 status = nfs4_set_lock_state(state, request);
6031 /* Unlock _before_ we do the RPC call */
6032 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006033 /* Exclude nfs_delegation_claim_locks() */
6034 mutex_lock(&sp->so_delegreturn_mutex);
6035 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006036 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006037 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006038 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006039 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006040 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006041 }
6042 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006043 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006044 if (status != 0)
6045 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006046 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006047 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006048 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6049 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006050 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6051 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006052 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006053 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006054 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006055 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006056 status = PTR_ERR(task);
6057 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006058 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006059 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006060 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006061out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006062 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006063 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006064 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065}
6066
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006067struct nfs4_lockdata {
6068 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006069 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006070 struct nfs4_lock_state *lsp;
6071 struct nfs_open_context *ctx;
6072 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006073 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006074 int rpc_status;
6075 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006076 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006077};
6078
6079static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006080 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6081 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006082{
6083 struct nfs4_lockdata *p;
6084 struct inode *inode = lsp->ls_state->inode;
6085 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006086 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006087
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006088 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006089 if (p == NULL)
6090 return NULL;
6091
6092 p->arg.fh = NFS_FH(inode);
6093 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006094 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006095 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006096 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006097 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6098 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006099 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006100 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006101 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006102 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006103 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006104 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006105 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006106 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006107 atomic_inc(&lsp->ls_count);
6108 p->ctx = get_nfs_open_context(ctx);
6109 memcpy(&p->fl, fl, sizeof(p->fl));
6110 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006111out_free_seqid:
6112 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006113out_free:
6114 kfree(p);
6115 return NULL;
6116}
6117
6118static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6119{
6120 struct nfs4_lockdata *data = calldata;
6121 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122
Harvey Harrison3110ff82008-05-02 13:42:44 -07006123 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006124 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006125 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006126 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006127 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006128 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006129 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006130 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006131 nfs4_stateid_copy(&data->arg.open_stateid,
6132 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006133 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006134 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006135 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006136 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006137 nfs4_stateid_copy(&data->arg.lock_stateid,
6138 &data->lsp->ls_stateid);
6139 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006140 if (!nfs4_valid_open_stateid(state)) {
6141 data->rpc_status = -EBADF;
6142 task->tk_action = NULL;
6143 goto out_release_open_seqid;
6144 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006145 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006146 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006147 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006148 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006149 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006150 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006151out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006152 nfs_release_seqid(data->arg.open_seqid);
6153out_release_lock_seqid:
6154 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006155out_wait:
6156 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006157 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006158}
6159
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006160static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6161{
6162 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006163 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006164
Harvey Harrison3110ff82008-05-02 13:42:44 -07006165 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006166
Trond Myklebust14516c32010-07-31 14:29:06 -04006167 if (!nfs4_sequence_done(task, &data->res.seq_res))
6168 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006169
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006170 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006171 switch (task->tk_status) {
6172 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006173 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006174 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006175 if (data->arg.new_lock) {
6176 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006177 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006178 rpc_restart_call_prepare(task);
6179 break;
6180 }
6181 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006182 if (data->arg.new_lock_owner != 0) {
6183 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6184 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6185 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6186 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6187 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006188 break;
6189 case -NFS4ERR_BAD_STATEID:
6190 case -NFS4ERR_OLD_STATEID:
6191 case -NFS4ERR_STALE_STATEID:
6192 case -NFS4ERR_EXPIRED:
6193 if (data->arg.new_lock_owner != 0) {
6194 if (!nfs4_stateid_match(&data->arg.open_stateid,
6195 &lsp->ls_state->open_stateid))
6196 rpc_restart_call_prepare(task);
6197 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6198 &lsp->ls_stateid))
6199 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006200 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006201 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006202}
6203
6204static void nfs4_lock_release(void *calldata)
6205{
6206 struct nfs4_lockdata *data = calldata;
6207
Harvey Harrison3110ff82008-05-02 13:42:44 -07006208 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006209 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006210 if (data->cancelled != 0) {
6211 struct rpc_task *task;
6212 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6213 data->arg.lock_seqid);
6214 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006215 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006216 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006217 } else
6218 nfs_free_seqid(data->arg.lock_seqid);
6219 nfs4_put_lock_state(data->lsp);
6220 put_nfs_open_context(data->ctx);
6221 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006222 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006223}
6224
6225static const struct rpc_call_ops nfs4_lock_ops = {
6226 .rpc_call_prepare = nfs4_lock_prepare,
6227 .rpc_call_done = nfs4_lock_done,
6228 .rpc_release = nfs4_lock_release,
6229};
6230
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006231static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6232{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006233 switch (error) {
6234 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006235 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006236 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006237 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006238 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006239 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006240 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006241 break;
6242 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006243 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006244 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006245 };
6246}
6247
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006248static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006249{
6250 struct nfs4_lockdata *data;
6251 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006252 struct rpc_message msg = {
6253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6254 .rpc_cred = state->owner->so_cred,
6255 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006256 struct rpc_task_setup task_setup_data = {
6257 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006258 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006259 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006260 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006261 .flags = RPC_TASK_ASYNC,
6262 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006263 int ret;
6264
Harvey Harrison3110ff82008-05-02 13:42:44 -07006265 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006266 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006267 fl->fl_u.nfs4_fl.owner,
6268 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006269 if (data == NULL)
6270 return -ENOMEM;
6271 if (IS_SETLKW(cmd))
6272 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006273 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006274 msg.rpc_argp = &data->arg;
6275 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006276 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006277 if (recovery_type > NFS_LOCK_NEW) {
6278 if (recovery_type == NFS_LOCK_RECLAIM)
6279 data->arg.reclaim = NFS_LOCK_RECLAIM;
6280 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006281 } else
6282 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006283 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006284 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006285 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006286 ret = nfs4_wait_for_completion_rpc_task(task);
6287 if (ret == 0) {
6288 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006289 if (ret)
6290 nfs4_handle_setlk_error(data->server, data->lsp,
6291 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006292 } else
6293 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006294 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006295 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006296 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006297 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298}
6299
6300static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6301{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006302 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006303 struct nfs4_exception exception = {
6304 .inode = state->inode,
6305 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006306 int err;
6307
6308 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006309 /* Cache the lock if possible... */
6310 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6311 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006312 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006313 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006314 break;
6315 nfs4_handle_exception(server, err, &exception);
6316 } while (exception.retry);
6317 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318}
6319
6320static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6321{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006322 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006323 struct nfs4_exception exception = {
6324 .inode = state->inode,
6325 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006326 int err;
6327
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006328 err = nfs4_set_lock_state(state, request);
6329 if (err != 0)
6330 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006331 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006332 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6333 return 0;
6334 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006335 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006336 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6337 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006338 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006339 switch (err) {
6340 default:
6341 goto out;
6342 case -NFS4ERR_GRACE:
6343 case -NFS4ERR_DELAY:
6344 nfs4_handle_exception(server, err, &exception);
6345 err = 0;
6346 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006347 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006348out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006349 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350}
6351
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006352#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006353static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6354{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006355 struct nfs4_lock_state *lsp;
6356 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006357
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006358 status = nfs4_set_lock_state(state, request);
6359 if (status != 0)
6360 return status;
6361 lsp = request->fl_u.nfs4_fl.owner;
6362 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6363 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6364 return 0;
6365 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006366 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006367}
6368#endif
6369
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6371{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006372 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006373 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006374 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006375 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376
Trond Myklebust01c3b862006-06-29 16:38:39 -04006377 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006378 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006379 if (status < 0)
6380 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006381 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006382 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006383 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006384 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006385 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006386 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006387 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006388 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006389 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006390 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006391 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006392 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006393 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006394 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006395out:
6396 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397 return status;
6398}
6399
6400static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6401{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006402 struct nfs4_exception exception = {
6403 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006404 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006405 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 int err;
6407
6408 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006409 err = _nfs4_proc_setlk(state, cmd, request);
6410 if (err == -NFS4ERR_DENIED)
6411 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006413 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 } while (exception.retry);
6415 return err;
6416}
6417
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006418#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6419#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6420
6421static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006422nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6423 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006424{
6425 int status = -ERESTARTSYS;
6426 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6427
6428 while(!signalled()) {
6429 status = nfs4_proc_setlk(state, cmd, request);
6430 if ((status != -EAGAIN) || IS_SETLK(cmd))
6431 break;
6432 freezable_schedule_timeout_interruptible(timeout);
6433 timeout *= 2;
6434 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6435 status = -ERESTARTSYS;
6436 }
6437 return status;
6438}
6439
Jeff Laytona1d617d82016-09-17 18:17:39 -04006440#ifdef CONFIG_NFS_V4_1
6441struct nfs4_lock_waiter {
6442 struct task_struct *task;
6443 struct inode *inode;
6444 struct nfs_lowner *owner;
6445 bool notified;
6446};
6447
6448static int
6449nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6450{
6451 int ret;
6452 struct cb_notify_lock_args *cbnl = key;
6453 struct nfs4_lock_waiter *waiter = wait->private;
6454 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6455 *wowner = waiter->owner;
6456
6457 /* Only wake if the callback was for the same owner */
6458 if (lowner->clientid != wowner->clientid ||
6459 lowner->id != wowner->id ||
6460 lowner->s_dev != wowner->s_dev)
6461 return 0;
6462
6463 /* Make sure it's for the right inode */
6464 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6465 return 0;
6466
6467 waiter->notified = true;
6468
6469 /* override "private" so we can use default_wake_function */
6470 wait->private = waiter->task;
6471 ret = autoremove_wake_function(wait, mode, flags, key);
6472 wait->private = waiter;
6473 return ret;
6474}
6475
6476static int
6477nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6478{
6479 int status = -ERESTARTSYS;
6480 unsigned long flags;
6481 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6482 struct nfs_server *server = NFS_SERVER(state->inode);
6483 struct nfs_client *clp = server->nfs_client;
6484 wait_queue_head_t *q = &clp->cl_lock_waitq;
6485 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6486 .id = lsp->ls_seqid.owner_id,
6487 .s_dev = server->s_dev };
6488 struct nfs4_lock_waiter waiter = { .task = current,
6489 .inode = state->inode,
6490 .owner = &owner,
6491 .notified = false };
6492 wait_queue_t wait;
6493
6494 /* Don't bother with waitqueue if we don't expect a callback */
6495 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6496 return nfs4_retry_setlk_simple(state, cmd, request);
6497
6498 init_wait(&wait);
6499 wait.private = &waiter;
6500 wait.func = nfs4_wake_lock_waiter;
6501 add_wait_queue(q, &wait);
6502
6503 while(!signalled()) {
6504 status = nfs4_proc_setlk(state, cmd, request);
6505 if ((status != -EAGAIN) || IS_SETLK(cmd))
6506 break;
6507
6508 status = -ERESTARTSYS;
6509 spin_lock_irqsave(&q->lock, flags);
6510 if (waiter.notified) {
6511 spin_unlock_irqrestore(&q->lock, flags);
6512 continue;
6513 }
6514 set_current_state(TASK_INTERRUPTIBLE);
6515 spin_unlock_irqrestore(&q->lock, flags);
6516
6517 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6518 }
6519
6520 finish_wait(q, &wait);
6521 return status;
6522}
6523#else /* !CONFIG_NFS_V4_1 */
6524static inline int
6525nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6526{
6527 return nfs4_retry_setlk_simple(state, cmd, request);
6528}
6529#endif
6530
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531static int
6532nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6533{
6534 struct nfs_open_context *ctx;
6535 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536 int status;
6537
6538 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006539 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540 state = ctx->state;
6541
6542 if (request->fl_start < 0 || request->fl_end < 0)
6543 return -EINVAL;
6544
Trond Myklebustd9531262009-07-21 19:22:38 -04006545 if (IS_GETLK(cmd)) {
6546 if (state != NULL)
6547 return nfs4_proc_getlk(state, F_GETLK, request);
6548 return 0;
6549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550
6551 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6552 return -EINVAL;
6553
Trond Myklebustd9531262009-07-21 19:22:38 -04006554 if (request->fl_type == F_UNLCK) {
6555 if (state != NULL)
6556 return nfs4_proc_unlck(state, cmd, request);
6557 return 0;
6558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559
Trond Myklebustd9531262009-07-21 19:22:38 -04006560 if (state == NULL)
6561 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006562
6563 if ((request->fl_flags & FL_POSIX) &&
6564 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6565 return -ENOLCK;
6566
Trond Myklebust55725512012-04-18 12:48:35 -04006567 /*
6568 * Don't rely on the VFS having checked the file open mode,
6569 * since it won't do this for flock() locks.
6570 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006571 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006572 case F_RDLCK:
6573 if (!(filp->f_mode & FMODE_READ))
6574 return -EBADF;
6575 break;
6576 case F_WRLCK:
6577 if (!(filp->f_mode & FMODE_WRITE))
6578 return -EBADF;
6579 }
6580
Jeff Layton1ea67db2016-09-17 18:17:37 -04006581 status = nfs4_set_lock_state(state, request);
6582 if (status != 0)
6583 return status;
6584
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006585 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586}
6587
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006588int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006589{
6590 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006591 int err;
6592
6593 err = nfs4_set_lock_state(state, fl);
6594 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006595 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006596 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006597 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006598}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006599
Trond Myklebustcf470c32012-03-07 13:49:12 -05006600struct nfs_release_lockowner_data {
6601 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006602 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006603 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006604 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006605 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006606};
6607
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006608static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6609{
6610 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006611 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006612 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6613 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006614 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006615 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006616}
6617
6618static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6619{
6620 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006621 struct nfs_server *server = data->server;
6622
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006623 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006624
6625 switch (task->tk_status) {
6626 case 0:
6627 renew_lease(server, data->timestamp);
6628 break;
6629 case -NFS4ERR_STALE_CLIENTID:
6630 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006631 nfs4_schedule_lease_recovery(server->nfs_client);
6632 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006633 case -NFS4ERR_LEASE_MOVED:
6634 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006635 if (nfs4_async_handle_error(task, server,
6636 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006637 rpc_restart_call_prepare(task);
6638 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006639}
6640
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006641static void nfs4_release_lockowner_release(void *calldata)
6642{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006643 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006644 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006645 kfree(calldata);
6646}
6647
Trond Myklebust17280172012-03-11 13:11:00 -04006648static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006649 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6650 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006651 .rpc_release = nfs4_release_lockowner_release,
6652};
6653
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006654static void
6655nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006656{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006657 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006658 struct rpc_message msg = {
6659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6660 };
6661
6662 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006663 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006664
Trond Myklebustcf470c32012-03-07 13:49:12 -05006665 data = kmalloc(sizeof(*data), GFP_NOFS);
6666 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006667 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006668 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006669 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006670 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6671 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6672 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006673
Trond Myklebustcf470c32012-03-07 13:49:12 -05006674 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006675 msg.rpc_resp = &data->res;
6676 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006677 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006678}
6679
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006680#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6681
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006682static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006683 struct dentry *unused, struct inode *inode,
6684 const char *key, const void *buf,
6685 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006686{
Al Viro59301222016-05-27 10:19:30 -04006687 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006688}
6689
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006690static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006691 struct dentry *unused, struct inode *inode,
6692 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006693{
Al Virob2968212016-04-10 20:48:24 -04006694 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006695}
6696
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006697static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006698{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006699 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006700}
6701
David Quigleyc9bccef2013-05-22 12:50:45 -04006702#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006703
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006704static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006705 struct dentry *unused, struct inode *inode,
6706 const char *key, const void *buf,
6707 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006708{
6709 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006710 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006711
6712 return -EOPNOTSUPP;
6713}
6714
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006715static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006716 struct dentry *unused, struct inode *inode,
6717 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006718{
6719 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006720 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006721 return -EOPNOTSUPP;
6722}
6723
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006724static ssize_t
6725nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006726{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006727 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006728
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006729 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6730 len = security_inode_listsecurity(inode, list, list_len);
6731 if (list_len && len > list_len)
6732 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006733 }
6734 return len;
6735}
6736
6737static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6738 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006739 .get = nfs4_xattr_get_nfs4_label,
6740 .set = nfs4_xattr_set_nfs4_label,
6741};
David Quigleyc9bccef2013-05-22 12:50:45 -04006742
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006743#else
6744
6745static ssize_t
6746nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6747{
6748 return 0;
6749}
6750
6751#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006752
Andy Adamson533eb462011-06-13 18:25:56 -04006753/*
6754 * nfs_fhget will use either the mounted_on_fileid or the fileid
6755 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006756static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6757{
Andy Adamson533eb462011-06-13 18:25:56 -04006758 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6759 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6760 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006761 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006762 return;
6763
6764 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006765 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006766 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6767 fattr->nlink = 2;
6768}
6769
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006770static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6771 const struct qstr *name,
6772 struct nfs4_fs_locations *fs_locations,
6773 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006774{
6775 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006776 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006777 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006778 };
6779 struct nfs4_fs_locations_arg args = {
6780 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006781 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006782 .page = page,
6783 .bitmask = bitmask,
6784 };
Benny Halevy22958462009-04-01 09:22:02 -04006785 struct nfs4_fs_locations_res res = {
6786 .fs_locations = fs_locations,
6787 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006788 struct rpc_message msg = {
6789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6790 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006791 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006792 };
6793 int status;
6794
Harvey Harrison3110ff82008-05-02 13:42:44 -07006795 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006796
6797 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6798 * is not supported */
6799 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6800 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6801 else
6802 bitmask[0] |= FATTR4_WORD0_FILEID;
6803
Trond Myklebustc228fd32007-01-13 02:28:11 -05006804 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006805 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006806 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006807 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006808 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006809 return status;
6810}
6811
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006812int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6813 const struct qstr *name,
6814 struct nfs4_fs_locations *fs_locations,
6815 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006816{
6817 struct nfs4_exception exception = { };
6818 int err;
6819 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006820 err = _nfs4_proc_fs_locations(client, dir, name,
6821 fs_locations, page);
6822 trace_nfs4_get_fs_locations(dir, name, err);
6823 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006824 &exception);
6825 } while (exception.retry);
6826 return err;
6827}
6828
Chuck Leverb03d7352013-10-17 14:12:50 -04006829/*
6830 * This operation also signals the server that this client is
6831 * performing migration recovery. The server can stop returning
6832 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6833 * appended to this compound to identify the client ID which is
6834 * performing recovery.
6835 */
6836static int _nfs40_proc_get_locations(struct inode *inode,
6837 struct nfs4_fs_locations *locations,
6838 struct page *page, struct rpc_cred *cred)
6839{
6840 struct nfs_server *server = NFS_SERVER(inode);
6841 struct rpc_clnt *clnt = server->client;
6842 u32 bitmask[2] = {
6843 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6844 };
6845 struct nfs4_fs_locations_arg args = {
6846 .clientid = server->nfs_client->cl_clientid,
6847 .fh = NFS_FH(inode),
6848 .page = page,
6849 .bitmask = bitmask,
6850 .migration = 1, /* skip LOOKUP */
6851 .renew = 1, /* append RENEW */
6852 };
6853 struct nfs4_fs_locations_res res = {
6854 .fs_locations = locations,
6855 .migration = 1,
6856 .renew = 1,
6857 };
6858 struct rpc_message msg = {
6859 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6860 .rpc_argp = &args,
6861 .rpc_resp = &res,
6862 .rpc_cred = cred,
6863 };
6864 unsigned long now = jiffies;
6865 int status;
6866
6867 nfs_fattr_init(&locations->fattr);
6868 locations->server = server;
6869 locations->nlocations = 0;
6870
6871 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6872 nfs4_set_sequence_privileged(&args.seq_args);
6873 status = nfs4_call_sync_sequence(clnt, server, &msg,
6874 &args.seq_args, &res.seq_res);
6875 if (status)
6876 return status;
6877
6878 renew_lease(server, now);
6879 return 0;
6880}
6881
6882#ifdef CONFIG_NFS_V4_1
6883
6884/*
6885 * This operation also signals the server that this client is
6886 * performing migration recovery. The server can stop asserting
6887 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6888 * performing this operation is identified in the SEQUENCE
6889 * operation in this compound.
6890 *
6891 * When the client supports GETATTR(fs_locations_info), it can
6892 * be plumbed in here.
6893 */
6894static int _nfs41_proc_get_locations(struct inode *inode,
6895 struct nfs4_fs_locations *locations,
6896 struct page *page, struct rpc_cred *cred)
6897{
6898 struct nfs_server *server = NFS_SERVER(inode);
6899 struct rpc_clnt *clnt = server->client;
6900 u32 bitmask[2] = {
6901 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6902 };
6903 struct nfs4_fs_locations_arg args = {
6904 .fh = NFS_FH(inode),
6905 .page = page,
6906 .bitmask = bitmask,
6907 .migration = 1, /* skip LOOKUP */
6908 };
6909 struct nfs4_fs_locations_res res = {
6910 .fs_locations = locations,
6911 .migration = 1,
6912 };
6913 struct rpc_message msg = {
6914 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6915 .rpc_argp = &args,
6916 .rpc_resp = &res,
6917 .rpc_cred = cred,
6918 };
6919 int status;
6920
6921 nfs_fattr_init(&locations->fattr);
6922 locations->server = server;
6923 locations->nlocations = 0;
6924
6925 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6926 nfs4_set_sequence_privileged(&args.seq_args);
6927 status = nfs4_call_sync_sequence(clnt, server, &msg,
6928 &args.seq_args, &res.seq_res);
6929 if (status == NFS4_OK &&
6930 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6931 status = -NFS4ERR_LEASE_MOVED;
6932 return status;
6933}
6934
6935#endif /* CONFIG_NFS_V4_1 */
6936
6937/**
6938 * nfs4_proc_get_locations - discover locations for a migrated FSID
6939 * @inode: inode on FSID that is migrating
6940 * @locations: result of query
6941 * @page: buffer
6942 * @cred: credential to use for this operation
6943 *
6944 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6945 * operation failed, or a negative errno if a local error occurred.
6946 *
6947 * On success, "locations" is filled in, but if the server has
6948 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6949 * asserted.
6950 *
6951 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6952 * from this client that require migration recovery.
6953 */
6954int nfs4_proc_get_locations(struct inode *inode,
6955 struct nfs4_fs_locations *locations,
6956 struct page *page, struct rpc_cred *cred)
6957{
6958 struct nfs_server *server = NFS_SERVER(inode);
6959 struct nfs_client *clp = server->nfs_client;
6960 const struct nfs4_mig_recovery_ops *ops =
6961 clp->cl_mvops->mig_recovery_ops;
6962 struct nfs4_exception exception = { };
6963 int status;
6964
6965 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6966 (unsigned long long)server->fsid.major,
6967 (unsigned long long)server->fsid.minor,
6968 clp->cl_hostname);
6969 nfs_display_fhandle(NFS_FH(inode), __func__);
6970
6971 do {
6972 status = ops->get_locations(inode, locations, page, cred);
6973 if (status != -NFS4ERR_DELAY)
6974 break;
6975 nfs4_handle_exception(server, status, &exception);
6976 } while (exception.retry);
6977 return status;
6978}
6979
Chuck Lever44c99932013-10-17 14:13:30 -04006980/*
6981 * This operation also signals the server that this client is
6982 * performing "lease moved" recovery. The server can stop
6983 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6984 * is appended to this compound to identify the client ID which is
6985 * performing recovery.
6986 */
6987static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6988{
6989 struct nfs_server *server = NFS_SERVER(inode);
6990 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6991 struct rpc_clnt *clnt = server->client;
6992 struct nfs4_fsid_present_arg args = {
6993 .fh = NFS_FH(inode),
6994 .clientid = clp->cl_clientid,
6995 .renew = 1, /* append RENEW */
6996 };
6997 struct nfs4_fsid_present_res res = {
6998 .renew = 1,
6999 };
7000 struct rpc_message msg = {
7001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7002 .rpc_argp = &args,
7003 .rpc_resp = &res,
7004 .rpc_cred = cred,
7005 };
7006 unsigned long now = jiffies;
7007 int status;
7008
7009 res.fh = nfs_alloc_fhandle();
7010 if (res.fh == NULL)
7011 return -ENOMEM;
7012
7013 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7014 nfs4_set_sequence_privileged(&args.seq_args);
7015 status = nfs4_call_sync_sequence(clnt, server, &msg,
7016 &args.seq_args, &res.seq_res);
7017 nfs_free_fhandle(res.fh);
7018 if (status)
7019 return status;
7020
7021 do_renew_lease(clp, now);
7022 return 0;
7023}
7024
7025#ifdef CONFIG_NFS_V4_1
7026
7027/*
7028 * This operation also signals the server that this client is
7029 * performing "lease moved" recovery. The server can stop asserting
7030 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7031 * this operation is identified in the SEQUENCE operation in this
7032 * compound.
7033 */
7034static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7035{
7036 struct nfs_server *server = NFS_SERVER(inode);
7037 struct rpc_clnt *clnt = server->client;
7038 struct nfs4_fsid_present_arg args = {
7039 .fh = NFS_FH(inode),
7040 };
7041 struct nfs4_fsid_present_res res = {
7042 };
7043 struct rpc_message msg = {
7044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7045 .rpc_argp = &args,
7046 .rpc_resp = &res,
7047 .rpc_cred = cred,
7048 };
7049 int status;
7050
7051 res.fh = nfs_alloc_fhandle();
7052 if (res.fh == NULL)
7053 return -ENOMEM;
7054
7055 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7056 nfs4_set_sequence_privileged(&args.seq_args);
7057 status = nfs4_call_sync_sequence(clnt, server, &msg,
7058 &args.seq_args, &res.seq_res);
7059 nfs_free_fhandle(res.fh);
7060 if (status == NFS4_OK &&
7061 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7062 status = -NFS4ERR_LEASE_MOVED;
7063 return status;
7064}
7065
7066#endif /* CONFIG_NFS_V4_1 */
7067
7068/**
7069 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7070 * @inode: inode on FSID to check
7071 * @cred: credential to use for this operation
7072 *
7073 * Server indicates whether the FSID is present, moved, or not
7074 * recognized. This operation is necessary to clear a LEASE_MOVED
7075 * condition for this client ID.
7076 *
7077 * Returns NFS4_OK if the FSID is present on this server,
7078 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7079 * NFS4ERR code if some error occurred on the server, or a
7080 * negative errno if a local failure occurred.
7081 */
7082int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7083{
7084 struct nfs_server *server = NFS_SERVER(inode);
7085 struct nfs_client *clp = server->nfs_client;
7086 const struct nfs4_mig_recovery_ops *ops =
7087 clp->cl_mvops->mig_recovery_ops;
7088 struct nfs4_exception exception = { };
7089 int status;
7090
7091 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7092 (unsigned long long)server->fsid.major,
7093 (unsigned long long)server->fsid.minor,
7094 clp->cl_hostname);
7095 nfs_display_fhandle(NFS_FH(inode), __func__);
7096
7097 do {
7098 status = ops->fsid_present(inode, cred);
7099 if (status != -NFS4ERR_DELAY)
7100 break;
7101 nfs4_handle_exception(server, status, &exception);
7102 } while (exception.retry);
7103 return status;
7104}
7105
Andy Adamson5ec16a82013-08-08 10:57:55 -04007106/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007107 * If 'use_integrity' is true and the state managment nfs_client
7108 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7109 * and the machine credential as per RFC3530bis and RFC5661 Security
7110 * Considerations sections. Otherwise, just use the user cred with the
7111 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007112 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007113static 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 +00007114{
7115 int status;
7116 struct nfs4_secinfo_arg args = {
7117 .dir_fh = NFS_FH(dir),
7118 .name = name,
7119 };
7120 struct nfs4_secinfo_res res = {
7121 .flavors = flavors,
7122 };
7123 struct rpc_message msg = {
7124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7125 .rpc_argp = &args,
7126 .rpc_resp = &res,
7127 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007128 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007129 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007130
7131 if (use_integrity) {
7132 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007133 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7134 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007135 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007136
7137 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007138
7139 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7140 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7141
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007142 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7143 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007144 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007145
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007146 if (cred)
7147 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007148
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007149 return status;
7150}
7151
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007152int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7153 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007154{
7155 struct nfs4_exception exception = { };
7156 int err;
7157 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007158 err = -NFS4ERR_WRONGSEC;
7159
7160 /* try to use integrity protection with machine cred */
7161 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7162 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7163
7164 /*
7165 * if unable to use integrity protection, or SECINFO with
7166 * integrity protection returns NFS4ERR_WRONGSEC (which is
7167 * disallowed by spec, but exists in deployed servers) use
7168 * the current filesystem's rpc_client and the user cred.
7169 */
7170 if (err == -NFS4ERR_WRONGSEC)
7171 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7172
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007173 trace_nfs4_secinfo(dir, name, err);
7174 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007175 &exception);
7176 } while (exception.retry);
7177 return err;
7178}
7179
Andy Adamson557134a2009-04-01 09:21:53 -04007180#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007181/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007182 * Check the exchange flags returned by the server for invalid flags, having
7183 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7184 * DS flags set.
7185 */
7186static int nfs4_check_cl_exchange_flags(u32 flags)
7187{
7188 if (flags & ~EXCHGID4_FLAG_MASK_R)
7189 goto out_inval;
7190 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7191 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7192 goto out_inval;
7193 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7194 goto out_inval;
7195 return NFS_OK;
7196out_inval:
7197 return -NFS4ERR_INVAL;
7198}
7199
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007200static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007201nfs41_same_server_scope(struct nfs41_server_scope *a,
7202 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007203{
7204 if (a->server_scope_sz == b->server_scope_sz &&
7205 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7206 return true;
7207
7208 return false;
7209}
7210
Andy Adamson02a95de2016-02-05 16:08:37 -05007211static void
7212nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7213{
7214}
7215
7216static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7217 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7218};
7219
Andy Adamson357f54d2010-12-14 10:11:57 -05007220/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007221 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007222 *
7223 * The 4.1 client currently uses the same TCP connection for the
7224 * fore and backchannel.
7225 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007226static
7227int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7228 struct rpc_xprt *xprt,
7229 struct nfs_client *clp,
7230 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007231{
7232 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007233 struct nfs41_bind_conn_to_session_args args = {
7234 .client = clp,
7235 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7236 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007237 struct nfs41_bind_conn_to_session_res res;
7238 struct rpc_message msg = {
7239 .rpc_proc =
7240 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007241 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007242 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007243 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007244 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007245 struct rpc_task_setup task_setup_data = {
7246 .rpc_client = clnt,
7247 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007248 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007249 .rpc_message = &msg,
7250 .flags = RPC_TASK_TIMEOUT,
7251 };
7252 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007253
7254 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007255
Trond Myklebust71a097c2015-02-18 09:27:18 -08007256 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7257 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7258 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007259
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007260 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7261 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7262 args.dir = NFS4_CDFC4_FORE;
7263
7264 task = rpc_run_task(&task_setup_data);
7265 if (!IS_ERR(task)) {
7266 status = task->tk_status;
7267 rpc_put_task(task);
7268 } else
7269 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007270 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007271 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007272 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007273 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7274 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7275 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007276 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007277 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007278 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007279 dprintk("NFS: %s: Unexpected direction from server\n",
7280 __func__);
7281 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007282 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007283 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007284 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007285 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7286 __func__);
7287 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007288 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007289 }
7290 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007291out:
7292 dprintk("<-- %s status= %d\n", __func__, status);
7293 return status;
7294}
7295
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007296struct rpc_bind_conn_calldata {
7297 struct nfs_client *clp;
7298 struct rpc_cred *cred;
7299};
7300
7301static int
7302nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7303 struct rpc_xprt *xprt,
7304 void *calldata)
7305{
7306 struct rpc_bind_conn_calldata *p = calldata;
7307
7308 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7309}
7310
7311int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7312{
7313 struct rpc_bind_conn_calldata data = {
7314 .clp = clp,
7315 .cred = cred,
7316 };
7317 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7318 nfs4_proc_bind_conn_to_session_callback, &data);
7319}
7320
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007321/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007322 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7323 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007324 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007325static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7326 .how = SP4_MACH_CRED,
7327 .enforce.u.words = {
7328 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7329 1 << (OP_EXCHANGE_ID - 32) |
7330 1 << (OP_CREATE_SESSION - 32) |
7331 1 << (OP_DESTROY_SESSION - 32) |
7332 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007333 },
7334 .allow.u.words = {
7335 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007336 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007337 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007338 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007339 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007340 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007341 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007342 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007343 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007344 1 << (OP_FREE_STATEID - 32) |
7345 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007346 }
7347};
7348
7349/*
7350 * Select the state protection mode for client `clp' given the server results
7351 * from exchange_id in `sp'.
7352 *
7353 * Returns 0 on success, negative errno otherwise.
7354 */
7355static int nfs4_sp4_select_mode(struct nfs_client *clp,
7356 struct nfs41_state_protection *sp)
7357{
7358 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7359 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7360 1 << (OP_EXCHANGE_ID - 32) |
7361 1 << (OP_CREATE_SESSION - 32) |
7362 1 << (OP_DESTROY_SESSION - 32) |
7363 1 << (OP_DESTROY_CLIENTID - 32)
7364 };
7365 unsigned int i;
7366
7367 if (sp->how == SP4_MACH_CRED) {
7368 /* Print state protect result */
7369 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7370 for (i = 0; i <= LAST_NFS4_OP; i++) {
7371 if (test_bit(i, sp->enforce.u.longs))
7372 dfprintk(MOUNT, " enforce op %d\n", i);
7373 if (test_bit(i, sp->allow.u.longs))
7374 dfprintk(MOUNT, " allow op %d\n", i);
7375 }
7376
7377 /* make sure nothing is on enforce list that isn't supported */
7378 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7379 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7380 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7381 return -EINVAL;
7382 }
7383 }
7384
7385 /*
7386 * Minimal mode - state operations are allowed to use machine
7387 * credential. Note this already happens by default, so the
7388 * client doesn't have to do anything more than the negotiation.
7389 *
7390 * NOTE: we don't care if EXCHANGE_ID is in the list -
7391 * we're already using the machine cred for exchange_id
7392 * and will never use a different cred.
7393 */
7394 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7395 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7396 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7397 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7398 dfprintk(MOUNT, "sp4_mach_cred:\n");
7399 dfprintk(MOUNT, " minimal mode enabled\n");
7400 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7401 } else {
7402 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7403 return -EINVAL;
7404 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007405
7406 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007407 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7408 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007409 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7410 dfprintk(MOUNT, " cleanup mode enabled\n");
7411 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7412 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007413
Andrew Elble99ade3c2015-12-02 09:39:51 -05007414 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7415 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7416 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7417 &clp->cl_sp4_flags);
7418 }
7419
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007420 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7421 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7422 dfprintk(MOUNT, " secinfo mode enabled\n");
7423 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7424 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007425
7426 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7427 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7428 dfprintk(MOUNT, " stateid mode enabled\n");
7429 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7430 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007431
7432 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7433 dfprintk(MOUNT, " write mode enabled\n");
7434 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7435 }
7436
7437 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7438 dfprintk(MOUNT, " commit mode enabled\n");
7439 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7440 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007441 }
7442
7443 return 0;
7444}
7445
Andy Adamson8d89bd72016-09-09 09:22:18 -04007446struct nfs41_exchange_id_data {
7447 struct nfs41_exchange_id_res res;
7448 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007449 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007450 int rpc_status;
7451};
7452
7453static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007454{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007455 struct nfs41_exchange_id_data *cdata =
7456 (struct nfs41_exchange_id_data *)data;
7457 struct nfs_client *clp = cdata->args.client;
7458 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007459
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007460 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007461
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007462 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007463 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007464
7465 if (cdata->xprt && status == 0) {
7466 status = nfs4_detect_session_trunking(clp, &cdata->res,
7467 cdata->xprt);
7468 goto out;
7469 }
7470
Andy Adamson8d89bd72016-09-09 09:22:18 -04007471 if (status == 0)
7472 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007473
Chuck Lever177313f2012-05-21 22:44:58 -04007474 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007475 clp->cl_clientid = cdata->res.clientid;
7476 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007477 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007478 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007479 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007480 &clp->cl_session->session_state);
7481 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007482 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007483
Chuck Leveracdeb692012-05-21 22:46:16 -04007484 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007485 clp->cl_serverowner = cdata->res.server_owner;
7486 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007487
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007488 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007489 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007490 clp->cl_implid = cdata->res.impl_id;
7491 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007492
Chuck Lever177313f2012-05-21 22:44:58 -04007493 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007494 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007495 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007496 dprintk("%s: server_scope mismatch detected\n",
7497 __func__);
7498 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007499 kfree(clp->cl_serverscope);
7500 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007501 }
7502
Chuck Lever177313f2012-05-21 22:44:58 -04007503 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007504 clp->cl_serverscope = cdata->res.server_scope;
7505 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007506 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007507 /* Save the EXCHANGE_ID verifier session trunk tests */
7508 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7509 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007510 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007511out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007512 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007513 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007514}
7515
7516static void nfs4_exchange_id_release(void *data)
7517{
7518 struct nfs41_exchange_id_data *cdata =
7519 (struct nfs41_exchange_id_data *)data;
7520
7521 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007522 if (cdata->xprt) {
7523 xprt_put(cdata->xprt);
7524 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7525 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007526 kfree(cdata->res.impl_id);
7527 kfree(cdata->res.server_scope);
7528 kfree(cdata->res.server_owner);
7529 kfree(cdata);
7530}
7531
7532static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7533 .rpc_call_done = nfs4_exchange_id_done,
7534 .rpc_release = nfs4_exchange_id_release,
7535};
7536
Benny Halevy99fe60d2009-04-01 09:22:29 -04007537/*
7538 * _nfs4_proc_exchange_id()
7539 *
7540 * Wrapper for EXCHANGE_ID operation.
7541 */
7542static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007543 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007544{
7545 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007546 struct rpc_message msg = {
7547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007548 .rpc_cred = cred,
7549 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007550 struct rpc_task_setup task_setup_data = {
7551 .rpc_client = clp->cl_rpcclient,
7552 .callback_ops = &nfs4_exchange_id_call_ops,
7553 .rpc_message = &msg,
7554 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7555 };
7556 struct nfs41_exchange_id_data *calldata;
7557 struct rpc_task *task;
7558 int status = -EIO;
7559
7560 if (!atomic_inc_not_zero(&clp->cl_count))
7561 goto out;
7562
7563 status = -ENOMEM;
7564 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7565 if (!calldata)
7566 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007567
Andy Adamsonad0849a2016-09-09 09:22:28 -04007568 if (!xprt)
7569 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007570
7571 status = nfs4_init_uniform_client_string(clp);
7572 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007573 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007574
7575 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7576 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7577 clp->cl_owner_id);
7578
Andy Adamson8d89bd72016-09-09 09:22:18 -04007579 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7580 GFP_NOFS);
7581 status = -ENOMEM;
7582 if (unlikely(calldata->res.server_owner == NULL))
7583 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007584
Andy Adamson8d89bd72016-09-09 09:22:18 -04007585 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007586 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007587 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007588 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007589
Andy Adamson8d89bd72016-09-09 09:22:18 -04007590 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7591 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007592 goto out_server_scope;
7593
7594 switch (sp4_how) {
7595 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007596 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007597 break;
7598
7599 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007600 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007601 break;
7602
7603 default:
7604 /* unsupported! */
7605 WARN_ON_ONCE(1);
7606 status = -EINVAL;
7607 goto out_impl_id;
7608 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007609 if (xprt) {
7610 calldata->xprt = xprt;
7611 task_setup_data.rpc_xprt = xprt;
7612 task_setup_data.flags =
7613 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7614 calldata->args.verifier = &clp->cl_confirm;
7615 } else {
7616 calldata->args.verifier = &verifier;
7617 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007618 calldata->args.client = clp;
7619#ifdef CONFIG_NFS_V4_1_MIGRATION
7620 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7621 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7622 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7623#else
7624 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7625 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7626#endif
7627 msg.rpc_argp = &calldata->args;
7628 msg.rpc_resp = &calldata->res;
7629 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007630
Andy Adamson8d89bd72016-09-09 09:22:18 -04007631 task = rpc_run_task(&task_setup_data);
7632 if (IS_ERR(task)) {
7633 status = PTR_ERR(task);
7634 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007635 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007636
Andy Adamsonad0849a2016-09-09 09:22:28 -04007637 if (!xprt) {
7638 status = rpc_wait_for_completion_task(task);
7639 if (!status)
7640 status = calldata->rpc_status;
7641 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007642 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007643
Andy Adamson8d89bd72016-09-09 09:22:18 -04007644 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007645out:
Chuck Lever177313f2012-05-21 22:44:58 -04007646 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007647 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007648 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007649 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007650 clp->cl_implid->date.seconds,
7651 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007652 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007653 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007654
7655out_impl_id:
7656 kfree(calldata->res.impl_id);
7657out_server_scope:
7658 kfree(calldata->res.server_scope);
7659out_server_owner:
7660 kfree(calldata->res.server_owner);
7661out_calldata:
7662 kfree(calldata);
7663 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007664}
7665
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007666/*
7667 * nfs4_proc_exchange_id()
7668 *
7669 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7670 *
7671 * Since the clientid has expired, all compounds using sessions
7672 * associated with the stale clientid will be returning
7673 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7674 * be in some phase of session reset.
7675 *
7676 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7677 */
7678int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7679{
7680 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7681 int status;
7682
7683 /* try SP4_MACH_CRED if krb5i/p */
7684 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7685 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007686 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007687 if (!status)
7688 return 0;
7689 }
7690
7691 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007692 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007693}
7694
Andy Adamson04fa2c62016-09-09 09:22:29 -04007695/**
7696 * nfs4_test_session_trunk
7697 *
7698 * This is an add_xprt_test() test function called from
7699 * rpc_clnt_setup_test_and_add_xprt.
7700 *
7701 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7702 * and is dereferrenced in nfs4_exchange_id_release
7703 *
7704 * Upon success, add the new transport to the rpc_clnt
7705 *
7706 * @clnt: struct rpc_clnt to get new transport
7707 * @xprt: the rpc_xprt to test
7708 * @data: call data for _nfs4_proc_exchange_id.
7709 */
7710int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7711 void *data)
7712{
7713 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7714 u32 sp4_how;
7715
7716 dprintk("--> %s try %s\n", __func__,
7717 xprt->address_strings[RPC_DISPLAY_ADDR]);
7718
7719 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7720
7721 /* Test connection for session trunking. Async exchange_id call */
7722 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7723}
7724EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7725
Trond Myklebust66245532012-05-25 17:18:09 -04007726static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7727 struct rpc_cred *cred)
7728{
7729 struct rpc_message msg = {
7730 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7731 .rpc_argp = clp,
7732 .rpc_cred = cred,
7733 };
7734 int status;
7735
7736 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007737 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007738 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007739 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007740 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7741 return status;
7742}
7743
7744static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7745 struct rpc_cred *cred)
7746{
7747 unsigned int loop;
7748 int ret;
7749
7750 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7751 ret = _nfs4_proc_destroy_clientid(clp, cred);
7752 switch (ret) {
7753 case -NFS4ERR_DELAY:
7754 case -NFS4ERR_CLIENTID_BUSY:
7755 ssleep(1);
7756 break;
7757 default:
7758 return ret;
7759 }
7760 }
7761 return 0;
7762}
7763
7764int nfs4_destroy_clientid(struct nfs_client *clp)
7765{
7766 struct rpc_cred *cred;
7767 int ret = 0;
7768
7769 if (clp->cl_mvops->minor_version < 1)
7770 goto out;
7771 if (clp->cl_exchange_flags == 0)
7772 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007773 if (clp->cl_preserve_clid)
7774 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007775 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007776 ret = nfs4_proc_destroy_clientid(clp, cred);
7777 if (cred)
7778 put_rpccred(cred);
7779 switch (ret) {
7780 case 0:
7781 case -NFS4ERR_STALE_CLIENTID:
7782 clp->cl_exchange_flags = 0;
7783 }
7784out:
7785 return ret;
7786}
7787
Andy Adamson2050f0c2009-04-01 09:22:30 -04007788struct nfs4_get_lease_time_data {
7789 struct nfs4_get_lease_time_args *args;
7790 struct nfs4_get_lease_time_res *res;
7791 struct nfs_client *clp;
7792};
7793
7794static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7795 void *calldata)
7796{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007797 struct nfs4_get_lease_time_data *data =
7798 (struct nfs4_get_lease_time_data *)calldata;
7799
7800 dprintk("--> %s\n", __func__);
7801 /* just setup sequence, do not trigger session recovery
7802 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007803 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007804 &data->args->la_seq_args,
7805 &data->res->lr_seq_res,
7806 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007807 dprintk("<-- %s\n", __func__);
7808}
7809
7810/*
7811 * Called from nfs4_state_manager thread for session setup, so don't recover
7812 * from sequence operation or clientid errors.
7813 */
7814static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7815{
7816 struct nfs4_get_lease_time_data *data =
7817 (struct nfs4_get_lease_time_data *)calldata;
7818
7819 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007820 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7821 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007822 switch (task->tk_status) {
7823 case -NFS4ERR_DELAY:
7824 case -NFS4ERR_GRACE:
7825 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7826 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7827 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007828 /* fall through */
7829 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007830 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007831 return;
7832 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007833 dprintk("<-- %s\n", __func__);
7834}
7835
Trond Myklebust17280172012-03-11 13:11:00 -04007836static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007837 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7838 .rpc_call_done = nfs4_get_lease_time_done,
7839};
7840
7841int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7842{
7843 struct rpc_task *task;
7844 struct nfs4_get_lease_time_args args;
7845 struct nfs4_get_lease_time_res res = {
7846 .lr_fsinfo = fsinfo,
7847 };
7848 struct nfs4_get_lease_time_data data = {
7849 .args = &args,
7850 .res = &res,
7851 .clp = clp,
7852 };
7853 struct rpc_message msg = {
7854 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7855 .rpc_argp = &args,
7856 .rpc_resp = &res,
7857 };
7858 struct rpc_task_setup task_setup = {
7859 .rpc_client = clp->cl_rpcclient,
7860 .rpc_message = &msg,
7861 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007862 .callback_data = &data,
7863 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007864 };
7865 int status;
7866
Chuck Levera9c92d62013-08-09 12:48:18 -04007867 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007868 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007869 dprintk("--> %s\n", __func__);
7870 task = rpc_run_task(&task_setup);
7871
7872 if (IS_ERR(task))
7873 status = PTR_ERR(task);
7874 else {
7875 status = task->tk_status;
7876 rpc_put_task(task);
7877 }
7878 dprintk("<-- %s return %d\n", __func__, status);
7879
7880 return status;
7881}
7882
Andy Adamsonfc931582009-04-01 09:22:31 -04007883/*
7884 * Initialize the values to be used by the client in CREATE_SESSION
7885 * If nfs4_init_session set the fore channel request and response sizes,
7886 * use them.
7887 *
7888 * Set the back channel max_resp_sz_cached to zero to force the client to
7889 * always set csa_cachethis to FALSE because the current implementation
7890 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7891 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007892static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7893 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007894{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007895 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007896 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007897
Andy Adamson18aad3d2013-06-26 12:21:49 -04007898 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7899 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7900
Andy Adamsonfc931582009-04-01 09:22:31 -04007901 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007902 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7903 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007904 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007905 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007906
7907 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007908 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007909 __func__,
7910 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007911 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007912
7913 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007914 args->bc_attrs.max_rqst_sz = max_bc_payload;
7915 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007916 args->bc_attrs.max_resp_sz_cached = 0;
7917 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007918 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007919
7920 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7921 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7922 __func__,
7923 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7924 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7925 args->bc_attrs.max_reqs);
7926}
7927
Trond Myklebust79969dd2015-02-18 11:30:18 -08007928static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7929 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007930{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007931 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007932 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007933
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007934 if (rcvd->max_resp_sz > sent->max_resp_sz)
7935 return -EINVAL;
7936 /*
7937 * Our requested max_ops is the minimum we need; we're not
7938 * prepared to break up compounds into smaller pieces than that.
7939 * So, no point even trying to continue if the server won't
7940 * cooperate:
7941 */
7942 if (rcvd->max_ops < sent->max_ops)
7943 return -EINVAL;
7944 if (rcvd->max_reqs == 0)
7945 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007946 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7947 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007948 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007949}
7950
Trond Myklebust79969dd2015-02-18 11:30:18 -08007951static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7952 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007953{
7954 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007955 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007956
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007957 if (!(res->flags & SESSION4_BACK_CHAN))
7958 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007959 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7960 return -EINVAL;
7961 if (rcvd->max_resp_sz < sent->max_resp_sz)
7962 return -EINVAL;
7963 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7964 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007965 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007966 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007967 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007968 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007969out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007970 return 0;
7971}
Andy Adamson8d353012009-04-01 09:22:32 -04007972
Andy Adamson8d353012009-04-01 09:22:32 -04007973static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007974 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007975{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007976 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007977
Trond Myklebust79969dd2015-02-18 11:30:18 -08007978 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007979 if (ret)
7980 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007981 return nfs4_verify_back_channel_attrs(args, res);
7982}
7983
7984static void nfs4_update_session(struct nfs4_session *session,
7985 struct nfs41_create_session_res *res)
7986{
7987 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007988 /* Mark client id and session as being confirmed */
7989 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7990 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007991 session->flags = res->flags;
7992 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007993 if (res->flags & SESSION4_BACK_CHAN)
7994 memcpy(&session->bc_attrs, &res->bc_attrs,
7995 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007996}
7997
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007998static int _nfs4_proc_create_session(struct nfs_client *clp,
7999 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008000{
8001 struct nfs4_session *session = clp->cl_session;
8002 struct nfs41_create_session_args args = {
8003 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008004 .clientid = clp->cl_clientid,
8005 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008006 .cb_program = NFS4_CALLBACK,
8007 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008008 struct nfs41_create_session_res res;
8009
Andy Adamsonfc931582009-04-01 09:22:31 -04008010 struct rpc_message msg = {
8011 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8012 .rpc_argp = &args,
8013 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008014 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008015 };
8016 int status;
8017
Chuck Lever6b26cc82016-05-02 14:40:40 -04008018 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008019 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008020
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008021 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008022 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008023
Trond Myklebustb519d402016-09-11 14:50:01 -04008024 switch (status) {
8025 case -NFS4ERR_STALE_CLIENTID:
8026 case -NFS4ERR_DELAY:
8027 case -ETIMEDOUT:
8028 case -EACCES:
8029 case -EAGAIN:
8030 goto out;
8031 };
8032
8033 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008034 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008035 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008036 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008037 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008038 if (status)
8039 goto out;
8040 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008041 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008042out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008043 return status;
8044}
8045
8046/*
8047 * Issues a CREATE_SESSION operation to the server.
8048 * It is the responsibility of the caller to verify the session is
8049 * expired before calling this routine.
8050 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008051int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008052{
8053 int status;
8054 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008055 struct nfs4_session *session = clp->cl_session;
8056
8057 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8058
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008059 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008060 if (status)
8061 goto out;
8062
Andy Adamsonaacd5532011-11-09 13:58:21 -05008063 /* Init or reset the session slot tables */
8064 status = nfs4_setup_session_slot_tables(session);
8065 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008066 if (status)
8067 goto out;
8068
8069 ptr = (unsigned *)&session->sess_id.data[0];
8070 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8071 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008072out:
8073 dprintk("<-- %s\n", __func__);
8074 return status;
8075}
8076
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008077/*
8078 * Issue the over-the-wire RPC DESTROY_SESSION.
8079 * The caller must serialize access to this routine.
8080 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008081int nfs4_proc_destroy_session(struct nfs4_session *session,
8082 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008083{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008084 struct rpc_message msg = {
8085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8086 .rpc_argp = session,
8087 .rpc_cred = cred,
8088 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008089 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008090
8091 dprintk("--> nfs4_proc_destroy_session\n");
8092
8093 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008094 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8095 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008096
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008097 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008098 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008099
8100 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008101 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008102 "Session has been destroyed regardless...\n", status);
8103
8104 dprintk("<-- nfs4_proc_destroy_session\n");
8105 return status;
8106}
8107
Trond Myklebust7b38c362012-05-23 13:23:31 -04008108/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008109 * Renew the cl_session lease.
8110 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008111struct nfs4_sequence_data {
8112 struct nfs_client *clp;
8113 struct nfs4_sequence_args args;
8114 struct nfs4_sequence_res res;
8115};
8116
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008117static void nfs41_sequence_release(void *data)
8118{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008119 struct nfs4_sequence_data *calldata = data;
8120 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008121
Alexandros Batsakis71358402010-02-05 03:45:05 -08008122 if (atomic_read(&clp->cl_count) > 1)
8123 nfs4_schedule_state_renewal(clp);
8124 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008125 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008126}
8127
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008128static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8129{
8130 switch(task->tk_status) {
8131 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008132 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8133 return -EAGAIN;
8134 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008135 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008136 }
8137 return 0;
8138}
8139
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008140static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008141{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008142 struct nfs4_sequence_data *calldata = data;
8143 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008144
Trond Myklebust14516c32010-07-31 14:29:06 -04008145 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8146 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008147
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008148 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008149 if (task->tk_status < 0) {
8150 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008151 if (atomic_read(&clp->cl_count) == 1)
8152 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008153
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008154 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8155 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008156 return;
8157 }
8158 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008159 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008160out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008161 dprintk("<-- %s\n", __func__);
8162}
8163
8164static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8165{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008166 struct nfs4_sequence_data *calldata = data;
8167 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008168 struct nfs4_sequence_args *args;
8169 struct nfs4_sequence_res *res;
8170
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008171 args = task->tk_msg.rpc_argp;
8172 res = task->tk_msg.rpc_resp;
8173
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008174 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008175}
8176
8177static const struct rpc_call_ops nfs41_sequence_ops = {
8178 .rpc_call_done = nfs41_sequence_call_done,
8179 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008180 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008181};
8182
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008183static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8184 struct rpc_cred *cred,
8185 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008186{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008187 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008188 struct rpc_message msg = {
8189 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8190 .rpc_cred = cred,
8191 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008192 struct rpc_task_setup task_setup_data = {
8193 .rpc_client = clp->cl_rpcclient,
8194 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008195 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008196 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008197 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008198
Alexandros Batsakis71358402010-02-05 03:45:05 -08008199 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008200 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008201 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008202 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008203 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008204 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008205 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008206 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008207 if (is_privileged)
8208 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008209 msg.rpc_argp = &calldata->args;
8210 msg.rpc_resp = &calldata->res;
8211 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008212 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008213
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008214 return rpc_run_task(&task_setup_data);
8215}
8216
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008217static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008218{
8219 struct rpc_task *task;
8220 int ret = 0;
8221
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008222 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008223 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008224 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008225 if (IS_ERR(task))
8226 ret = PTR_ERR(task);
8227 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008228 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008229 dprintk("<-- %s status=%d\n", __func__, ret);
8230 return ret;
8231}
8232
8233static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8234{
8235 struct rpc_task *task;
8236 int ret;
8237
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008238 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008239 if (IS_ERR(task)) {
8240 ret = PTR_ERR(task);
8241 goto out;
8242 }
8243 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008244 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008245 ret = task->tk_status;
8246 rpc_put_task(task);
8247out:
8248 dprintk("<-- %s status=%d\n", __func__, ret);
8249 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008250}
8251
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008252struct nfs4_reclaim_complete_data {
8253 struct nfs_client *clp;
8254 struct nfs41_reclaim_complete_args arg;
8255 struct nfs41_reclaim_complete_res res;
8256};
8257
8258static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8259{
8260 struct nfs4_reclaim_complete_data *calldata = data;
8261
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008262 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008263 &calldata->arg.seq_args,
8264 &calldata->res.seq_res,
8265 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008266}
8267
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008268static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8269{
8270 switch(task->tk_status) {
8271 case 0:
8272 case -NFS4ERR_COMPLETE_ALREADY:
8273 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8274 break;
8275 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008276 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008277 /* fall through */
8278 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008279 return -EAGAIN;
8280 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008281 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008282 }
8283 return 0;
8284}
8285
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008286static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8287{
8288 struct nfs4_reclaim_complete_data *calldata = data;
8289 struct nfs_client *clp = calldata->clp;
8290 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8291
8292 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008293 if (!nfs41_sequence_done(task, res))
8294 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008295
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008296 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008297 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8298 rpc_restart_call_prepare(task);
8299 return;
8300 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008301 dprintk("<-- %s\n", __func__);
8302}
8303
8304static void nfs4_free_reclaim_complete_data(void *data)
8305{
8306 struct nfs4_reclaim_complete_data *calldata = data;
8307
8308 kfree(calldata);
8309}
8310
8311static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8312 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8313 .rpc_call_done = nfs4_reclaim_complete_done,
8314 .rpc_release = nfs4_free_reclaim_complete_data,
8315};
8316
8317/*
8318 * Issue a global reclaim complete.
8319 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008320static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8321 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008322{
8323 struct nfs4_reclaim_complete_data *calldata;
8324 struct rpc_task *task;
8325 struct rpc_message msg = {
8326 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008327 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008328 };
8329 struct rpc_task_setup task_setup_data = {
8330 .rpc_client = clp->cl_rpcclient,
8331 .rpc_message = &msg,
8332 .callback_ops = &nfs4_reclaim_complete_call_ops,
8333 .flags = RPC_TASK_ASYNC,
8334 };
8335 int status = -ENOMEM;
8336
8337 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008338 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008339 if (calldata == NULL)
8340 goto out;
8341 calldata->clp = clp;
8342 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008343
Chuck Levera9c92d62013-08-09 12:48:18 -04008344 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008345 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008346 msg.rpc_argp = &calldata->arg;
8347 msg.rpc_resp = &calldata->res;
8348 task_setup_data.callback_data = calldata;
8349 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008350 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008351 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008352 goto out;
8353 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008354 status = nfs4_wait_for_completion_rpc_task(task);
8355 if (status == 0)
8356 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008357 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008358 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008359out:
8360 dprintk("<-- %s status=%d\n", __func__, status);
8361 return status;
8362}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008363
8364static void
8365nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8366{
8367 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008368 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008369
8370 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008371 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008372 &lgp->res.seq_res, task);
8373 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008374}
8375
8376static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8377{
8378 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008379
8380 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008381 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008382 dprintk("<-- %s\n", __func__);
8383}
8384
8385static int
8386nfs4_layoutget_handle_exception(struct rpc_task *task,
8387 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8388{
Trond Myklebustee314c22012-10-01 17:25:48 -07008389 struct inode *inode = lgp->args.inode;
8390 struct nfs_server *server = NFS_SERVER(inode);
8391 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008392 int nfs4err = task->tk_status;
8393 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008394 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008395
Boaz Harroshed7e5422014-01-22 20:34:54 +02008396 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008397
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008398 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008399 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008400 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008401
8402 /*
8403 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8404 * on the file. set tk_status to -ENODATA to tell upper layer to
8405 * retry go inband.
8406 */
8407 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008408 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008409 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008410 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008411 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8412 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8413 */
8414 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008415 status = -EOVERFLOW;
8416 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008417 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008418 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008419 * (or clients) writing to the same RAID stripe except when
8420 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008421 *
8422 * Treat it like we would RECALLCONFLICT -- we retry for a little
8423 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008424 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008425 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008426 if (lgp->args.minlength == 0) {
8427 status = -EOVERFLOW;
8428 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008429 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008430 status = -EBUSY;
8431 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008432 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008433 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008434 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008435 case -NFS4ERR_DELEG_REVOKED:
8436 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008437 case -NFS4ERR_EXPIRED:
8438 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008439 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008440 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008441 lo = NFS_I(inode)->layout;
8442 /* If the open stateid was bad, then recover it. */
8443 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8444 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008445 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008446 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008447 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008448 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008449 break;
8450 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008451
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008452 /*
8453 * Mark the bad layout state as invalid, then retry
8454 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008455 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008456 spin_unlock(&inode->i_lock);
8457 pnfs_free_lseg_list(&head);
8458 status = -EAGAIN;
8459 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008460 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008461
Trond Myklebust8ac09252017-01-23 22:44:12 -05008462 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008463 err = nfs4_handle_exception(server, nfs4err, exception);
8464 if (!status) {
8465 if (exception->retry)
8466 status = -EAGAIN;
8467 else
8468 status = err;
8469 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008470out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008471 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008472 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008473}
8474
Idan Kedar85541162012-08-02 11:47:10 +03008475static size_t max_response_pages(struct nfs_server *server)
8476{
8477 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8478 return nfs_page_array_len(0, max_resp_sz);
8479}
8480
8481static void nfs4_free_pages(struct page **pages, size_t size)
8482{
8483 int i;
8484
8485 if (!pages)
8486 return;
8487
8488 for (i = 0; i < size; i++) {
8489 if (!pages[i])
8490 break;
8491 __free_page(pages[i]);
8492 }
8493 kfree(pages);
8494}
8495
8496static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8497{
8498 struct page **pages;
8499 int i;
8500
8501 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8502 if (!pages) {
8503 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8504 return NULL;
8505 }
8506
8507 for (i = 0; i < size; i++) {
8508 pages[i] = alloc_page(gfp_flags);
8509 if (!pages[i]) {
8510 dprintk("%s: failed to allocate page\n", __func__);
8511 nfs4_free_pages(pages, size);
8512 return NULL;
8513 }
8514 }
8515
8516 return pages;
8517}
8518
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008519static void nfs4_layoutget_release(void *calldata)
8520{
8521 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008522 struct inode *inode = lgp->args.inode;
8523 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008524 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008525
8526 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008527 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008528 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008529 put_nfs_open_context(lgp->args.ctx);
8530 kfree(calldata);
8531 dprintk("<-- %s\n", __func__);
8532}
8533
8534static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8535 .rpc_call_prepare = nfs4_layoutget_prepare,
8536 .rpc_call_done = nfs4_layoutget_done,
8537 .rpc_release = nfs4_layoutget_release,
8538};
8539
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008540struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008541nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008542{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008543 struct inode *inode = lgp->args.inode;
8544 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008545 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008546 struct rpc_task *task;
8547 struct rpc_message msg = {
8548 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8549 .rpc_argp = &lgp->args,
8550 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008551 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008552 };
8553 struct rpc_task_setup task_setup_data = {
8554 .rpc_client = server->client,
8555 .rpc_message = &msg,
8556 .callback_ops = &nfs4_layoutget_call_ops,
8557 .callback_data = lgp,
8558 .flags = RPC_TASK_ASYNC,
8559 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008560 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008561 struct nfs4_exception exception = {
8562 .inode = inode,
8563 .timeout = *timeout,
8564 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008565 int status = 0;
8566
8567 dprintk("--> %s\n", __func__);
8568
Peng Tao4bd5a982014-11-17 11:05:17 +08008569 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8570 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8571
Idan Kedar85541162012-08-02 11:47:10 +03008572 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8573 if (!lgp->args.layout.pages) {
8574 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008575 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008576 }
8577 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8578
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008579 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008580 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008581 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008582
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008583 task = rpc_run_task(&task_setup_data);
8584 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008585 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008586 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008587 if (status == 0) {
8588 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8589 *timeout = exception.timeout;
8590 }
8591
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008592 trace_nfs4_layoutget(lgp->args.ctx,
8593 &lgp->args.range,
8594 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008595 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008596 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008597
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008598 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8599 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008600 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008601 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008602 rpc_put_task(task);
8603 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008604 if (status)
8605 return ERR_PTR(status);
8606 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008607}
8608
Benny Halevycbe82602011-05-22 19:52:37 +03008609static void
8610nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8611{
8612 struct nfs4_layoutreturn *lrp = calldata;
8613
8614 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008615 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008616 &lrp->args.seq_args,
8617 &lrp->res.seq_res,
8618 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008619}
8620
8621static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8622{
8623 struct nfs4_layoutreturn *lrp = calldata;
8624 struct nfs_server *server;
8625
8626 dprintk("--> %s\n", __func__);
8627
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008628 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008629 return;
8630
8631 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008632 switch (task->tk_status) {
8633 default:
8634 task->tk_status = 0;
8635 case 0:
8636 break;
8637 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008638 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008639 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008640 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008641 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008642 return;
8643 }
Benny Halevycbe82602011-05-22 19:52:37 +03008644 dprintk("<-- %s\n", __func__);
8645}
8646
8647static void nfs4_layoutreturn_release(void *calldata)
8648{
8649 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008650 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008651
8652 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008653 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008654 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008655 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008656 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8657 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008658 pnfs_put_layout_hdr(lrp->args.layout);
8659 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008660 kfree(calldata);
8661 dprintk("<-- %s\n", __func__);
8662}
8663
8664static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8665 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8666 .rpc_call_done = nfs4_layoutreturn_done,
8667 .rpc_release = nfs4_layoutreturn_release,
8668};
8669
Peng Tao6c166052014-11-17 09:30:40 +08008670int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008671{
8672 struct rpc_task *task;
8673 struct rpc_message msg = {
8674 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8675 .rpc_argp = &lrp->args,
8676 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008677 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008678 };
8679 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008680 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008681 .rpc_message = &msg,
8682 .callback_ops = &nfs4_layoutreturn_call_ops,
8683 .callback_data = lrp,
8684 };
Peng Tao6c166052014-11-17 09:30:40 +08008685 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008686
Andrew Elble99ade3c2015-12-02 09:39:51 -05008687 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8688 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8689 &task_setup_data.rpc_client, &msg);
8690
Benny Halevycbe82602011-05-22 19:52:37 +03008691 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008692 if (!sync) {
8693 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8694 if (!lrp->inode) {
8695 nfs4_layoutreturn_release(lrp);
8696 return -EAGAIN;
8697 }
8698 task_setup_data.flags |= RPC_TASK_ASYNC;
8699 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008700 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008701 task = rpc_run_task(&task_setup_data);
8702 if (IS_ERR(task))
8703 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008704 if (sync)
8705 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008706 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008707 dprintk("<-- %s status=%d\n", __func__, status);
8708 rpc_put_task(task);
8709 return status;
8710}
8711
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008712static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008713_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8714 struct pnfs_device *pdev,
8715 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008716{
8717 struct nfs4_getdeviceinfo_args args = {
8718 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008719 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8720 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008721 };
8722 struct nfs4_getdeviceinfo_res res = {
8723 .pdev = pdev,
8724 };
8725 struct rpc_message msg = {
8726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8727 .rpc_argp = &args,
8728 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008729 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008730 };
8731 int status;
8732
8733 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008734 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008735 if (res.notification & ~args.notify_types)
8736 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008737 if (res.notification != args.notify_types)
8738 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008739
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008740 dprintk("<-- %s status=%d\n", __func__, status);
8741
8742 return status;
8743}
8744
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008745int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8746 struct pnfs_device *pdev,
8747 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008748{
8749 struct nfs4_exception exception = { };
8750 int err;
8751
8752 do {
8753 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008754 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008755 &exception);
8756 } while (exception.retry);
8757 return err;
8758}
8759EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8760
Andy Adamson863a3c62011-03-23 13:27:54 +00008761static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8762{
8763 struct nfs4_layoutcommit_data *data = calldata;
8764 struct nfs_server *server = NFS_SERVER(data->args.inode);
8765
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008766 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008767 &data->args.seq_args,
8768 &data->res.seq_res,
8769 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008770}
8771
8772static void
8773nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8774{
8775 struct nfs4_layoutcommit_data *data = calldata;
8776 struct nfs_server *server = NFS_SERVER(data->args.inode);
8777
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008778 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008779 return;
8780
8781 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008782 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8783 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8784 case -NFS4ERR_BADLAYOUT: /* no layout */
8785 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008786 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008787 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008788 break;
8789 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008790 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008791 rpc_restart_call_prepare(task);
8792 return;
8793 }
8794 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008795}
8796
8797static void nfs4_layoutcommit_release(void *calldata)
8798{
8799 struct nfs4_layoutcommit_data *data = calldata;
8800
Andy Adamsondb29c082011-07-30 20:52:38 -04008801 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008802 nfs_post_op_update_inode_force_wcc(data->args.inode,
8803 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008804 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008805 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008806 kfree(data);
8807}
8808
8809static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8810 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8811 .rpc_call_done = nfs4_layoutcommit_done,
8812 .rpc_release = nfs4_layoutcommit_release,
8813};
8814
8815int
Andy Adamsonef311532011-03-12 02:58:10 -05008816nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008817{
8818 struct rpc_message msg = {
8819 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8820 .rpc_argp = &data->args,
8821 .rpc_resp = &data->res,
8822 .rpc_cred = data->cred,
8823 };
8824 struct rpc_task_setup task_setup_data = {
8825 .task = &data->task,
8826 .rpc_client = NFS_CLIENT(data->args.inode),
8827 .rpc_message = &msg,
8828 .callback_ops = &nfs4_layoutcommit_ops,
8829 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008830 };
8831 struct rpc_task *task;
8832 int status = 0;
8833
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008834 dprintk("NFS: initiating layoutcommit call. sync %d "
8835 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008836 data->args.lastbytewritten,
8837 data->args.inode->i_ino);
8838
Trond Myklebust472e2592015-02-05 16:50:30 -05008839 if (!sync) {
8840 data->inode = nfs_igrab_and_active(data->args.inode);
8841 if (data->inode == NULL) {
8842 nfs4_layoutcommit_release(data);
8843 return -EAGAIN;
8844 }
8845 task_setup_data.flags = RPC_TASK_ASYNC;
8846 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008847 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008848 task = rpc_run_task(&task_setup_data);
8849 if (IS_ERR(task))
8850 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008851 if (sync)
8852 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008853 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008854 dprintk("%s: status %d\n", __func__, status);
8855 rpc_put_task(task);
8856 return status;
8857}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008858
Andy Adamson97431202013-08-08 10:57:56 -04008859/**
8860 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8861 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8862 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008863static int
8864_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008865 struct nfs_fsinfo *info,
8866 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008867{
8868 struct nfs41_secinfo_no_name_args args = {
8869 .style = SECINFO_STYLE_CURRENT_FH,
8870 };
8871 struct nfs4_secinfo_res res = {
8872 .flavors = flavors,
8873 };
8874 struct rpc_message msg = {
8875 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8876 .rpc_argp = &args,
8877 .rpc_resp = &res,
8878 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008879 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008880 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008881 int status;
8882
8883 if (use_integrity) {
8884 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008885 cred = nfs4_get_clid_cred(server->nfs_client);
8886 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008887 }
8888
8889 dprintk("--> %s\n", __func__);
8890 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8891 &res.seq_res, 0);
8892 dprintk("<-- %s status=%d\n", __func__, status);
8893
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008894 if (cred)
8895 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008896
8897 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008898}
8899
8900static int
8901nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8902 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8903{
8904 struct nfs4_exception exception = { };
8905 int err;
8906 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008907 /* first try using integrity protection */
8908 err = -NFS4ERR_WRONGSEC;
8909
8910 /* try to use integrity protection with machine cred */
8911 if (_nfs4_is_integrity_protected(server->nfs_client))
8912 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8913 flavors, true);
8914
8915 /*
8916 * if unable to use integrity protection, or SECINFO with
8917 * integrity protection returns NFS4ERR_WRONGSEC (which is
8918 * disallowed by spec, but exists in deployed servers) use
8919 * the current filesystem's rpc_client and the user cred.
8920 */
8921 if (err == -NFS4ERR_WRONGSEC)
8922 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8923 flavors, false);
8924
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008925 switch (err) {
8926 case 0:
8927 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008928 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008929 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008930 default:
8931 err = nfs4_handle_exception(server, err, &exception);
8932 }
8933 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008934out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008935 return err;
8936}
8937
8938static int
8939nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8940 struct nfs_fsinfo *info)
8941{
8942 int err;
8943 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008944 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008945 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008946 struct nfs4_secinfo4 *secinfo;
8947 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008948
8949 page = alloc_page(GFP_KERNEL);
8950 if (!page) {
8951 err = -ENOMEM;
8952 goto out;
8953 }
8954
8955 flavors = page_address(page);
8956 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8957
8958 /*
8959 * Fall back on "guess and check" method if
8960 * the server doesn't support SECINFO_NO_NAME
8961 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008962 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008963 err = nfs4_find_root_sec(server, fhandle, info);
8964 goto out_freepage;
8965 }
8966 if (err)
8967 goto out_freepage;
8968
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008969 for (i = 0; i < flavors->num_flavors; i++) {
8970 secinfo = &flavors->flavors[i];
8971
8972 switch (secinfo->flavor) {
8973 case RPC_AUTH_NULL:
8974 case RPC_AUTH_UNIX:
8975 case RPC_AUTH_GSS:
8976 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8977 &secinfo->flavor_info);
8978 break;
8979 default:
8980 flavor = RPC_AUTH_MAXFLAVOR;
8981 break;
8982 }
8983
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008984 if (!nfs_auth_info_match(&server->auth_info, flavor))
8985 flavor = RPC_AUTH_MAXFLAVOR;
8986
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008987 if (flavor != RPC_AUTH_MAXFLAVOR) {
8988 err = nfs4_lookup_root_sec(server, fhandle,
8989 info, flavor);
8990 if (!err)
8991 break;
8992 }
8993 }
8994
8995 if (flavor == RPC_AUTH_MAXFLAVOR)
8996 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008997
8998out_freepage:
8999 put_page(page);
9000 if (err == -EACCES)
9001 return -EPERM;
9002out:
9003 return err;
9004}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009005
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009006static int _nfs41_test_stateid(struct nfs_server *server,
9007 nfs4_stateid *stateid,
9008 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009009{
9010 int status;
9011 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009012 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009013 };
9014 struct nfs41_test_stateid_res res;
9015 struct rpc_message msg = {
9016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9017 .rpc_argp = &args,
9018 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009019 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009020 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009021 struct rpc_clnt *rpc_client = server->client;
9022
9023 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9024 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009025
Chuck Lever38527b12012-07-11 16:30:23 -04009026 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009027 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009028 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009029 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009030 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009031 if (status != NFS_OK) {
9032 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009033 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009034 }
9035 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009036 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009037}
9038
Trond Myklebust43912bb2016-09-22 13:38:56 -04009039static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9040 int err, struct nfs4_exception *exception)
9041{
9042 exception->retry = 0;
9043 switch(err) {
9044 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009045 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009046 nfs4_handle_exception(server, err, exception);
9047 break;
9048 case -NFS4ERR_BADSESSION:
9049 case -NFS4ERR_BADSLOT:
9050 case -NFS4ERR_BAD_HIGH_SLOT:
9051 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9052 case -NFS4ERR_DEADSESSION:
9053 nfs4_do_handle_exception(server, err, exception);
9054 }
9055}
9056
Chuck Lever38527b12012-07-11 16:30:23 -04009057/**
9058 * nfs41_test_stateid - perform a TEST_STATEID operation
9059 *
9060 * @server: server / transport on which to perform the operation
9061 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009062 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009063 *
9064 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9065 * Otherwise a negative NFS4ERR value is returned if the operation
9066 * failed or the state ID is not currently valid.
9067 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009068static int nfs41_test_stateid(struct nfs_server *server,
9069 nfs4_stateid *stateid,
9070 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009071{
9072 struct nfs4_exception exception = { };
9073 int err;
9074 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009075 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009076 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009077 } while (exception.retry);
9078 return err;
9079}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009080
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009081struct nfs_free_stateid_data {
9082 struct nfs_server *server;
9083 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009084 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009085};
9086
9087static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9088{
9089 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009090 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009091 &data->args.seq_args,
9092 &data->res.seq_res,
9093 task);
9094}
9095
9096static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9097{
9098 struct nfs_free_stateid_data *data = calldata;
9099
9100 nfs41_sequence_done(task, &data->res.seq_res);
9101
9102 switch (task->tk_status) {
9103 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009104 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009105 rpc_restart_call_prepare(task);
9106 }
9107}
9108
9109static void nfs41_free_stateid_release(void *calldata)
9110{
9111 kfree(calldata);
9112}
9113
Trond Myklebust17f26b12013-08-21 15:48:42 -04009114static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009115 .rpc_call_prepare = nfs41_free_stateid_prepare,
9116 .rpc_call_done = nfs41_free_stateid_done,
9117 .rpc_release = nfs41_free_stateid_release,
9118};
9119
9120static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009121 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009122 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009123 bool privileged)
9124{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009125 struct rpc_message msg = {
9126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009127 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009128 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009129 struct rpc_task_setup task_setup = {
9130 .rpc_client = server->client,
9131 .rpc_message = &msg,
9132 .callback_ops = &nfs41_free_stateid_ops,
9133 .flags = RPC_TASK_ASYNC,
9134 };
9135 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009136
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009137 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9138 &task_setup.rpc_client, &msg);
9139
Chuck Lever38527b12012-07-11 16:30:23 -04009140 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009141 data = kmalloc(sizeof(*data), GFP_NOFS);
9142 if (!data)
9143 return ERR_PTR(-ENOMEM);
9144 data->server = server;
9145 nfs4_stateid_copy(&data->args.stateid, stateid);
9146
9147 task_setup.callback_data = data;
9148
9149 msg.rpc_argp = &data->args;
9150 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009151 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009152 if (privileged)
9153 nfs4_set_sequence_privileged(&data->args.seq_args);
9154
9155 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009156}
9157
Chuck Lever38527b12012-07-11 16:30:23 -04009158/**
9159 * nfs41_free_stateid - perform a FREE_STATEID operation
9160 *
9161 * @server: server / transport on which to perform the operation
9162 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009163 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009164 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009165 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009166 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009167 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009168static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009169 const nfs4_stateid *stateid,
9170 struct rpc_cred *cred,
9171 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009172{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009173 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009174
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009175 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009176 if (IS_ERR(task))
9177 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009178 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009179 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009180}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009181
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009182static void
9183nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009184{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009185 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009186
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009187 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009188 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009189}
9190
Trond Myklebust36281ca2012-03-04 18:13:56 -05009191static bool nfs41_match_stateid(const nfs4_stateid *s1,
9192 const nfs4_stateid *s2)
9193{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009194 if (s1->type != s2->type)
9195 return false;
9196
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009197 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009198 return false;
9199
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009200 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009201 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009202 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009203 return true;
9204
9205 return false;
9206}
9207
Andy Adamson557134a2009-04-01 09:21:53 -04009208#endif /* CONFIG_NFS_V4_1 */
9209
Trond Myklebust36281ca2012-03-04 18:13:56 -05009210static bool nfs4_match_stateid(const nfs4_stateid *s1,
9211 const nfs4_stateid *s2)
9212{
Trond Myklebustf597c532012-03-04 18:13:56 -05009213 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009214}
9215
9216
Trond Myklebust17280172012-03-11 13:11:00 -04009217static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009218 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009219 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009220 .recover_open = nfs4_open_reclaim,
9221 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009222 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009223 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009224};
9225
Andy Adamson591d71c2009-04-01 09:22:47 -04009226#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009227static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009228 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9229 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9230 .recover_open = nfs4_open_reclaim,
9231 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009232 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009233 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009234 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009235};
9236#endif /* CONFIG_NFS_V4_1 */
9237
Trond Myklebust17280172012-03-11 13:11:00 -04009238static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009239 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009240 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009241 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009242 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009243 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009244};
9245
Andy Adamson591d71c2009-04-01 09:22:47 -04009246#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009247static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009248 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9249 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009250 .recover_open = nfs41_open_expired,
9251 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009252 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009253};
9254#endif /* CONFIG_NFS_V4_1 */
9255
Trond Myklebust17280172012-03-11 13:11:00 -04009256static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009257 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009258 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009259 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009260};
9261
9262#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009263static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009264 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009265 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009266 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009267};
9268#endif
9269
Chuck Leverec011fe2013-10-17 14:12:39 -04009270static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009271 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009272 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009273};
9274
9275#if defined(CONFIG_NFS_V4_1)
9276static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009277 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009278 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009279};
9280#endif /* CONFIG_NFS_V4_1 */
9281
Trond Myklebust97dc1352010-06-16 09:52:26 -04009282static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9283 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009284 .init_caps = NFS_CAP_READDIRPLUS
9285 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009286 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009287 .init_client = nfs40_init_client,
9288 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009289 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009290 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009291 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009292 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009293 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009294 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009295 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9296 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9297 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009298 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009299};
9300
9301#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009302static struct nfs_seqid *
9303nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9304{
9305 return NULL;
9306}
9307
Trond Myklebust97dc1352010-06-16 09:52:26 -04009308static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9309 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009310 .init_caps = NFS_CAP_READDIRPLUS
9311 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009312 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009313 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009314 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009315 .init_client = nfs41_init_client,
9316 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009317 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009318 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009319 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009320 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009321 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009322 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009323 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009324 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9325 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9326 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009327 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009328};
9329#endif
9330
Steve Dickson42c2c422013-05-22 12:50:38 -04009331#if defined(CONFIG_NFS_V4_2)
9332static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9333 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009334 .init_caps = NFS_CAP_READDIRPLUS
9335 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009336 | NFS_CAP_POSIX_LOCK
9337 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009338 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009339 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009340 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009341 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009342 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009343 | NFS_CAP_LAYOUTSTATS
9344 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009345 .init_client = nfs41_init_client,
9346 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009347 .match_stateid = nfs41_match_stateid,
9348 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009349 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009350 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009351 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009352 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009353 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009354 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9355 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9356 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009357 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009358};
9359#endif
9360
Trond Myklebust97dc1352010-06-16 09:52:26 -04009361const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9362 [0] = &nfs_v4_0_minor_ops,
9363#if defined(CONFIG_NFS_V4_1)
9364 [1] = &nfs_v4_1_minor_ops,
9365#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009366#if defined(CONFIG_NFS_V4_2)
9367 [2] = &nfs_v4_2_minor_ops,
9368#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009369};
9370
Trond Myklebust13997822016-07-24 17:10:52 -04009371static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009372{
9373 ssize_t error, error2;
9374
9375 error = generic_listxattr(dentry, list, size);
9376 if (error < 0)
9377 return error;
9378 if (list) {
9379 list += error;
9380 size -= error;
9381 }
9382
9383 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9384 if (error2 < 0)
9385 return error2;
9386 return error + error2;
9387}
9388
Trond Myklebust17f26b12013-08-21 15:48:42 -04009389static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009390 .create = nfs_create,
9391 .lookup = nfs_lookup,
9392 .atomic_open = nfs_atomic_open,
9393 .link = nfs_link,
9394 .unlink = nfs_unlink,
9395 .symlink = nfs_symlink,
9396 .mkdir = nfs_mkdir,
9397 .rmdir = nfs_rmdir,
9398 .mknod = nfs_mknod,
9399 .rename = nfs_rename,
9400 .permission = nfs_permission,
9401 .getattr = nfs_getattr,
9402 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009403 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009404};
9405
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009406static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009407 .permission = nfs_permission,
9408 .getattr = nfs_getattr,
9409 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009410 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009411};
9412
David Howells509de812006-08-22 20:06:11 -04009413const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009414 .version = 4, /* protocol version */
9415 .dentry_ops = &nfs4_dentry_operations,
9416 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009417 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009418 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009419 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009420 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009421 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009422 .getattr = nfs4_proc_getattr,
9423 .setattr = nfs4_proc_setattr,
9424 .lookup = nfs4_proc_lookup,
9425 .access = nfs4_proc_access,
9426 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009427 .create = nfs4_proc_create,
9428 .remove = nfs4_proc_remove,
9429 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009430 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009431 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009432 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009433 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009434 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009435 .link = nfs4_proc_link,
9436 .symlink = nfs4_proc_symlink,
9437 .mkdir = nfs4_proc_mkdir,
9438 .rmdir = nfs4_proc_remove,
9439 .readdir = nfs4_proc_readdir,
9440 .mknod = nfs4_proc_mknod,
9441 .statfs = nfs4_proc_statfs,
9442 .fsinfo = nfs4_proc_fsinfo,
9443 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009444 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009445 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009446 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009447 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009448 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009449 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009450 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009451 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009452 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009453 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009454 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009455 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009456 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009457 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009458 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009459 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009460 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009461 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009462 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009463 .create_server = nfs4_create_server,
9464 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009465};
9466
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009467static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009468 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009469 .list = nfs4_xattr_list_nfs4_acl,
9470 .get = nfs4_xattr_get_nfs4_acl,
9471 .set = nfs4_xattr_set_nfs4_acl,
9472};
9473
9474const struct xattr_handler *nfs4_xattr_handlers[] = {
9475 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009476#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9477 &nfs4_xattr_nfs4_label_handler,
9478#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009479 NULL
9480};
9481
Linus Torvalds1da177e2005-04-16 15:20:36 -07009482/*
9483 * Local variables:
9484 * c-basic-offset: 8
9485 * End:
9486 */