blob: a0b4e1091340df476bd48990e3631d1588d25052 [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{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400278 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700279 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000282 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
284 return;
285 }
286
287 readdir->cookie = 0;
288 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
289 if (cookie == 2)
290 return;
291
292 /*
293 * NFSv4 servers do not return entries for '.' and '..'
294 * Therefore, we fake these entries here. We let '.'
295 * have cookie 0 and '..' have cookie 1. Note that
296 * when talking to the server, we always send cookie 0
297 * instead of 1 or 2.
298 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800299 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 if (cookie == 0) {
302 *p++ = xdr_one; /* next */
303 *p++ = xdr_zero; /* cookie, first word */
304 *p++ = xdr_one; /* cookie, second word */
305 *p++ = xdr_one; /* entry len */
306 memcpy(p, ".\0\0\0", 4); /* entry */
307 p++;
308 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400309 *p++ = htonl(attrs); /* bitmap */
310 *p++ = htonl(12); /* attribute buffer length */
311 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000312 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 *p++ = xdr_one; /* next */
316 *p++ = xdr_zero; /* cookie, first word */
317 *p++ = xdr_two; /* cookie, second word */
318 *p++ = xdr_two; /* entry len */
319 memcpy(p, "..\0\0", 4); /* entry */
320 p++;
321 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400322 *p++ = htonl(attrs); /* bitmap */
323 *p++ = htonl(12); /* attribute buffer length */
324 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000325 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 readdir->pgbase = (char *)p - (char *)start;
328 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800329 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Trond Myklebust26d36302016-09-22 13:39:05 -0400332static void nfs4_test_and_free_stateid(struct nfs_server *server,
333 nfs4_stateid *stateid,
334 struct rpc_cred *cred)
335{
336 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
337
338 ops->test_and_free_expired(server, stateid, cred);
339}
340
341static void __nfs4_free_revoked_stateid(struct nfs_server *server,
342 nfs4_stateid *stateid,
343 struct rpc_cred *cred)
344{
345 stateid->type = NFS4_REVOKED_STATEID_TYPE;
346 nfs4_test_and_free_stateid(server, stateid, cred);
347}
348
349static void nfs4_free_revoked_stateid(struct nfs_server *server,
350 const nfs4_stateid *stateid,
351 struct rpc_cred *cred)
352{
353 nfs4_stateid tmp;
354
355 nfs4_stateid_copy(&tmp, stateid);
356 __nfs4_free_revoked_stateid(server, &tmp, cred);
357}
358
NeilBrown8478eaa2014-09-18 16:09:27 +1000359static long nfs4_update_delay(long *timeout)
360{
361 long ret;
362 if (!timeout)
363 return NFS4_POLL_RETRY_MAX;
364 if (*timeout <= 0)
365 *timeout = NFS4_POLL_RETRY_MIN;
366 if (*timeout > NFS4_POLL_RETRY_MAX)
367 *timeout = NFS4_POLL_RETRY_MAX;
368 ret = *timeout;
369 *timeout <<= 1;
370 return ret;
371}
372
Trond Myklebust65de8722008-12-23 15:21:44 -0500373static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
374{
375 int res = 0;
376
377 might_sleep();
378
NeilBrown8478eaa2014-09-18 16:09:27 +1000379 freezable_schedule_timeout_killable_unsafe(
380 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500381 if (fatal_signal_pending(current))
382 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500383 return res;
384}
385
386/* This is the error handling routine for processes that are allowed
387 * to sleep.
388 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400389static int nfs4_do_handle_exception(struct nfs_server *server,
390 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500391{
392 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500393 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400394 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500395 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500396 int ret = errorcode;
397
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400398 exception->delay = 0;
399 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500400 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400401
402 if (stateid == NULL && state != NULL)
403 stateid = &state->stateid;
404
Trond Myklebust65de8722008-12-23 15:21:44 -0500405 switch(errorcode) {
406 case 0:
407 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400408 case -NFS4ERR_DELEG_REVOKED:
409 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400410 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400411 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400412 if (inode != NULL && stateid != NULL) {
413 nfs_inode_find_state_and_recover(inode,
414 stateid);
415 goto wait_on_recovery;
416 }
417 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400418 if (inode) {
419 int err;
420
421 err = nfs_async_inode_return_delegation(inode,
422 stateid);
423 if (err == 0)
424 goto wait_on_recovery;
425 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
426 exception->retry = 1;
427 break;
428 }
429 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500430 if (state == NULL)
431 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400432 ret = nfs4_schedule_stateid_recovery(server, state);
433 if (ret < 0)
434 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500435 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500436 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500437 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500438 nfs4_schedule_lease_recovery(clp);
439 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400440 case -NFS4ERR_MOVED:
441 ret = nfs4_schedule_migration_recovery(server);
442 if (ret < 0)
443 break;
444 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400445 case -NFS4ERR_LEASE_MOVED:
446 nfs4_schedule_lease_moved_recovery(clp);
447 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500448#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400449 case -NFS4ERR_BADSESSION:
450 case -NFS4ERR_BADSLOT:
451 case -NFS4ERR_BAD_HIGH_SLOT:
452 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
453 case -NFS4ERR_DEADSESSION:
454 case -NFS4ERR_SEQ_FALSE_RETRY:
455 case -NFS4ERR_SEQ_MISORDERED:
456 dprintk("%s ERROR: %d Reset session\n", __func__,
457 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400458 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400459 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500460#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500461 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500462 if (exception->timeout > HZ) {
463 /* We have retried a decent amount, time to
464 * fail
465 */
466 ret = -EBUSY;
467 break;
468 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500469 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400470 nfs_inc_server_stats(server, NFSIOS_DELAY);
471 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400472 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400473 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400474 exception->delay = 1;
475 return 0;
476
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400477 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500478 case -NFS4ERR_OLD_STATEID:
479 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800480 break;
481 case -NFS4ERR_BADOWNER:
482 /* The following works around a Linux server bug! */
483 case -NFS4ERR_BADNAME:
484 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
485 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
486 exception->retry = 1;
487 printk(KERN_WARNING "NFS: v4 server %s "
488 "does not accept raw "
489 "uid/gids. "
490 "Reenabling the idmapper.\n",
491 server->nfs_client->cl_hostname);
492 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500493 }
494 /* We failed to handle the error */
495 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500496wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400497 exception->recovering = 1;
498 return 0;
499}
500
501/* This is the error handling routine for processes that are allowed
502 * to sleep.
503 */
504int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
505{
506 struct nfs_client *clp = server->nfs_client;
507 int ret;
508
509 ret = nfs4_do_handle_exception(server, errorcode, exception);
510 if (exception->delay) {
511 ret = nfs4_delay(server->client, &exception->timeout);
512 goto out_retry;
513 }
514 if (exception->recovering) {
515 ret = nfs4_wait_clnt_recover(clp);
516 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
517 return -EIO;
518 goto out_retry;
519 }
520 return ret;
521out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500522 if (ret == 0)
523 exception->retry = 1;
524 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500525}
526
Trond Myklebust037fc982015-09-20 15:51:00 -0400527static int
528nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
529 int errorcode, struct nfs4_exception *exception)
530{
531 struct nfs_client *clp = server->nfs_client;
532 int ret;
533
534 ret = nfs4_do_handle_exception(server, errorcode, exception);
535 if (exception->delay) {
536 rpc_delay(task, nfs4_update_delay(&exception->timeout));
537 goto out_retry;
538 }
539 if (exception->recovering) {
540 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
541 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
542 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
543 goto out_retry;
544 }
545 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
546 ret = -EIO;
547 return ret;
548out_retry:
549 if (ret == 0)
550 exception->retry = 1;
551 return ret;
552}
553
554static int
555nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
556 struct nfs4_state *state, long *timeout)
557{
558 struct nfs4_exception exception = {
559 .state = state,
560 };
561
562 if (task->tk_status >= 0)
563 return 0;
564 if (timeout)
565 exception.timeout = *timeout;
566 task->tk_status = nfs4_async_handle_exception(task, server,
567 task->tk_status,
568 &exception);
569 if (exception.delay && timeout)
570 *timeout = exception.timeout;
571 if (exception.retry)
572 return -EAGAIN;
573 return 0;
574}
575
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400576/*
577 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
578 * or 'false' otherwise.
579 */
580static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
581{
582 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500583 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400584}
Trond Myklebust65de8722008-12-23 15:21:44 -0500585
Trond Myklebust452e9352010-07-31 14:29:06 -0400586static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 spin_lock(&clp->cl_lock);
589 if (time_before(clp->cl_last_renewal,timestamp))
590 clp->cl_last_renewal = timestamp;
591 spin_unlock(&clp->cl_lock);
592}
593
Trond Myklebust452e9352010-07-31 14:29:06 -0400594static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
595{
Trond Myklebustbe824162015-07-05 14:50:46 -0400596 struct nfs_client *clp = server->nfs_client;
597
598 if (!nfs4_has_session(clp))
599 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400600}
601
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400602struct nfs4_call_sync_data {
603 const struct nfs_server *seq_server;
604 struct nfs4_sequence_args *seq_args;
605 struct nfs4_sequence_res *seq_res;
606};
607
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800608void nfs4_init_sequence(struct nfs4_sequence_args *args,
609 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400610{
611 args->sa_slot = NULL;
612 args->sa_cache_this = cache_reply;
613 args->sa_privileged = 0;
614
615 res->sr_slot = NULL;
616}
617
618static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
619{
620 args->sa_privileged = 1;
621}
622
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400623static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400624{
625 struct nfs4_slot *slot = res->sr_slot;
626 struct nfs4_slot_table *tbl;
627
Chuck Lever3bd23842013-08-09 12:49:19 -0400628 tbl = slot->table;
629 spin_lock(&tbl->slot_tbl_lock);
630 if (!nfs41_wake_and_assign_slot(tbl, slot))
631 nfs4_free_slot(tbl, slot);
632 spin_unlock(&tbl->slot_tbl_lock);
633
634 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400635}
636
637static int nfs40_sequence_done(struct rpc_task *task,
638 struct nfs4_sequence_res *res)
639{
640 if (res->sr_slot != NULL)
641 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400642 return 1;
643}
644
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400645#if defined(CONFIG_NFS_V4_1)
646
Trond Myklebustd185a332010-06-16 09:52:25 -0400647static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400648{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500649 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400650 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500651 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500652 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400653
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500654 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500655 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500656
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400657 /* Bump the slot sequence number */
658 if (slot->seq_done)
659 slot->seq_nr++;
660 slot->seq_done = 0;
661
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500662 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500663 /* Be nice to the server: try to ensure that the last transmitted
664 * value for highest_user_slotid <= target_highest_slotid
665 */
666 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
667 send_new_highest_used_slotid = true;
668
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500669 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500670 send_new_highest_used_slotid = false;
671 goto out_unlock;
672 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500673 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500674
675 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
676 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500677out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500678 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400679 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500680 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400681 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400682 if (waitqueue_active(&tbl->slot_waitq))
683 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400684}
685
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400686static int nfs41_sequence_process(struct rpc_task *task,
687 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400688{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500689 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500690 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400691 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500692 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500693 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400694
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500695 if (slot == NULL)
696 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400697 /* don't increment the sequence number if the task wasn't sent */
698 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400699 goto out;
700
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500701 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500702
Trond Myklebustac20d162012-12-15 15:36:07 -0500703 if (slot->interrupted) {
Olga Kornievskaia88bd4f82017-04-26 14:21:22 -0400704 if (res->sr_status != -NFS4ERR_DELAY)
705 slot->interrupted = 0;
Trond Myklebustac20d162012-12-15 15:36:07 -0500706 interrupted = true;
707 }
708
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400709 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500710 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400711 switch (res->sr_status) {
712 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400713 /* If previous op on slot was interrupted and we reused
714 * the seq# and got a reply from the cache, then retry
715 */
716 if (task->tk_status == -EREMOTEIO && interrupted) {
717 ++slot->seq_nr;
718 goto retry_nowait;
719 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400720 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400721 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500722 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500723 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500724 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400725 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
726 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500727 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400728 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500729 case 1:
730 /*
731 * sr_status remains 1 if an RPC level error occurred.
732 * The server may or may not have processed the sequence
733 * operation..
734 * Mark the slot as having hosted an interrupted RPC call.
735 */
736 slot->interrupted = 1;
737 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400738 case -NFS4ERR_DELAY:
739 /* The server detected a resend of the RPC call and
740 * returned NFS4ERR_DELAY as per Section 2.10.6.2
741 * of RFC5661.
742 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500743 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400744 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500745 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500746 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400747 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500748 case -NFS4ERR_BADSLOT:
749 /*
750 * The slot id we used was probably retired. Try again
751 * using a different slot id.
752 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500753 goto retry_nowait;
754 case -NFS4ERR_SEQ_MISORDERED:
755 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500756 * Was the last operation on this sequence interrupted?
757 * If so, retry after bumping the sequence number.
758 */
759 if (interrupted) {
760 ++slot->seq_nr;
761 goto retry_nowait;
762 }
763 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500764 * Could this slot have been previously retired?
765 * If so, then the server may be expecting seq_nr = 1!
766 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500767 if (slot->seq_nr != 1) {
768 slot->seq_nr = 1;
769 goto retry_nowait;
770 }
771 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500772 case -NFS4ERR_SEQ_FALSE_RETRY:
773 ++slot->seq_nr;
774 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400775 default:
776 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400777 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400778 }
779out:
780 /* The session may be reset by one of the error handlers. */
781 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500782out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500783 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500784retry_nowait:
785 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400786 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500787 task->tk_status = 0;
788 ret = 0;
789 }
790 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400791out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400792 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400793 goto out;
794 rpc_delay(task, NFS4_POLL_RETRY_MAX);
795 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400796}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400797
798int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
799{
800 if (!nfs41_sequence_process(task, res))
801 return 0;
802 if (res->sr_slot != NULL)
803 nfs41_sequence_free_slot(res);
804 return 1;
805
806}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500807EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400808
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400809static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
810{
811 if (res->sr_slot == NULL)
812 return 1;
813 if (res->sr_slot->table->session != NULL)
814 return nfs41_sequence_process(task, res);
815 return nfs40_sequence_done(task, res);
816}
817
818static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
819{
820 if (res->sr_slot != NULL) {
821 if (res->sr_slot->table->session != NULL)
822 nfs41_sequence_free_slot(res);
823 else
824 nfs40_sequence_free_slot(res);
825 }
826}
827
Peng Tao2c4b1312014-06-11 05:24:16 +0800828int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400829{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500830 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400831 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400832 if (!res->sr_slot->table->session)
833 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400834 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400835}
Peng Tao2c4b1312014-06-11 05:24:16 +0800836EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400837
Andy Adamsonce5039c2009-04-01 09:22:13 -0400838static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
839{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400840 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400841
Trond Myklebust035168ab2010-06-16 09:52:26 -0400842 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
843
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500844 nfs4_setup_sequence(data->seq_server->nfs_client,
845 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400846}
847
Andy Adamson69ab40c2009-04-01 09:22:19 -0400848static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
849{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400850 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400851
Trond Myklebust14516c32010-07-31 14:29:06 -0400852 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400853}
854
Trond Myklebust17280172012-03-11 13:11:00 -0400855static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400856 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400857 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400858};
859
Chuck Lever3bd23842013-08-09 12:49:19 -0400860#else /* !CONFIG_NFS_V4_1 */
861
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400862static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
863{
864 return nfs40_sequence_done(task, res);
865}
866
867static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
868{
869 if (res->sr_slot != NULL)
870 nfs40_sequence_free_slot(res);
871}
872
Peng Tao2c4b1312014-06-11 05:24:16 +0800873int nfs4_sequence_done(struct rpc_task *task,
874 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400875{
Chuck Lever3bd23842013-08-09 12:49:19 -0400876 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400877}
Peng Tao2c4b1312014-06-11 05:24:16 +0800878EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400879
880#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400881
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500882int nfs4_setup_sequence(const struct nfs_client *client,
883 struct nfs4_sequence_args *args,
884 struct nfs4_sequence_res *res,
885 struct rpc_task *task)
886{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500887 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500888 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500889 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500890
Anna Schumaker9dd91072017-01-10 12:01:46 -0500891 /* slot already allocated? */
892 if (res->sr_slot != NULL)
893 goto out_start;
894
Anna Schumaker76ee0352017-01-10 16:49:31 -0500895 if (session) {
896 tbl = &session->fc_slot_table;
897 task->tk_timeout = 0;
898 }
899
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500900 spin_lock(&tbl->slot_tbl_lock);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500901 /* The state manager will wait until the slot table is empty */
902 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
903 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500904
Anna Schumaker3d358082017-01-11 10:54:04 -0500905 slot = nfs4_alloc_slot(tbl);
906 if (IS_ERR(slot)) {
907 /* Try again in 1/4 second */
908 if (slot == ERR_PTR(-ENOMEM))
909 task->tk_timeout = HZ >> 2;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500910 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -0500911 }
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500912 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500913
Anna Schumaker3d358082017-01-11 10:54:04 -0500914 slot->privileged = args->sa_privileged ? 1 : 0;
915 args->sa_slot = slot;
916
917 res->sr_slot = slot;
918 if (session) {
919 res->sr_timestamp = jiffies;
920 res->sr_status_flags = 0;
921 res->sr_status = 1;
Anna Schumaker3d358082017-01-11 10:54:04 -0500922 }
923
Anna Schumakerad05cc02017-01-11 13:37:06 -0500924 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd91072017-01-10 12:01:46 -0500925out_start:
926 rpc_call_start(task);
927 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500928
929out_sleep:
930 if (args->sa_privileged)
931 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
932 NULL, RPC_PRIORITY_PRIVILEGED);
933 else
934 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
935 spin_unlock(&tbl->slot_tbl_lock);
936 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500937}
938EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
939
Chuck Lever9915ea72013-08-09 12:48:27 -0400940static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
941{
942 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500943 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -0400944 data->seq_args, data->seq_res, task);
945}
946
947static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
948{
949 struct nfs4_call_sync_data *data = calldata;
950 nfs4_sequence_done(task, data->seq_res);
951}
952
953static const struct rpc_call_ops nfs40_call_sync_ops = {
954 .rpc_call_prepare = nfs40_call_sync_prepare,
955 .rpc_call_done = nfs40_call_sync_done,
956};
957
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400958static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400959 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500960 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100961 struct nfs4_sequence_args *args,
962 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400963{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100964 int ret;
965 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400966 struct nfs_client *clp = server->nfs_client;
967 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100968 .seq_server = server,
969 .seq_args = args,
970 .seq_res = res,
971 };
972 struct rpc_task_setup task_setup = {
973 .rpc_client = clnt,
974 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400975 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100976 .callback_data = &data
977 };
978
979 task = rpc_run_task(&task_setup);
980 if (IS_ERR(task))
981 ret = PTR_ERR(task);
982 else {
983 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400984 rpc_put_task(task);
985 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100986 return ret;
987}
988
Bryan Schumaker7c513052011-03-24 17:12:24 +0000989int nfs4_call_sync(struct rpc_clnt *clnt,
990 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000991 struct rpc_message *msg,
992 struct nfs4_sequence_args *args,
993 struct nfs4_sequence_res *res,
994 int cache_reply)
995{
Chuck Levera9c92d62013-08-09 12:48:18 -0400996 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400997 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000998}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001000static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1001 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 struct nfs_inode *nfsi = NFS_I(dir);
1004
1005 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001006 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001007 if (cinfo->atomic && cinfo->before == dir->i_version) {
1008 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1009 nfsi->attrtimeo_timestamp = jiffies;
1010 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 nfs_force_lookup_revalidate(dir);
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001012 if (cinfo->before != dir->i_version)
1013 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1014 NFS_INO_INVALID_ACL;
1015 }
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001016 dir->i_version = cinfo->after;
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001017 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001018 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001019 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 spin_unlock(&dir->i_lock);
1021}
1022
1023struct nfs4_opendata {
1024 struct kref kref;
1025 struct nfs_openargs o_arg;
1026 struct nfs_openres o_res;
1027 struct nfs_open_confirmargs c_arg;
1028 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001029 struct nfs4_string owner_name;
1030 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001031 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001033 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001035 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 struct nfs4_state_owner *owner;
1037 struct nfs4_state *state;
1038 struct iattr attrs;
1039 unsigned long timestamp;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001040 bool rpc_done;
1041 bool file_created;
1042 bool is_recover;
1043 bool cancelled;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001044 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001046
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001047static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1048 int err, struct nfs4_exception *exception)
1049{
1050 if (err != -EINVAL)
1051 return false;
1052 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1053 return false;
1054 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1055 exception->retry = 1;
1056 return true;
1057}
1058
Trond Myklebust6ae37332015-01-30 14:21:14 -05001059static u32
1060nfs4_map_atomic_open_share(struct nfs_server *server,
1061 fmode_t fmode, int openflags)
1062{
1063 u32 res = 0;
1064
1065 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1066 case FMODE_READ:
1067 res = NFS4_SHARE_ACCESS_READ;
1068 break;
1069 case FMODE_WRITE:
1070 res = NFS4_SHARE_ACCESS_WRITE;
1071 break;
1072 case FMODE_READ|FMODE_WRITE:
1073 res = NFS4_SHARE_ACCESS_BOTH;
1074 }
1075 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1076 goto out;
1077 /* Want no delegation if we're using O_DIRECT */
1078 if (openflags & O_DIRECT)
1079 res |= NFS4_SHARE_WANT_NO_DELEG;
1080out:
1081 return res;
1082}
1083
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001084static enum open_claim_type4
1085nfs4_map_atomic_open_claim(struct nfs_server *server,
1086 enum open_claim_type4 claim)
1087{
1088 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1089 return claim;
1090 switch (claim) {
1091 default:
1092 return claim;
1093 case NFS4_OPEN_CLAIM_FH:
1094 return NFS4_OPEN_CLAIM_NULL;
1095 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1096 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1097 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1098 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1099 }
1100}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001103{
1104 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001105 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001106 p->o_res.seqid = p->o_arg.seqid;
1107 p->c_res.seqid = p->c_arg.seqid;
1108 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001109 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001110 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001111 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001112}
1113
Al Viro82a2c1b2011-06-22 18:30:55 -04001114static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001115 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001116 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001117 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001118 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001119 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001120{
Al Viro82a2c1b2011-06-22 18:30:55 -04001121 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001122 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001123 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001124 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001125 struct nfs4_opendata *p;
1126
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001127 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001128 if (p == NULL)
1129 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001130
1131 p->f_label = nfs4_label_alloc(server, gfp_mask);
1132 if (IS_ERR(p->f_label))
1133 goto err_free_p;
1134
Kinglong Meea49c2692015-07-27 15:31:38 +08001135 p->a_label = nfs4_label_alloc(server, gfp_mask);
1136 if (IS_ERR(p->a_label))
1137 goto err_free_f;
1138
Trond Myklebust63f5f792015-01-23 19:19:25 -05001139 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1140 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001141 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001142 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001143 nfs_sb_active(dentry->d_sb);
1144 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001145 p->dir = parent;
1146 p->owner = sp;
1147 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001148 p->o_arg.open_flags = flags;
1149 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001150 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001151 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001152 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1153 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001154 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1155 * will return permission denied for all bits until close */
1156 if (!(flags & O_EXCL)) {
1157 /* ask server to check for all possible rights as results
1158 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001159 switch (p->o_arg.claim) {
1160 default:
1161 break;
1162 case NFS4_OPEN_CLAIM_NULL:
1163 case NFS4_OPEN_CLAIM_FH:
1164 p->o_arg.access = NFS4_ACCESS_READ |
1165 NFS4_ACCESS_MODIFY |
1166 NFS4_ACCESS_EXTEND |
1167 NFS4_ACCESS_EXECUTE;
1168 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001169 }
David Howells7539bba2006-08-22 20:06:09 -04001170 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001171 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1172 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001173 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001174 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001175 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001176 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001177 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001178 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001179 case NFS4_OPEN_CLAIM_NULL:
1180 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1181 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1182 p->o_arg.fh = NFS_FH(dir);
1183 break;
1184 case NFS4_OPEN_CLAIM_PREVIOUS:
1185 case NFS4_OPEN_CLAIM_FH:
1186 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1187 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001188 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001189 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001190 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001191 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001192
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001193 p->o_arg.u.attrs = &p->attrs;
1194 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001195
1196 verf[0] = jiffies;
1197 verf[1] = current->pid;
1198 memcpy(p->o_arg.u.verifier.data, verf,
1199 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001200 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001201 p->c_arg.fh = &p->o_res.fh;
1202 p->c_arg.stateid = &p->o_res.stateid;
1203 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001204 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001205 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001206 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001207
1208err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001209 nfs4_label_free(p->a_label);
1210err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001211 nfs4_label_free(p->f_label);
1212err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001213 kfree(p);
1214err:
1215 dput(parent);
1216 return NULL;
1217}
1218
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001219static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001220{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001221 struct nfs4_opendata *p = container_of(kref,
1222 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001223 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001224
1225 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001226 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001227 if (p->state != NULL)
1228 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001229 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001230
Kinglong Meea49c2692015-07-27 15:31:38 +08001231 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001232 nfs4_label_free(p->f_label);
1233
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001234 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001235 dput(p->dentry);
1236 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001237 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001238 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001239 kfree(p);
1240}
1241
1242static void nfs4_opendata_put(struct nfs4_opendata *p)
1243{
1244 if (p != NULL)
1245 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001246}
1247
Trond Myklebust24311f82015-09-20 10:50:17 -04001248static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1249 fmode_t fmode)
1250{
1251 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1252 case FMODE_READ|FMODE_WRITE:
1253 return state->n_rdwr != 0;
1254 case FMODE_WRITE:
1255 return state->n_wronly != 0;
1256 case FMODE_READ:
1257 return state->n_rdonly != 0;
1258 }
1259 WARN_ON_ONCE(1);
1260 return false;
1261}
1262
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001263static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001264{
1265 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001266
Trond Myklebust536e43d2012-01-17 22:04:26 -05001267 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001268 goto out;
1269 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001270 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001271 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1272 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001273 break;
1274 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001275 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1276 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001277 break;
1278 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001279 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1280 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001281 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001282out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001283 return ret;
1284}
1285
Trond Myklebust2a606182015-08-19 22:30:00 -05001286static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1287 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001288{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001289 if (delegation == NULL)
1290 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001291 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001292 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001293 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1294 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001295 switch (claim) {
1296 case NFS4_OPEN_CLAIM_NULL:
1297 case NFS4_OPEN_CLAIM_FH:
1298 break;
1299 case NFS4_OPEN_CLAIM_PREVIOUS:
1300 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1301 break;
1302 default:
1303 return 0;
1304 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001305 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001306 return 1;
1307}
1308
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001309static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001310{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001311 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001312 case FMODE_WRITE:
1313 state->n_wronly++;
1314 break;
1315 case FMODE_READ:
1316 state->n_rdonly++;
1317 break;
1318 case FMODE_READ|FMODE_WRITE:
1319 state->n_rdwr++;
1320 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001321 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001322}
1323
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001324#ifdef CONFIG_NFS_V4_1
1325static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1326{
1327 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1328 return true;
1329 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1330 return true;
1331 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1332 return true;
1333 return false;
1334}
1335#endif /* CONFIG_NFS_V4_1 */
1336
Trond Myklebust4f14c192014-02-12 19:15:06 -05001337static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001338{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001339 struct nfs_client *clp = state->owner->so_server->nfs_client;
1340 bool need_recover = false;
1341
1342 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1343 need_recover = true;
1344 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1345 need_recover = true;
1346 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1347 need_recover = true;
1348 if (need_recover)
1349 nfs4_state_mark_reclaim_nograce(clp, state);
1350}
1351
Trond Myklebuste999e802014-02-10 18:20:47 -05001352static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001353 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001354{
1355 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1356 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001357 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001358 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001359 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001360 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001361 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001362 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1363 return true;
1364 return false;
1365}
1366
Trond Myklebustf95549c2015-01-23 18:06:09 -05001367static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1368{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001369 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1370 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001371 if (state->n_wronly)
1372 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1373 if (state->n_rdonly)
1374 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1375 if (state->n_rdwr)
1376 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001377 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001378}
1379
Trond Myklebust226056c2014-02-11 10:41:07 -05001380static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1381 nfs4_stateid *stateid, fmode_t fmode)
1382{
1383 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1384 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1385 case FMODE_WRITE:
1386 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1387 break;
1388 case FMODE_READ:
1389 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1390 break;
1391 case 0:
1392 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1393 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1394 clear_bit(NFS_OPEN_STATE, &state->flags);
1395 }
1396 if (stateid == NULL)
1397 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001398 /* Handle OPEN+OPEN_DOWNGRADE races */
1399 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1400 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001401 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001402 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001403 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001404 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001405 nfs4_stateid_copy(&state->stateid, stateid);
1406 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001407}
1408
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001409static void nfs_clear_open_stateid(struct nfs4_state *state,
1410 nfs4_stateid *arg_stateid,
1411 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001412{
1413 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001414 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1415 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1416 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001417 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001418 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1419 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001420}
1421
Trond Myklebust1393d962016-09-22 13:39:13 -04001422static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1423 const nfs4_stateid *stateid, fmode_t fmode,
1424 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001425{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001426 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001427 case FMODE_READ:
1428 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1429 break;
1430 case FMODE_WRITE:
1431 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1432 break;
1433 case FMODE_READ|FMODE_WRITE:
1434 set_bit(NFS_O_RDWR_STATE, &state->flags);
1435 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001436 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001437 return;
1438 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1439 nfs4_stateid_copy(&state->stateid, stateid);
1440 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001441}
1442
Trond Myklebust1393d962016-09-22 13:39:13 -04001443static void __update_open_stateid(struct nfs4_state *state,
1444 const nfs4_stateid *open_stateid,
1445 const nfs4_stateid *deleg_stateid,
1446 fmode_t fmode,
1447 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001449 /*
1450 * Protect the call to nfs4_state_set_mode_locked and
1451 * serialise the stateid update
1452 */
Andrew Elble361cad32015-12-02 09:20:57 -05001453 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001454 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001455 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001456 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001457 set_bit(NFS_DELEGATED_STATE, &state->flags);
1458 }
1459 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001460 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001461 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001462 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001463 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Trond Myklebust1393d962016-09-22 13:39:13 -04001466static int update_open_stateid(struct nfs4_state *state,
1467 const nfs4_stateid *open_stateid,
1468 const nfs4_stateid *delegation,
1469 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001470{
Trond Myklebust1393d962016-09-22 13:39:13 -04001471 struct nfs_server *server = NFS_SERVER(state->inode);
1472 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001473 struct nfs_inode *nfsi = NFS_I(state->inode);
1474 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001475 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001476 int ret = 0;
1477
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001478 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001479
1480 rcu_read_lock();
1481 deleg_cur = rcu_dereference(nfsi->delegation);
1482 if (deleg_cur == NULL)
1483 goto no_delegation;
1484
1485 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001486 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001487 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001488 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001489 goto no_delegation_unlock;
1490
1491 if (delegation == NULL)
1492 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001493 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001494 goto no_delegation_unlock;
1495
Trond Myklebustb7391f42008-12-23 15:21:52 -05001496 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001497 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1498 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001499 ret = 1;
1500no_delegation_unlock:
1501 spin_unlock(&deleg_cur->lock);
1502no_delegation:
1503 rcu_read_unlock();
1504
1505 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001506 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001507 ret = 1;
1508 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001509 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001510 nfs4_schedule_state_manager(clp);
1511 if (freeme.type != 0)
1512 nfs4_test_and_free_stateid(server, &freeme,
1513 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001514
1515 return ret;
1516}
1517
Trond Myklebust39071e62015-01-24 15:07:56 -05001518static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1519 const nfs4_stateid *stateid)
1520{
1521 struct nfs4_state *state = lsp->ls_state;
1522 bool ret = false;
1523
1524 spin_lock(&state->state_lock);
1525 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1526 goto out_noupdate;
1527 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1528 goto out_noupdate;
1529 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1530 ret = true;
1531out_noupdate:
1532 spin_unlock(&state->state_lock);
1533 return ret;
1534}
Trond Myklebust34310432008-12-23 15:21:38 -05001535
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001536static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001537{
1538 struct nfs_delegation *delegation;
1539
1540 rcu_read_lock();
1541 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001542 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001543 rcu_read_unlock();
1544 return;
1545 }
1546 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001547 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001548}
1549
Trond Myklebust6ee41262007-07-08 14:11:36 -04001550static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001551{
1552 struct nfs4_state *state = opendata->state;
1553 struct nfs_inode *nfsi = NFS_I(state->inode);
1554 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001555 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001556 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001557 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001558 nfs4_stateid stateid;
1559 int ret = -EAGAIN;
1560
Trond Myklebustaac00a82007-07-05 19:02:21 -04001561 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001562 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001563 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001564 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001565 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001566 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001567 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001568 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001569 rcu_read_lock();
1570 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001571 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001572 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001573 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001574 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001575 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001576 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001577 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001578 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001579 if (!opendata->is_recover) {
1580 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1581 if (ret != 0)
1582 goto out;
1583 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001584 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001585
1586 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001587 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001588 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001589 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001590out:
1591 return ERR_PTR(ret);
1592out_return_state:
1593 atomic_inc(&state->count);
1594 return state;
1595}
1596
Andy Adamsone23008e2012-10-02 21:07:32 -04001597static void
1598nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1599{
1600 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1601 struct nfs_delegation *delegation;
1602 int delegation_flags = 0;
1603
1604 rcu_read_lock();
1605 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1606 if (delegation)
1607 delegation_flags = delegation->flags;
1608 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001609 switch (data->o_arg.claim) {
1610 default:
1611 break;
1612 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1613 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001614 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1615 "returning a delegation for "
1616 "OPEN(CLAIM_DELEGATE_CUR)\n",
1617 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001618 return;
1619 }
1620 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001621 nfs_inode_set_delegation(state->inode,
1622 data->owner->so_cred,
1623 &data->o_res);
1624 else
1625 nfs_inode_reclaim_delegation(state->inode,
1626 data->owner->so_cred,
1627 &data->o_res);
1628}
1629
1630/*
1631 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1632 * and update the nfs4_state.
1633 */
1634static struct nfs4_state *
1635_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1636{
1637 struct inode *inode = data->state->inode;
1638 struct nfs4_state *state = data->state;
1639 int ret;
1640
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001641 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001642 if (data->rpc_status)
1643 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001644 /* cached opens have already been processed */
1645 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001646 }
1647
Andy Adamsone23008e2012-10-02 21:07:32 -04001648 ret = nfs_refresh_inode(inode, &data->f_attr);
1649 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001650 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001651
1652 if (data->o_res.delegation_type != 0)
1653 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001654update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001655 update_open_stateid(state, &data->o_res.stateid, NULL,
1656 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001657 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001658
1659 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001660}
1661
1662static struct nfs4_state *
1663_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001664{
1665 struct inode *inode;
1666 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001667 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001668
Trond Myklebustaac00a82007-07-05 19:02:21 -04001669 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001670 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001671 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001672 goto out;
1673 }
1674
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001675 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001676 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001677 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001678 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001679 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001680 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001681 goto err;
1682 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001683 state = nfs4_get_open_state(inode, data->owner);
1684 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001685 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001686 if (data->o_res.delegation_type != 0)
1687 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001688 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001689 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001690 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001691out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001692 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001693 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001694err_put_inode:
1695 iput(inode);
1696err:
1697 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001698}
1699
Andy Adamsone23008e2012-10-02 21:07:32 -04001700static struct nfs4_state *
1701nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1702{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001703 struct nfs4_state *ret;
1704
Andy Adamsone23008e2012-10-02 21:07:32 -04001705 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001706 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1707 else
1708 ret = _nfs4_opendata_to_nfs4_state(data);
1709 nfs4_sequence_free_slot(&data->o_res.seq_res);
1710 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001711}
1712
Trond Myklebust864472e2006-01-03 09:55:15 +01001713static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1714{
1715 struct nfs_inode *nfsi = NFS_I(state->inode);
1716 struct nfs_open_context *ctx;
1717
1718 spin_lock(&state->inode->i_lock);
1719 list_for_each_entry(ctx, &nfsi->open_files, list) {
1720 if (ctx->state != state)
1721 continue;
1722 get_nfs_open_context(ctx);
1723 spin_unlock(&state->inode->i_lock);
1724 return ctx;
1725 }
1726 spin_unlock(&state->inode->i_lock);
1727 return ERR_PTR(-ENOENT);
1728}
1729
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001730static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1731 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001732{
1733 struct nfs4_opendata *opendata;
1734
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001735 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001736 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001737 if (opendata == NULL)
1738 return ERR_PTR(-ENOMEM);
1739 opendata->state = state;
1740 atomic_inc(&state->count);
1741 return opendata;
1742}
1743
Trond Myklebust24311f82015-09-20 10:50:17 -04001744static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1745 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001746{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001747 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001748 int ret;
1749
Trond Myklebust24311f82015-09-20 10:50:17 -04001750 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001751 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001752 opendata->o_arg.open_flags = 0;
1753 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001754 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1755 NFS_SB(opendata->dentry->d_sb),
1756 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001757 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1758 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1759 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001760 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001761 if (ret != 0)
1762 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001763 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001764 if (IS_ERR(newstate))
1765 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001766 if (newstate != opendata->state)
1767 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001768 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001769 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001770}
1771
1772static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1773{
Trond Myklebust864472e2006-01-03 09:55:15 +01001774 int ret;
1775
Trond Myklebust4f14c192014-02-12 19:15:06 -05001776 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1777 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1778 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1779 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001780 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001781 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001782 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001783 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001784 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1785 if (ret != 0)
1786 return ret;
1787 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1788 if (ret != 0)
1789 return ret;
1790 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1791 if (ret != 0)
1792 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001793 /*
1794 * We may have performed cached opens for all three recoveries.
1795 * Check if we need to update the current stateid.
1796 */
1797 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001798 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001799 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001800 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001801 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001802 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001803 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001804 return 0;
1805}
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807/*
1808 * OPEN_RECLAIM:
1809 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001811static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001813 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001814 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001815 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 int status;
1817
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001818 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1819 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001820 if (IS_ERR(opendata))
1821 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001822 rcu_read_lock();
1823 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001824 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001825 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001826 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001827 opendata->o_arg.u.delegation_type = delegation_type;
1828 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001829 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return status;
1831}
1832
Trond Myklebust539cd032007-06-05 11:46:42 -04001833static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
1835 struct nfs_server *server = NFS_SERVER(state->inode);
1836 struct nfs4_exception exception = { };
1837 int err;
1838 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001839 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001840 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001841 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1842 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001843 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001844 break;
1845 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 } while (exception.retry);
1847 return err;
1848}
1849
Trond Myklebust864472e2006-01-03 09:55:15 +01001850static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1851{
1852 struct nfs_open_context *ctx;
1853 int ret;
1854
1855 ctx = nfs4_state_find_open_context(state);
1856 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001857 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001858 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001859 put_nfs_open_context(ctx);
1860 return ret;
1861}
1862
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001863static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001865 switch (err) {
1866 default:
1867 printk(KERN_ERR "NFS: %s: unhandled error "
1868 "%d.\n", __func__, err);
1869 case 0:
1870 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001871 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001872 case -ESTALE:
1873 break;
1874 case -NFS4ERR_BADSESSION:
1875 case -NFS4ERR_BADSLOT:
1876 case -NFS4ERR_BAD_HIGH_SLOT:
1877 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1878 case -NFS4ERR_DEADSESSION:
1879 set_bit(NFS_DELEGATED_STATE, &state->flags);
1880 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1881 return -EAGAIN;
1882 case -NFS4ERR_STALE_CLIENTID:
1883 case -NFS4ERR_STALE_STATEID:
1884 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001885 /* Don't recall a delegation if it was lost */
1886 nfs4_schedule_lease_recovery(server->nfs_client);
1887 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001888 case -NFS4ERR_MOVED:
1889 nfs4_schedule_migration_recovery(server);
1890 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001891 case -NFS4ERR_LEASE_MOVED:
1892 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1893 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001894 case -NFS4ERR_DELEG_REVOKED:
1895 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001896 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001897 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001898 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001899 nfs_inode_find_state_and_recover(state->inode,
1900 stateid);
1901 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001902 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001903 case -NFS4ERR_DELAY:
1904 case -NFS4ERR_GRACE:
1905 set_bit(NFS_DELEGATED_STATE, &state->flags);
1906 ssleep(1);
1907 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001908 case -ENOMEM:
1909 case -NFS4ERR_DENIED:
1910 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1911 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return err;
1914}
1915
Trond Myklebust24311f82015-09-20 10:50:17 -04001916int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1917 struct nfs4_state *state, const nfs4_stateid *stateid,
1918 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001919{
1920 struct nfs_server *server = NFS_SERVER(state->inode);
1921 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001922 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001923
1924 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1925 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1926 if (IS_ERR(opendata))
1927 return PTR_ERR(opendata);
1928 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001929 write_seqlock(&state->seqlock);
1930 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1931 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001932 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1933 switch (type & (FMODE_READ|FMODE_WRITE)) {
1934 case FMODE_READ|FMODE_WRITE:
1935 case FMODE_WRITE:
1936 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1937 if (err)
1938 break;
1939 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1940 if (err)
1941 break;
1942 case FMODE_READ:
1943 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1944 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001945 nfs4_opendata_put(opendata);
1946 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1947}
1948
Chuck Leverbe05c862013-08-09 12:49:47 -04001949static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1950{
1951 struct nfs4_opendata *data = calldata;
1952
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001953 nfs4_setup_sequence(data->o_arg.server->nfs_client,
1954 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001955}
1956
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001957static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1958{
1959 struct nfs4_opendata *data = calldata;
1960
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001961 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001962
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001963 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001964 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001965 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001966 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001967 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001968 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001969 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001970}
1971
1972static void nfs4_open_confirm_release(void *calldata)
1973{
1974 struct nfs4_opendata *data = calldata;
1975 struct nfs4_state *state = NULL;
1976
1977 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001978 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001979 goto out_free;
1980 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001981 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001982 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001983 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001984 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001985 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001986out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001987 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001988}
1989
1990static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001991 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001992 .rpc_call_done = nfs4_open_confirm_done,
1993 .rpc_release = nfs4_open_confirm_release,
1994};
1995
1996/*
1997 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1998 */
1999static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2000{
David Howells2b0143b2015-03-17 22:25:59 +00002001 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002002 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002003 struct rpc_message msg = {
2004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2005 .rpc_argp = &data->c_arg,
2006 .rpc_resp = &data->c_res,
2007 .rpc_cred = data->owner->so_cred,
2008 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002009 struct rpc_task_setup task_setup_data = {
2010 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002011 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002012 .callback_ops = &nfs4_open_confirm_ops,
2013 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002014 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002015 .flags = RPC_TASK_ASYNC,
2016 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 int status;
2018
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002019 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002020 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002021 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002022 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002023 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002024 if (data->is_recover)
2025 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002026 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002027 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002028 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002029 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002030 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002031 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002032 smp_wmb();
2033 } else
2034 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002035 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return status;
2037}
2038
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002039static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002041 struct nfs4_opendata *data = calldata;
2042 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002043 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002044 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002045
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002046 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002047 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002048 /*
2049 * Check if we still need to send an OPEN call, or if we can use
2050 * a delegation instead.
2051 */
2052 if (data->state != NULL) {
2053 struct nfs_delegation *delegation;
2054
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002055 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002056 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002057 rcu_read_lock();
2058 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002059 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002060 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002061 rcu_read_unlock();
2062 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002063 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002064 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002065 switch (claim) {
2066 default:
2067 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002068 case NFS4_OPEN_CLAIM_PREVIOUS:
2069 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2070 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002071 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002072 case NFS4_OPEN_CLAIM_FH:
2073 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002074 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2075 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002076 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002077 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002078 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002079 &data->o_res.seq_res,
2080 task) != 0)
2081 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002082
2083 /* Set the create mode (note dependency on the session type) */
2084 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2085 if (data->o_arg.open_flags & O_EXCL) {
2086 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2087 if (nfs4_has_persistent_session(clp))
2088 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2089 else if (clp->cl_mvops->minor_version > 0)
2090 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2091 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002092 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002093unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002094 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002095 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002096out_no_action:
2097 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002098out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002099 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002100}
2101
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002102static void nfs4_open_done(struct rpc_task *task, void *calldata)
2103{
2104 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002106 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002107
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002108 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002109 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002110
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002111 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002112 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2113 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002114 case S_IFREG:
2115 break;
2116 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002117 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002118 break;
2119 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002120 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002121 break;
2122 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002123 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002124 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002125 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002126 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002127 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2128 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002129 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002130 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002131}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002132
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002133static void nfs4_open_release(void *calldata)
2134{
2135 struct nfs4_opendata *data = calldata;
2136 struct nfs4_state *state = NULL;
2137
2138 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002139 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002140 goto out_free;
2141 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002142 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002143 goto out_free;
2144 /* In case we need an open_confirm, no cleanup! */
2145 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2146 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002147 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002148 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002149 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002150out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002151 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002152}
2153
2154static const struct rpc_call_ops nfs4_open_ops = {
2155 .rpc_call_prepare = nfs4_open_prepare,
2156 .rpc_call_done = nfs4_open_done,
2157 .rpc_release = nfs4_open_release,
2158};
2159
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002160static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002161{
David Howells2b0143b2015-03-17 22:25:59 +00002162 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002163 struct nfs_server *server = NFS_SERVER(dir);
2164 struct nfs_openargs *o_arg = &data->o_arg;
2165 struct nfs_openres *o_res = &data->o_res;
2166 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002167 struct rpc_message msg = {
2168 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2169 .rpc_argp = o_arg,
2170 .rpc_resp = o_res,
2171 .rpc_cred = data->owner->so_cred,
2172 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002173 struct rpc_task_setup task_setup_data = {
2174 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002175 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002176 .callback_ops = &nfs4_open_ops,
2177 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002178 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002179 .flags = RPC_TASK_ASYNC,
2180 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002181 int status;
2182
Chuck Levera9c92d62013-08-09 12:48:18 -04002183 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002184 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002185 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002186 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002187 data->cancelled = false;
2188 data->is_recover = false;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002189 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002190 nfs4_set_sequence_privileged(&o_arg->seq_args);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002191 data->is_recover = true;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002192 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002193 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002194 if (IS_ERR(task))
2195 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002196 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002197 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002198 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002199 smp_wmb();
2200 } else
2201 status = data->rpc_status;
2202 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002203
2204 return status;
2205}
2206
2207static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2208{
David Howells2b0143b2015-03-17 22:25:59 +00002209 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002210 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002211 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002212
2213 status = nfs4_run_open_task(data, 1);
2214 if (status != 0 || !data->rpc_done)
2215 return status;
2216
Trond Myklebust6926afd2012-01-07 13:22:46 -05002217 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2218
Anna Schumakerd7e98252017-01-11 16:13:29 -05002219 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002220 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002221
2222 return status;
2223}
2224
Trond Myklebustf3792d62014-07-10 08:54:32 -04002225/*
2226 * Additional permission checks in order to distinguish between an
2227 * open for read, and an open for execute. This works around the
2228 * fact that NFSv4 OPEN treats read and execute permissions as being
2229 * the same.
2230 * Note that in the non-execute case, we want to turn off permission
2231 * checking if we just created a new file (POSIX open() semantics).
2232 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002233static int nfs4_opendata_access(struct rpc_cred *cred,
2234 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002235 struct nfs4_state *state, fmode_t fmode,
2236 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002237{
2238 struct nfs_access_entry cache;
2239 u32 mask;
2240
2241 /* access call failed or for some reason the server doesn't
2242 * support any access modes -- defer access call until later */
2243 if (opendata->o_res.access_supported == 0)
2244 return 0;
2245
2246 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002247 /*
2248 * Use openflags to check for exec, because fmode won't
2249 * always have FMODE_EXEC set when file open for exec.
2250 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002251 if (openflags & __FMODE_EXEC) {
2252 /* ONLY check for exec rights */
2253 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002254 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002255 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002256
2257 cache.cred = cred;
2258 cache.jiffies = jiffies;
2259 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2260 nfs_access_add_cache(state->inode, &cache);
2261
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002262 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002263 return 0;
2264
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002265 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002266}
2267
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002268/*
2269 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2270 */
2271static int _nfs4_proc_open(struct nfs4_opendata *data)
2272{
David Howells2b0143b2015-03-17 22:25:59 +00002273 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002274 struct nfs_server *server = NFS_SERVER(dir);
2275 struct nfs_openargs *o_arg = &data->o_arg;
2276 struct nfs_openres *o_res = &data->o_res;
2277 int status;
2278
2279 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002280 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002281 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002282 if (status != 0) {
2283 if (status == -NFS4ERR_BADNAME &&
2284 !(o_arg->open_flags & O_CREAT))
2285 return -ENOENT;
2286 return status;
2287 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002288
Trond Myklebust6926afd2012-01-07 13:22:46 -05002289 nfs_fattr_map_and_free_names(server, &data->f_attr);
2290
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002291 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002292 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002293 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002294 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002295 data->file_created = true;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002296 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002297 update_changeattr(dir, &o_res->cinfo,
2298 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002299 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002300 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2301 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002303 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002305 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002307 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2308 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002309 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002310 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002311 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314/*
2315 * OPEN_EXPIRED:
2316 * reclaim state on the server after a network partition.
2317 * Assumes caller holds the appropriate lock
2318 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002319static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002321 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002322 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002324 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002325 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002326 if (IS_ERR(opendata))
2327 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002328 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002329 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002330 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002331 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002332 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333}
2334
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002335static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002336{
Trond Myklebust539cd032007-06-05 11:46:42 -04002337 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002338 struct nfs4_exception exception = { };
2339 int err;
2340
2341 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002342 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002343 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002344 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2345 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002346 switch (err) {
2347 default:
2348 goto out;
2349 case -NFS4ERR_GRACE:
2350 case -NFS4ERR_DELAY:
2351 nfs4_handle_exception(server, err, &exception);
2352 err = 0;
2353 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002354 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002355out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002356 return err;
2357}
2358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2360{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002362 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Trond Myklebust864472e2006-01-03 09:55:15 +01002364 ctx = nfs4_state_find_open_context(state);
2365 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002366 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002367 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002368 put_nfs_open_context(ctx);
2369 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370}
2371
Trond Myklebust41020b62016-09-22 13:38:58 -04002372static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2373 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002374{
Trond Myklebust41020b62016-09-22 13:38:58 -04002375 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002376 write_seqlock(&state->seqlock);
2377 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2378 write_sequnlock(&state->seqlock);
2379 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2380}
2381
2382static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2383{
2384 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002385 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002386}
2387
2388static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2389{
2390 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2391 nfs40_clear_delegation_stateid(state);
2392 return nfs4_open_expired(sp, state);
2393}
2394
Trond Myklebust45870d62016-09-22 13:38:59 -04002395static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2396 nfs4_stateid *stateid,
2397 struct rpc_cred *cred)
2398{
2399 return -NFS4ERR_BAD_STATEID;
2400}
2401
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002402#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002403static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2404 nfs4_stateid *stateid,
2405 struct rpc_cred *cred)
2406{
2407 int status;
2408
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002409 switch (stateid->type) {
2410 default:
2411 break;
2412 case NFS4_INVALID_STATEID_TYPE:
2413 case NFS4_SPECIAL_STATEID_TYPE:
2414 return -NFS4ERR_BAD_STATEID;
2415 case NFS4_REVOKED_STATEID_TYPE:
2416 goto out_free;
2417 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002418
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002419 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002420 switch (status) {
2421 case -NFS4ERR_EXPIRED:
2422 case -NFS4ERR_ADMIN_REVOKED:
2423 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002424 break;
2425 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002426 return status;
2427 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002428out_free:
2429 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002430 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002431 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002432}
2433
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002434static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002435{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002436 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002437 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002438 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002439 struct rpc_cred *cred;
2440 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002441
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002442 /* Get the delegation credential for use by test/free_stateid */
2443 rcu_read_lock();
2444 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002445 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002446 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002447 return;
2448 }
2449
2450 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002451 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2452 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2453 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002454 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002455 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002456 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002457 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002458
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002459 cred = get_rpccred(delegation->cred);
2460 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002461 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002462 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002463 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002464 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002465
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002466 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002467}
2468
2469/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002470 * nfs41_check_expired_locks - possibly free a lock stateid
2471 *
2472 * @state: NFSv4 state for an inode
2473 *
2474 * Returns NFS_OK if recovery for this stateid is now finished.
2475 * Otherwise a negative NFS4ERR value is returned.
2476 */
2477static int nfs41_check_expired_locks(struct nfs4_state *state)
2478{
2479 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002480 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002481 struct nfs_server *server = NFS_SERVER(state->inode);
2482
2483 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2484 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002485
2486 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002487 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2488 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2489 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2490
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002491 atomic_inc(&lsp->ls_count);
2492 spin_unlock(&state->state_lock);
2493
2494 nfs4_put_lock_state(prev);
2495 prev = lsp;
2496
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002497 status = nfs41_test_and_free_expired_stateid(server,
2498 &lsp->ls_stateid,
2499 cred);
2500 trace_nfs4_test_lock_stateid(state, lsp, status);
2501 if (status == -NFS4ERR_EXPIRED ||
2502 status == -NFS4ERR_BAD_STATEID) {
2503 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002504 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002505 if (!recover_lost_locks)
2506 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2507 } else if (status != NFS_OK) {
2508 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002509 nfs4_put_lock_state(prev);
2510 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002511 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002512 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002513 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002514 }
2515 spin_unlock(&state->state_lock);
2516 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002517out:
2518 return ret;
2519}
2520
2521/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002522 * nfs41_check_open_stateid - possibly free an open stateid
2523 *
2524 * @state: NFSv4 state for an inode
2525 *
2526 * Returns NFS_OK if recovery for this stateid is now finished.
2527 * Otherwise a negative NFS4ERR value is returned.
2528 */
2529static int nfs41_check_open_stateid(struct nfs4_state *state)
2530{
2531 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002532 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002533 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002534 int status;
2535
Trond Myklebustb134fc42016-09-22 13:39:16 -04002536 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002537 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2538 if (nfs4_have_delegation(state->inode, state->state))
2539 return NFS_OK;
2540 return -NFS4ERR_OPENMODE;
2541 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002542 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002543 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002544 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002545 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002546 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002547 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2548 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2549 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002550 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002551 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002552 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002553 if (status != NFS_OK)
2554 return status;
2555 if (nfs_open_stateid_recover_openmode(state))
2556 return -NFS4ERR_OPENMODE;
2557 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002558}
2559
2560static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2561{
Chuck Levereb64cf92012-07-11 16:30:05 -04002562 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002563
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002564 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002565 status = nfs41_check_expired_locks(state);
2566 if (status != NFS_OK)
2567 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002568 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002569 if (status != NFS_OK)
2570 status = nfs4_open_expired(sp, state);
2571 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002572}
2573#endif
2574
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002576 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2577 * fields corresponding to attributes that were used to store the verifier.
2578 * Make sure we clobber those fields in the later setattr call
2579 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002580static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2581 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002582{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002583 const u32 *attrset = opendata->o_res.attrset;
2584
2585 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002586 !(sattr->ia_valid & ATTR_ATIME_SET))
2587 sattr->ia_valid |= ATTR_ATIME;
2588
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002589 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002590 !(sattr->ia_valid & ATTR_MTIME_SET))
2591 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002592
2593 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002594 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
Benjamin Coddington501e7a42017-06-02 11:21:34 -04002595 (attrset[1] & FATTR4_WORD1_MODE ||
2596 attrset[2] & FATTR4_WORD2_MODE_UMASK))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002597 sattr->ia_valid &= ~ATTR_MODE;
2598
2599 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2600 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002601}
2602
Trond Myklebustc21443c2013-02-07 14:26:21 -05002603static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2604 fmode_t fmode,
2605 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002606 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002607{
2608 struct nfs4_state_owner *sp = opendata->owner;
2609 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002610 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002611 struct nfs4_state *state;
2612 unsigned int seq;
2613 int ret;
2614
2615 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2616
2617 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002618 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002619 goto out;
2620
2621 state = nfs4_opendata_to_nfs4_state(opendata);
2622 ret = PTR_ERR(state);
2623 if (IS_ERR(state))
2624 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002625 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002626 if (server->caps & NFS_CAP_POSIX_LOCK)
2627 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002628 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2629 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002630
Trond Myklebust275bb302013-05-29 13:11:28 -04002631 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002632 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002633 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002634 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002635 alias = d_exact_alias(dentry, state->inode);
2636 if (!alias)
2637 alias = d_splice_alias(igrab(state->inode), dentry);
2638 /* d_splice_alias() can't fail here - it's a non-directory */
2639 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002640 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002641 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002642 }
2643 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002644 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002645 }
2646
Trond Myklebustc21443c2013-02-07 14:26:21 -05002647 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2648 if (ret != 0)
2649 goto out;
2650
David Howells2b0143b2015-03-17 22:25:59 +00002651 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002652 nfs_inode_attach_open_context(ctx);
2653 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2654 nfs4_schedule_stateid_recovery(server, state);
2655 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002656out:
2657 return ret;
2658}
2659
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002660/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002661 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 */
Andy Adamson82be4172012-05-23 05:02:35 -04002663static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002664 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002665 int flags,
2666 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002667 struct nfs4_label *label,
2668 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
2670 struct nfs4_state_owner *sp;
2671 struct nfs4_state *state = NULL;
2672 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002673 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002674 struct dentry *dentry = ctx->dentry;
2675 struct rpc_cred *cred = ctx->cred;
2676 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2677 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002678 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002679 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002680 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
2682 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002684 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2685 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2687 goto out_err;
2688 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002689 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002690 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002691 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002692 if (d_really_is_positive(dentry))
2693 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002694 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002695 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002696 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002697 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002698 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002699 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002700 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
David Quigley14c43f72013-05-22 12:50:43 -04002702 if (label) {
2703 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2704 if (IS_ERR(olabel)) {
2705 status = PTR_ERR(olabel);
2706 goto err_opendata_put;
2707 }
2708 }
2709
Trond Myklebuste911b812014-03-26 13:24:37 -07002710 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2711 if (!opendata->f_attr.mdsthreshold) {
2712 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2713 if (!opendata->f_attr.mdsthreshold)
2714 goto err_free_label;
2715 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002716 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002717 }
David Howells2b0143b2015-03-17 22:25:59 +00002718 if (d_really_is_positive(dentry))
2719 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002720
Trond Myklebust3efb9722013-05-29 13:17:04 -04002721 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002722 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002723 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002724 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002725
NeilBrownefcbc042015-07-30 13:00:56 +10002726 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002727 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002728 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002729 /*
2730 * send create attributes which was not set by open
2731 * with an extra setattr.
2732 */
2733 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2734 nfs_fattr_init(opendata->o_res.f_attr);
2735 status = nfs4_do_setattr(state->inode, cred,
2736 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002737 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002738 if (status == 0) {
2739 nfs_setattr_update_inode(state->inode, sattr,
2740 opendata->o_res.f_attr);
2741 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2742 }
David Quigley1775fd32013-05-22 12:50:42 -04002743 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002744 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002745 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002746 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002747
Trond Myklebuste911b812014-03-26 13:24:37 -07002748 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002749 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002750 opendata->f_attr.mdsthreshold = NULL;
2751 }
Andy Adamson82be4172012-05-23 05:02:35 -04002752
David Quigley14c43f72013-05-22 12:50:43 -04002753 nfs4_label_free(olabel);
2754
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002755 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002758err_free_label:
2759 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002760err_opendata_put:
2761 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002762err_put_state_owner:
2763 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 return status;
2766}
2767
2768
Andy Adamson82be4172012-05-23 05:02:35 -04002769static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002770 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002771 int flags,
2772 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002773 struct nfs4_label *label,
2774 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002776 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 struct nfs4_exception exception = { };
2778 struct nfs4_state *res;
2779 int status;
2780
2781 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002782 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002783 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002784 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 if (status == 0)
2786 break;
2787 /* NOTE: BAD_SEQID means the server and client disagree about the
2788 * book-keeping w.r.t. state-changing operations
2789 * (OPEN/CLOSE/LOCK/LOCKU...)
2790 * It is actually a sign of a bug on the client or on the server.
2791 *
2792 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002793 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 * have unhashed the old state_owner for us, and that we can
2795 * therefore safely retry using a new one. We should still warn
2796 * the user though...
2797 */
2798 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002799 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002800 " returned a bad sequence-id error!\n",
2801 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 exception.retry = 1;
2803 continue;
2804 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002805 /*
2806 * BAD_STATEID on OPEN means that the server cancelled our
2807 * state before it received the OPEN_CONFIRM.
2808 * Recover by retrying the request as per the discussion
2809 * on Page 181 of RFC3530.
2810 */
2811 if (status == -NFS4ERR_BAD_STATEID) {
2812 exception.retry = 1;
2813 continue;
2814 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002815 if (status == -EAGAIN) {
2816 /* We must have found a delegation */
2817 exception.retry = 1;
2818 continue;
2819 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002820 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2821 continue;
2822 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 status, &exception));
2824 } while (exception.retry);
2825 return res;
2826}
2827
Trond Myklebust8487c472016-06-26 08:44:35 -04002828static int _nfs4_do_setattr(struct inode *inode,
2829 struct nfs_setattrargs *arg,
2830 struct nfs_setattrres *res,
2831 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002832 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002833{
2834 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002835 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04002836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2837 .rpc_argp = arg,
2838 .rpc_resp = res,
2839 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002840 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002841 struct rpc_cred *delegation_cred = NULL;
2842 unsigned long timestamp = jiffies;
2843 fmode_t fmode;
2844 bool truncate;
2845 int status;
2846
2847 nfs_fattr_init(res->fattr);
2848
2849 /* Servers should only apply open mode checks for file size changes */
2850 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2851 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2852
2853 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2854 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002855 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002856 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002857 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002858 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002859 l_ctx = nfs_get_lock_context(ctx);
2860 if (IS_ERR(l_ctx))
2861 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002862 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2863 &arg->stateid, &delegation_cred);
2864 nfs_put_lock_context(l_ctx);
2865 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002866 return -EBADF;
2867 } else
2868 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2869 if (delegation_cred)
2870 msg.rpc_cred = delegation_cred;
2871
2872 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2873
2874 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002875 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002876 renew_lease(server, timestamp);
2877 trace_nfs4_setattr(inode, &arg->stateid, status);
2878 return status;
2879}
2880
2881static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2882 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002883 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002884 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002886 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002887 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002888 struct nfs_setattrargs arg = {
2889 .fh = NFS_FH(inode),
2890 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 .server = server,
2892 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002893 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002894 };
2895 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002897 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002899 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002900 struct nfs4_exception exception = {
2901 .state = state,
2902 .inode = inode,
2903 .stateid = &arg.stateid,
2904 };
2905 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
David Quigleyaa9c2662013-05-22 12:50:44 -04002907 arg.bitmask = nfs4_bitmask(server, ilabel);
2908 if (ilabel)
2909 arg.bitmask = nfs4_bitmask(server, olabel);
2910
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 do {
NeilBrown29b59f92016-10-13 15:26:47 +11002912 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04002913 switch (err) {
2914 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002915 if (!(sattr->ia_valid & ATTR_SIZE)) {
2916 pr_warn_once("NFSv4: server %s is incorrectly "
2917 "applying open mode checks to "
2918 "a SETATTR that is not "
2919 "changing file size.\n",
2920 server->nfs_client->cl_hostname);
2921 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002922 if (state && !(state->state & FMODE_WRITE)) {
2923 err = -EBADF;
2924 if (sattr->ia_valid & ATTR_OPEN)
2925 err = -EACCES;
2926 goto out;
2927 }
2928 }
2929 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002931out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 return err;
2933}
2934
Peng Tao500d7012015-09-22 11:35:22 +08002935static bool
2936nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2937{
2938 if (inode == NULL || !nfs_have_layout(inode))
2939 return false;
2940
2941 return pnfs_wait_on_layoutreturn(inode, task);
2942}
2943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944struct nfs4_closedata {
2945 struct inode *inode;
2946 struct nfs4_state *state;
2947 struct nfs_closeargs arg;
2948 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05002949 struct {
2950 struct nfs4_layoutreturn_args arg;
2951 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04002952 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05002953 u32 roc_barrier;
2954 bool roc;
2955 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002956 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002957 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958};
2959
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002960static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002961{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002962 struct nfs4_closedata *calldata = data;
2963 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002964 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002965
Trond Myklebustcf805162016-11-15 14:56:07 -05002966 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05002967 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
2968 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07002969 nfs4_put_open_state(calldata->state);
2970 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002971 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002972 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002973 kfree(calldata);
2974}
2975
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002976static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002978 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002981 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
Chuck Levera3ca5652012-03-01 17:00:40 -05002983 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002984 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2985 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002986 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05002987
2988 /* Handle Layoutreturn errors */
2989 if (calldata->arg.lr_args && task->tk_status != 0) {
2990 switch (calldata->res.lr_ret) {
2991 default:
2992 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
2993 break;
2994 case 0:
2995 calldata->arg.lr_args = NULL;
2996 calldata->res.lr_res = NULL;
2997 break;
2998 case -NFS4ERR_ADMIN_REVOKED:
2999 case -NFS4ERR_DELEG_REVOKED:
3000 case -NFS4ERR_EXPIRED:
3001 case -NFS4ERR_BAD_STATEID:
3002 case -NFS4ERR_OLD_STATEID:
3003 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3004 case -NFS4ERR_WRONG_CRED:
3005 calldata->arg.lr_args = NULL;
3006 calldata->res.lr_res = NULL;
3007 calldata->res.lr_ret = 0;
3008 rpc_restart_call_prepare(task);
3009 return;
3010 }
3011 }
3012
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003013 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 * the state_owner. we keep this around to process errors
3015 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 switch (task->tk_status) {
3017 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003018 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003019 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003020 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003021 case -NFS4ERR_ACCESS:
3022 if (calldata->arg.bitmask != NULL) {
3023 calldata->arg.bitmask = NULL;
3024 calldata->res.fattr = NULL;
3025 task->tk_status = 0;
3026 rpc_restart_call_prepare(task);
3027 goto out_release;
3028
3029 }
3030 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003031 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003033 case -NFS4ERR_EXPIRED:
3034 nfs4_free_revoked_stateid(server,
3035 &calldata->arg.stateid,
3036 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003037 case -NFS4ERR_OLD_STATEID:
3038 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003039 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003040 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003041 rpc_restart_call_prepare(task);
3042 goto out_release;
3043 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003044 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003045 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003047 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003048 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003049 goto out_release;
3050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003052 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3053 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003054out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003055 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003056 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003057 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058}
3059
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003060static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003062 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003063 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003064 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003065 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003066 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003067
Chuck Levera3ca5652012-03-01 17:00:40 -05003068 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003069 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003070 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003071
Trond Myklebust88069f72009-12-08 08:33:16 -05003072 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003073 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003074 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3075 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3076 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003077 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003078 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003079 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003080 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003081 if (state->n_rdonly == 0)
3082 call_close |= is_rdonly;
3083 else if (is_rdonly)
3084 calldata->arg.fmode |= FMODE_READ;
3085 if (state->n_wronly == 0)
3086 call_close |= is_wronly;
3087 else if (is_wronly)
3088 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003089 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3090 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003091 } else if (is_rdwr)
3092 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3093
Trond Myklebust5cc78612016-11-14 11:19:56 -05003094 if (!nfs4_valid_open_stateid(state) ||
3095 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003096 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003097 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003098
3099 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003100 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003101 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003102 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003103
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003104 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003105 nfs_release_seqid(calldata->arg.seqid);
3106 goto out_wait;
3107 }
3108
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003109 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003110 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003111
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003112 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003113 /* Close-to-open cache consistency revalidation */
3114 if (!nfs4_have_delegation(inode, FMODE_READ))
3115 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3116 else
3117 calldata->arg.bitmask = NULL;
3118 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003119
Trond Myklebust6ae37332015-01-30 14:21:14 -05003120 calldata->arg.share_access =
3121 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3122 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003123
Trond Myklebustd8d84982016-12-19 12:14:44 -05003124 if (calldata->res.fattr == NULL)
3125 calldata->arg.bitmask = NULL;
3126 else if (calldata->arg.bitmask == NULL)
3127 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003128 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003129 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003130 &calldata->arg.seq_args,
3131 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003132 task) != 0)
3133 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003134 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003135 return;
3136out_no_action:
3137 task->tk_action = NULL;
3138out_wait:
3139 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140}
3141
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003142static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003143 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003144 .rpc_call_done = nfs4_close_done,
3145 .rpc_release = nfs4_free_closedata,
3146};
3147
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148/*
3149 * It is possible for data to be read/written from a mem-mapped file
3150 * after the sys_close call (which hits the vfs layer as a flush).
3151 * This means that we can't safely call nfsv4 close on a file until
3152 * the inode is cleared. This in turn means that we are not good
3153 * NFSv4 citizens - we do not indicate to the server to update the file's
3154 * share state even when we are done with one of the three share
3155 * stateid's in the inode.
3156 *
3157 * NOTE: Caller must be holding the sp->so_owner semaphore!
3158 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003159int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003161 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003162 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003164 struct nfs4_state_owner *sp = state->owner;
3165 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003166 struct rpc_message msg = {
3167 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3168 .rpc_cred = state->owner->so_cred,
3169 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003170 struct rpc_task_setup task_setup_data = {
3171 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003172 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003173 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003174 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003175 .flags = RPC_TASK_ASYNC,
3176 };
Trond Myklebust95121352005-10-18 14:20:12 -07003177 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003179 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3180 &task_setup_data.rpc_client, &msg);
3181
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003182 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003184 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003185 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003186 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003188 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003190 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3191 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003192 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003193 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003194 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003195 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003196 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003197 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003198 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003199 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003200 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003201 calldata->lr.roc = pnfs_roc(state->inode,
3202 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3203 if (calldata->lr.roc) {
3204 calldata->arg.lr_args = &calldata->lr.arg;
3205 calldata->res.lr_res = &calldata->lr.res;
3206 }
Al Viro643168c2011-06-22 18:20:23 -04003207 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003208
Trond Myklebust1174dd12010-12-21 10:52:24 -05003209 msg.rpc_argp = &calldata->arg;
3210 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003211 task_setup_data.callback_data = calldata;
3212 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003213 if (IS_ERR(task))
3214 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003215 status = 0;
3216 if (wait)
3217 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003218 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003219 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003220out_free_calldata:
3221 kfree(calldata);
3222out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003223 nfs4_put_open_state(state);
3224 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003225 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226}
3227
Trond Myklebust2b484292010-09-17 10:56:51 -04003228static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003229nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3230 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003233 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3234
3235 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Trond Myklebust565277f2007-10-15 18:17:53 -04003237 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003238 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003239
3240 nfs4_label_release_security(label);
3241
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003242 if (IS_ERR(state))
3243 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003244 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245}
3246
Trond Myklebust1185a552009-12-03 15:54:02 -05003247static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003248{
3249 if (ctx->state == NULL)
3250 return;
3251 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003252 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003253 else
Al Viro643168c2011-06-22 18:20:23 -04003254 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003255}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
Trond Myklebustb944dba2013-11-04 15:20:20 -05003257#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3258#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003259#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003260
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3262{
Kinglong Mee8c612822015-08-26 21:12:58 +08003263 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003264 struct nfs4_server_caps_arg args = {
3265 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003266 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003267 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 struct nfs4_server_caps_res res = {};
3269 struct rpc_message msg = {
3270 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003271 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 .rpc_resp = &res,
3273 };
3274 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003275 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
Kinglong Mee8c612822015-08-26 21:12:58 +08003277 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3278 FATTR4_WORD0_FH_EXPIRE_TYPE |
3279 FATTR4_WORD0_LINK_SUPPORT |
3280 FATTR4_WORD0_SYMLINK_SUPPORT |
3281 FATTR4_WORD0_ACLSUPPORT;
3282 if (minorversion)
3283 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3284
Bryan Schumaker7c513052011-03-24 17:12:24 +00003285 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003287 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003288 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003289 case 0:
3290 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3291 res.attr_bitmask[2] = 0;
3292 break;
3293 case 1:
3294 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3295 break;
3296 case 2:
3297 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003300 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3301 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3302 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3303 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003304 NFS_CAP_CTIME|NFS_CAP_MTIME|
3305 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003306 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3307 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 server->caps |= NFS_CAP_ACLS;
3309 if (res.has_links != 0)
3310 server->caps |= NFS_CAP_HARDLINKS;
3311 if (res.has_symlinks != 0)
3312 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003313 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3314 server->caps |= NFS_CAP_FILEID;
3315 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3316 server->caps |= NFS_CAP_MODE;
3317 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3318 server->caps |= NFS_CAP_NLINK;
3319 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3320 server->caps |= NFS_CAP_OWNER;
3321 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3322 server->caps |= NFS_CAP_OWNER_GROUP;
3323 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3324 server->caps |= NFS_CAP_ATIME;
3325 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3326 server->caps |= NFS_CAP_CTIME;
3327 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3328 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003329#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3330 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3331 server->caps |= NFS_CAP_SECURITY_LABEL;
3332#endif
3333 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3334 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003335 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003336
Trond Myklebusta65318b2009-03-11 14:10:28 -04003337 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3338 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3339 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003340 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003341
3342 /* Avoid a regression due to buggy server */
3343 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3344 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003345 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3346 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003347
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003349 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003351
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 return status;
3353}
3354
Trond Myklebust55a97592006-06-09 09:34:19 -04003355int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356{
3357 struct nfs4_exception exception = { };
3358 int err;
3359 do {
3360 err = nfs4_handle_exception(server,
3361 _nfs4_server_capabilities(server, fhandle),
3362 &exception);
3363 } while (exception.retry);
3364 return err;
3365}
3366
3367static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3368 struct nfs_fsinfo *info)
3369{
David Quigleyaa9c2662013-05-22 12:50:44 -04003370 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003372 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 };
3374 struct nfs4_lookup_res res = {
3375 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003376 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 .fh = fhandle,
3378 };
3379 struct rpc_message msg = {
3380 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3381 .rpc_argp = &args,
3382 .rpc_resp = &res,
3383 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003384
David Quigleyaa9c2662013-05-22 12:50:44 -04003385 bitmask[0] = nfs4_fattr_bitmap[0];
3386 bitmask[1] = nfs4_fattr_bitmap[1];
3387 /*
3388 * Process the label in the upcoming getfattr
3389 */
3390 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3391
Trond Myklebust0e574af2005-10-27 22:12:38 -04003392 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003393 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394}
3395
3396static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3397 struct nfs_fsinfo *info)
3398{
3399 struct nfs4_exception exception = { };
3400 int err;
3401 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003402 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003403 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003404 switch (err) {
3405 case 0:
3406 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003407 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003408 default:
3409 err = nfs4_handle_exception(server, err, &exception);
3410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003412out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 return err;
3414}
3415
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003416static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3417 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3418{
Trond Myklebustc2190662013-08-26 19:23:04 -04003419 struct rpc_auth_create_args auth_args = {
3420 .pseudoflavor = flavor,
3421 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003422 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003423
Trond Myklebustc2190662013-08-26 19:23:04 -04003424 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003425 if (IS_ERR(auth))
3426 return -EACCES;
3427 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003428}
3429
Chuck Lever9a744ba2013-03-16 15:56:02 -04003430/*
3431 * Retry pseudoroot lookup with various security flavors. We do this when:
3432 *
3433 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3434 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3435 *
3436 * Returns zero on success, or a negative NFS4ERR value, or a
3437 * negative errno value.
3438 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003439static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003440 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003442 /* Per 3530bis 15.33.5 */
3443 static const rpc_authflavor_t flav_array[] = {
3444 RPC_AUTH_GSS_KRB5P,
3445 RPC_AUTH_GSS_KRB5I,
3446 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003447 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003448 RPC_AUTH_NULL,
3449 };
3450 int status = -EPERM;
3451 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003453 if (server->auth_info.flavor_len > 0) {
3454 /* try each flavor specified by user */
3455 for (i = 0; i < server->auth_info.flavor_len; i++) {
3456 status = nfs4_lookup_root_sec(server, fhandle, info,
3457 server->auth_info.flavors[i]);
3458 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3459 continue;
3460 break;
3461 }
3462 } else {
3463 /* no flavors specified by user, try default list */
3464 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3465 status = nfs4_lookup_root_sec(server, fhandle, info,
3466 flav_array[i]);
3467 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3468 continue;
3469 break;
3470 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003471 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003472
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003473 /*
3474 * -EACCESS could mean that the user doesn't have correct permissions
3475 * to access the mount. It could also mean that we tried to mount
3476 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3477 * existing mount programs don't handle -EACCES very well so it should
3478 * be mapped to -EPERM instead.
3479 */
3480 if (status == -EACCES)
3481 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003482 return status;
3483}
3484
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003485/**
3486 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3487 * @server: initialized nfs_server handle
3488 * @fhandle: we fill in the pseudo-fs root file handle
3489 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003490 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003491 *
3492 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003493 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003494int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003495 struct nfs_fsinfo *info,
3496 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003497{
Andre Przywarac7757072015-04-23 17:17:40 +01003498 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003499
Andre Przywarac7757072015-04-23 17:17:40 +01003500 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003501 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003502
3503 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003504 status = server->nfs_client->cl_mvops->find_root_sec(server,
3505 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003506
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 if (status == 0)
3508 status = nfs4_server_capabilities(server, fhandle);
3509 if (status == 0)
3510 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003511
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003512 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513}
3514
Bryan Schumakerbae36242012-05-10 15:07:31 -04003515static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3516 struct nfs_fsinfo *info)
3517{
3518 int error;
3519 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003520 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003521
3522 error = nfs4_server_capabilities(server, mntfh);
3523 if (error < 0) {
3524 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3525 return error;
3526 }
3527
David Quigley14c43f72013-05-22 12:50:43 -04003528 label = nfs4_label_alloc(server, GFP_KERNEL);
3529 if (IS_ERR(label))
3530 return PTR_ERR(label);
3531
David Quigley1775fd32013-05-22 12:50:42 -04003532 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003533 if (error < 0) {
3534 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003535 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003536 }
3537
3538 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3539 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3540 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3541
David Quigley14c43f72013-05-22 12:50:43 -04003542err_free_label:
3543 nfs4_label_free(label);
3544
Bryan Schumakerbae36242012-05-10 15:07:31 -04003545 return error;
3546}
3547
Manoj Naik6b97fd32006-06-09 09:34:29 -04003548/*
3549 * Get locations and (maybe) other attributes of a referral.
3550 * Note that we'll actually follow the referral later when
3551 * we detect fsid mismatch in inode revalidation
3552 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003553static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3554 const struct qstr *name, struct nfs_fattr *fattr,
3555 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003556{
3557 int status = -ENOMEM;
3558 struct page *page = NULL;
3559 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003560
3561 page = alloc_page(GFP_KERNEL);
3562 if (page == NULL)
3563 goto out;
3564 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3565 if (locations == NULL)
3566 goto out;
3567
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003568 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003569 if (status != 0)
3570 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003571
3572 /*
3573 * If the fsid didn't change, this is a migration event, not a
3574 * referral. Cause us to drop into the exception handler, which
3575 * will kick off migration recovery.
3576 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003577 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003578 dprintk("%s: server did not return a different fsid for"
3579 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003580 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003581 goto out;
3582 }
Andy Adamson533eb462011-06-13 18:25:56 -04003583 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3584 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003585
Andy Adamson533eb462011-06-13 18:25:56 -04003586 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003587 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003588 memset(fhandle, 0, sizeof(struct nfs_fh));
3589out:
3590 if (page)
3591 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003592 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003593 return status;
3594}
3595
David Quigley1775fd32013-05-22 12:50:42 -04003596static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3597 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598{
3599 struct nfs4_getattr_arg args = {
3600 .fh = fhandle,
3601 .bitmask = server->attr_bitmask,
3602 };
3603 struct nfs4_getattr_res res = {
3604 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003605 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 .server = server,
3607 };
3608 struct rpc_message msg = {
3609 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3610 .rpc_argp = &args,
3611 .rpc_resp = &res,
3612 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003613
3614 args.bitmask = nfs4_bitmask(server, label);
3615
Trond Myklebust0e574af2005-10-27 22:12:38 -04003616 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003617 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618}
3619
David Quigley1775fd32013-05-22 12:50:42 -04003620static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3621 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622{
3623 struct nfs4_exception exception = { };
3624 int err;
3625 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003626 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3627 trace_nfs4_getattr(server, fhandle, fattr, err);
3628 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 &exception);
3630 } while (exception.retry);
3631 return err;
3632}
3633
3634/*
3635 * The file is not closed if it is opened due to the a request to change
3636 * the size of the file. The open call will not be needed once the
3637 * VFS layer lookup-intents are implemented.
3638 *
3639 * Close is called when the inode is destroyed.
3640 * If we haven't opened the file for O_WRONLY, we
3641 * need to in the size_change case to obtain a stateid.
3642 *
3643 * Got race?
3644 * Because OPEN is always done by name in nfsv4, it is
3645 * possible that we opened a different file by the same
3646 * name. We can recognize this race condition, but we
3647 * can't do anything about it besides returning an error.
3648 *
3649 * This will be fixed with VFS changes (lookup-intent).
3650 */
3651static int
3652nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3653 struct iattr *sattr)
3654{
David Howells2b0143b2015-03-17 22:25:59 +00003655 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003656 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003657 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003658 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 int status;
3660
Peng Tao88ac8152014-09-12 11:04:10 +08003661 if (pnfs_ld_layoutret_on_setattr(inode) &&
3662 sattr->ia_valid & ATTR_SIZE &&
3663 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003664 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003665
Trond Myklebust0e574af2005-10-27 22:12:38 -04003666 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
Andy Adamson26699402012-05-30 16:12:24 -04003668 /* Deal with open(O_TRUNC) */
3669 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003670 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003671
3672 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003673 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003674 return 0;
3675
Trond Myklebustd5308382005-11-04 15:33:38 -05003676 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003677 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003678
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003679 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003680 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003681 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003682 }
3683
David Quigley14c43f72013-05-22 12:50:43 -04003684 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3685 if (IS_ERR(label))
3686 return PTR_ERR(label);
3687
NeilBrown29b59f92016-10-13 15:26:47 +11003688 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003689 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003690 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003691 nfs_setsecurity(inode, fattr, label);
3692 }
David Quigley14c43f72013-05-22 12:50:43 -04003693 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 return status;
3695}
3696
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003697static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3698 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003699 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003700{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003701 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003702 int status;
3703 struct nfs4_lookup_arg args = {
3704 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003705 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003706 .name = name,
3707 };
3708 struct nfs4_lookup_res res = {
3709 .server = server,
3710 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003711 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003712 .fh = fhandle,
3713 };
3714 struct rpc_message msg = {
3715 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3716 .rpc_argp = &args,
3717 .rpc_resp = &res,
3718 };
3719
David Quigleyaa9c2662013-05-22 12:50:44 -04003720 args.bitmask = nfs4_bitmask(server, label);
3721
David Howells2b3de442006-08-22 20:06:09 -04003722 nfs_fattr_init(fattr);
3723
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003725 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 dprintk("NFS reply lookup: %d\n", status);
3727 return status;
3728}
3729
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003730static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003731{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003732 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003733 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003734 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3735 fattr->nlink = 2;
3736}
3737
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003738static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003739 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003740 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003741{
3742 struct nfs4_exception exception = { };
3743 struct rpc_clnt *client = *clnt;
3744 int err;
3745 do {
David Quigley1775fd32013-05-22 12:50:42 -04003746 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003747 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003748 switch (err) {
3749 case -NFS4ERR_BADNAME:
3750 err = -ENOENT;
3751 goto out;
3752 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003753 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003754 if (err == -NFS4ERR_MOVED)
3755 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003756 goto out;
3757 case -NFS4ERR_WRONGSEC:
3758 err = -EPERM;
3759 if (client != *clnt)
3760 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003761 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003762 if (IS_ERR(client))
3763 return PTR_ERR(client);
3764
3765 exception.retry = 1;
3766 break;
3767 default:
3768 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3769 }
3770 } while (exception.retry);
3771
3772out:
3773 if (err == 0)
3774 *clnt = client;
3775 else if (client != *clnt)
3776 rpc_shutdown_client(client);
3777
3778 return err;
3779}
3780
Al Virobeffb8f2016-07-20 16:34:42 -04003781static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003782 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3783 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003785 int status;
3786 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003787
David Quigley1775fd32013-05-22 12:50:42 -04003788 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003789 if (client != NFS_CLIENT(dir)) {
3790 rpc_shutdown_client(client);
3791 nfs_fixup_secinfo_attributes(fattr);
3792 }
3793 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794}
3795
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003796struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003797nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003798 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3799{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003800 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003801 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003802
David Quigley1775fd32013-05-22 12:50:42 -04003803 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003804 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003805 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003806 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003807}
3808
Jeff Layton5b5faaf2017-06-29 06:34:52 -07003809static int _nfs4_proc_lookupp(struct inode *inode,
3810 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3811 struct nfs4_label *label)
3812{
3813 struct rpc_clnt *clnt = NFS_CLIENT(inode);
3814 struct nfs_server *server = NFS_SERVER(inode);
3815 int status;
3816 struct nfs4_lookupp_arg args = {
3817 .bitmask = server->attr_bitmask,
3818 .fh = NFS_FH(inode),
3819 };
3820 struct nfs4_lookupp_res res = {
3821 .server = server,
3822 .fattr = fattr,
3823 .label = label,
3824 .fh = fhandle,
3825 };
3826 struct rpc_message msg = {
3827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
3828 .rpc_argp = &args,
3829 .rpc_resp = &res,
3830 };
3831
3832 args.bitmask = nfs4_bitmask(server, label);
3833
3834 nfs_fattr_init(fattr);
3835
3836 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
3837 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
3838 &res.seq_res, 0);
3839 dprintk("NFS reply lookupp: %d\n", status);
3840 return status;
3841}
3842
3843static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
3844 struct nfs_fattr *fattr, struct nfs4_label *label)
3845{
3846 struct nfs4_exception exception = { };
3847 int err;
3848 do {
3849 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
3850 trace_nfs4_lookupp(inode, err);
3851 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3852 &exception);
3853 } while (exception.retry);
3854 return err;
3855}
3856
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3858{
Trond Myklebust76b32992007-08-10 17:45:11 -04003859 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 struct nfs4_accessargs args = {
3861 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003862 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003864 struct nfs4_accessres res = {
3865 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003866 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 struct rpc_message msg = {
3868 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3869 .rpc_argp = &args,
3870 .rpc_resp = &res,
3871 .rpc_cred = entry->cred,
3872 };
3873 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003874 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
3876 /*
3877 * Determine which access bits we want to ask for...
3878 */
3879 if (mode & MAY_READ)
3880 args.access |= NFS4_ACCESS_READ;
3881 if (S_ISDIR(inode->i_mode)) {
3882 if (mode & MAY_WRITE)
3883 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3884 if (mode & MAY_EXEC)
3885 args.access |= NFS4_ACCESS_LOOKUP;
3886 } else {
3887 if (mode & MAY_WRITE)
3888 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3889 if (mode & MAY_EXEC)
3890 args.access |= NFS4_ACCESS_EXECUTE;
3891 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003892
3893 res.fattr = nfs_alloc_fattr();
3894 if (res.fattr == NULL)
3895 return -ENOMEM;
3896
Bryan Schumaker7c513052011-03-24 17:12:24 +00003897 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003899 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003900 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003902 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 return status;
3904}
3905
3906static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3907{
3908 struct nfs4_exception exception = { };
3909 int err;
3910 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003911 err = _nfs4_proc_access(inode, entry);
3912 trace_nfs4_access(inode, err);
3913 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 &exception);
3915 } while (exception.retry);
3916 return err;
3917}
3918
3919/*
3920 * TODO: For the time being, we don't try to get any attributes
3921 * along with any of the zero-copy operations READ, READDIR,
3922 * READLINK, WRITE.
3923 *
3924 * In the case of the first three, we want to put the GETATTR
3925 * after the read-type operation -- this is because it is hard
3926 * to predict the length of a GETATTR response in v4, and thus
3927 * align the READ data correctly. This means that the GETATTR
3928 * may end up partially falling into the page cache, and we should
3929 * shift it into the 'tail' of the xdr_buf before processing.
3930 * To do this efficiently, we need to know the total length
3931 * of data received, which doesn't seem to be available outside
3932 * of the RPC layer.
3933 *
3934 * In the case of WRITE, we also want to put the GETATTR after
3935 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003936 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 *
3938 * Both of these changes to the XDR layer would in fact be quite
3939 * minor, but I decided to leave them for a subsequent patch.
3940 */
3941static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3942 unsigned int pgbase, unsigned int pglen)
3943{
3944 struct nfs4_readlink args = {
3945 .fh = NFS_FH(inode),
3946 .pgbase = pgbase,
3947 .pglen = pglen,
3948 .pages = &page,
3949 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003950 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 struct rpc_message msg = {
3952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3953 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003954 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 };
3956
Bryan Schumaker7c513052011-03-24 17:12:24 +00003957 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 -07003958}
3959
3960static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3961 unsigned int pgbase, unsigned int pglen)
3962{
3963 struct nfs4_exception exception = { };
3964 int err;
3965 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003966 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3967 trace_nfs4_readlink(inode, err);
3968 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 &exception);
3970 } while (exception.retry);
3971 return err;
3972}
3973
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003975 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977static int
3978nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003979 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003981 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04003982 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003983 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 int status = 0;
3986
NeilBrown532d4de2016-10-13 15:26:47 +11003987 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003988 if (IS_ERR(ctx))
3989 return PTR_ERR(ctx);
3990
David Quigleyaa9c2662013-05-22 12:50:44 -04003991 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3992
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003993 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
3994 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003995 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 if (IS_ERR(state)) {
3997 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003998 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004001 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004002 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 return status;
4004}
4005
Al Virobeffb8f2016-07-20 16:34:42 -04004006static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007{
Trond Myklebust16e42952005-10-27 22:12:44 -04004008 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004009 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004011 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004013 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004014 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004015 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004017 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4018 .rpc_argp = &args,
4019 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004021 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004022 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004023
Bryan Schumaker7c513052011-03-24 17:12:24 +00004024 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004025 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004026 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 return status;
4028}
4029
Al Virobeffb8f2016-07-20 16:34:42 -04004030static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031{
4032 struct nfs4_exception exception = { };
4033 int err;
4034 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004035 err = _nfs4_proc_remove(dir, name);
4036 trace_nfs4_remove(dir, name, err);
4037 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 &exception);
4039 } while (exception.retry);
4040 return err;
4041}
4042
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004043static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004045 struct nfs_server *server = NFS_SERVER(dir);
4046 struct nfs_removeargs *args = msg->rpc_argp;
4047 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004049 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004051 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004052
4053 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054}
4055
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004056static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4057{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004058 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004059 &data->args.seq_args,
4060 &data->res.seq_res,
4061 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062}
4063
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004064static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004066 struct nfs_unlinkdata *data = task->tk_calldata;
4067 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004068
Trond Myklebust14516c32010-07-31 14:29:06 -04004069 if (!nfs4_sequence_done(task, &res->seq_res))
4070 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004071 if (nfs4_async_handle_error(task, res->server, NULL,
4072 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004073 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004074 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004075 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004076 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077}
4078
Jeff Laytond3d41522010-09-17 17:31:57 -04004079static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4080{
4081 struct nfs_server *server = NFS_SERVER(dir);
4082 struct nfs_renameargs *arg = msg->rpc_argp;
4083 struct nfs_renameres *res = msg->rpc_resp;
4084
4085 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004086 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004087 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004088}
4089
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004090static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4091{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004092 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004093 &data->args.seq_args,
4094 &data->res.seq_res,
4095 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004096}
4097
4098static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4099 struct inode *new_dir)
4100{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004101 struct nfs_renamedata *data = task->tk_calldata;
4102 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004103
4104 if (!nfs4_sequence_done(task, &res->seq_res))
4105 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004106 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004107 return 0;
4108
Trond Myklebustc733c492017-01-11 12:32:26 -05004109 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004110 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004111 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004112 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004113 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004114 return 1;
4115}
4116
Al Virobeffb8f2016-07-20 16:34:42 -04004117static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004119 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 struct nfs4_link_arg arg = {
4121 .fh = NFS_FH(inode),
4122 .dir_fh = NFS_FH(dir),
4123 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004124 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004126 struct nfs4_link_res res = {
4127 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004128 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004129 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 struct rpc_message msg = {
4131 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4132 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004133 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004135 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136
Trond Myklebust136f2622010-04-16 16:22:49 -04004137 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004138 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004139 goto out;
4140
David Quigley14c43f72013-05-22 12:50:43 -04004141 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4142 if (IS_ERR(res.label)) {
4143 status = PTR_ERR(res.label);
4144 goto out;
4145 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004146 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004147
Bryan Schumaker7c513052011-03-24 17:12:24 +00004148 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004149 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004150 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004151 status = nfs_post_op_update_inode(inode, res.fattr);
4152 if (!status)
4153 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004154 }
David Quigley14c43f72013-05-22 12:50:43 -04004155
4156
4157 nfs4_label_free(res.label);
4158
Trond Myklebust136f2622010-04-16 16:22:49 -04004159out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004160 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 return status;
4162}
4163
Al Virobeffb8f2016-07-20 16:34:42 -04004164static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165{
4166 struct nfs4_exception exception = { };
4167 int err;
4168 do {
4169 err = nfs4_handle_exception(NFS_SERVER(inode),
4170 _nfs4_proc_link(inode, dir, name),
4171 &exception);
4172 } while (exception.retry);
4173 return err;
4174}
4175
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004176struct nfs4_createdata {
4177 struct rpc_message msg;
4178 struct nfs4_create_arg arg;
4179 struct nfs4_create_res res;
4180 struct nfs_fh fh;
4181 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004182 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004183};
4184
4185static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004186 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004187{
4188 struct nfs4_createdata *data;
4189
4190 data = kzalloc(sizeof(*data), GFP_KERNEL);
4191 if (data != NULL) {
4192 struct nfs_server *server = NFS_SERVER(dir);
4193
David Quigley14c43f72013-05-22 12:50:43 -04004194 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4195 if (IS_ERR(data->label))
4196 goto out_free;
4197
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004198 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4199 data->msg.rpc_argp = &data->arg;
4200 data->msg.rpc_resp = &data->res;
4201 data->arg.dir_fh = NFS_FH(dir);
4202 data->arg.server = server;
4203 data->arg.name = name;
4204 data->arg.attrs = sattr;
4205 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004206 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004207 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004208 data->res.server = server;
4209 data->res.fh = &data->fh;
4210 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004211 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004212 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004213 }
4214 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004215out_free:
4216 kfree(data);
4217 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004218}
4219
4220static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4221{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004222 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004223 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004224 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004225 update_changeattr(dir, &data->res.dir_cinfo,
4226 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004227 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004228 }
4229 return status;
4230}
4231
4232static void nfs4_free_createdata(struct nfs4_createdata *data)
4233{
David Quigley14c43f72013-05-22 12:50:43 -04004234 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004235 kfree(data);
4236}
4237
Chuck Lever4f390c12006-08-22 20:06:22 -04004238static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004239 struct page *page, unsigned int len, struct iattr *sattr,
4240 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004242 struct nfs4_createdata *data;
4243 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244
Chuck Lever94a6d752006-08-22 20:06:23 -04004245 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004246 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004247
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004248 status = -ENOMEM;
4249 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4250 if (data == NULL)
4251 goto out;
4252
4253 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4254 data->arg.u.symlink.pages = &page;
4255 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004256 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004258 status = nfs4_do_create(dir, dentry, data);
4259
4260 nfs4_free_createdata(data);
4261out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 return status;
4263}
4264
Chuck Lever4f390c12006-08-22 20:06:22 -04004265static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004266 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267{
4268 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004269 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004271
4272 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4273
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004275 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4276 trace_nfs4_symlink(dir, &dentry->d_name, err);
4277 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 &exception);
4279 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004280
4281 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 return err;
4283}
4284
4285static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004286 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004288 struct nfs4_createdata *data;
4289 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004291 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4292 if (data == NULL)
4293 goto out;
4294
David Quigley1775fd32013-05-22 12:50:42 -04004295 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004296 status = nfs4_do_create(dir, dentry, data);
4297
4298 nfs4_free_createdata(data);
4299out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 return status;
4301}
4302
4303static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4304 struct iattr *sattr)
4305{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004306 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004308 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004310
David Quigleyaa9c2662013-05-22 12:50:44 -04004311 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4312
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004313 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4314 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004316 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4317 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4318 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 &exception);
4320 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004321 nfs4_label_release_security(label);
4322
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 return err;
4324}
4325
4326static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004327 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328{
David Howells2b0143b2015-03-17 22:25:59 +00004329 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 struct nfs4_readdir_arg args = {
4331 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004332 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 .pgbase = 0,
4334 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004335 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004336 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 };
4338 struct nfs4_readdir_res res;
4339 struct rpc_message msg = {
4340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4341 .rpc_argp = &args,
4342 .rpc_resp = &res,
4343 .rpc_cred = cred,
4344 };
4345 int status;
4346
Al Viro6de14722013-09-16 10:53:17 -04004347 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4348 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004349 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004350 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004352 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 -05004353 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004354 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004355 status += args.pgbase;
4356 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004357
4358 nfs_invalidate_atime(dir);
4359
Harvey Harrison3110ff82008-05-02 13:42:44 -07004360 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 return status;
4362}
4363
4364static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004365 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366{
4367 struct nfs4_exception exception = { };
4368 int err;
4369 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004370 err = _nfs4_proc_readdir(dentry, cred, cookie,
4371 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004372 trace_nfs4_readdir(d_inode(dentry), err);
4373 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 &exception);
4375 } while (exception.retry);
4376 return err;
4377}
4378
4379static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004380 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004382 struct nfs4_createdata *data;
4383 int mode = sattr->ia_mode;
4384 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004386 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4387 if (data == NULL)
4388 goto out;
4389
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004391 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004393 data->arg.ftype = NF4BLK;
4394 data->arg.u.device.specdata1 = MAJOR(rdev);
4395 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 }
4397 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004398 data->arg.ftype = NF4CHR;
4399 data->arg.u.device.specdata1 = MAJOR(rdev);
4400 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004401 } else if (!S_ISSOCK(mode)) {
4402 status = -EINVAL;
4403 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 }
David Quigley1775fd32013-05-22 12:50:42 -04004405
David Quigleyaa9c2662013-05-22 12:50:44 -04004406 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004407 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004408out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004409 nfs4_free_createdata(data);
4410out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 return status;
4412}
4413
4414static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4415 struct iattr *sattr, dev_t rdev)
4416{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004417 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004419 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004421
David Quigleyaa9c2662013-05-22 12:50:44 -04004422 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4423
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004424 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4425 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004427 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4428 trace_nfs4_mknod(dir, &dentry->d_name, err);
4429 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 &exception);
4431 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004432
4433 nfs4_label_release_security(label);
4434
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 return err;
4436}
4437
4438static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4439 struct nfs_fsstat *fsstat)
4440{
4441 struct nfs4_statfs_arg args = {
4442 .fh = fhandle,
4443 .bitmask = server->attr_bitmask,
4444 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004445 struct nfs4_statfs_res res = {
4446 .fsstat = fsstat,
4447 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 struct rpc_message msg = {
4449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4450 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004451 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 };
4453
Trond Myklebust0e574af2005-10-27 22:12:38 -04004454 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004455 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456}
4457
4458static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4459{
4460 struct nfs4_exception exception = { };
4461 int err;
4462 do {
4463 err = nfs4_handle_exception(server,
4464 _nfs4_proc_statfs(server, fhandle, fsstat),
4465 &exception);
4466 } while (exception.retry);
4467 return err;
4468}
4469
4470static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4471 struct nfs_fsinfo *fsinfo)
4472{
4473 struct nfs4_fsinfo_arg args = {
4474 .fh = fhandle,
4475 .bitmask = server->attr_bitmask,
4476 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004477 struct nfs4_fsinfo_res res = {
4478 .fsinfo = fsinfo,
4479 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 struct rpc_message msg = {
4481 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4482 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004483 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 };
4485
Bryan Schumaker7c513052011-03-24 17:12:24 +00004486 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487}
4488
4489static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4490{
4491 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004492 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 int err;
4494
4495 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004496 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004497 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004498 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004499 nfs4_set_lease_period(server->nfs_client,
4500 fsinfo->lease_time * HZ,
4501 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004502 break;
4503 }
4504 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 } while (exception.retry);
4506 return err;
4507}
4508
4509static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4510{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004511 int error;
4512
Trond Myklebust0e574af2005-10-27 22:12:38 -04004513 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004514 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004515 if (error == 0) {
4516 /* block layout checks this! */
4517 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004518 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004519 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004520
4521 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522}
4523
4524static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4525 struct nfs_pathconf *pathconf)
4526{
4527 struct nfs4_pathconf_arg args = {
4528 .fh = fhandle,
4529 .bitmask = server->attr_bitmask,
4530 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004531 struct nfs4_pathconf_res res = {
4532 .pathconf = pathconf,
4533 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 struct rpc_message msg = {
4535 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4536 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004537 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 };
4539
4540 /* None of the pathconf attributes are mandatory to implement */
4541 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4542 memset(pathconf, 0, sizeof(*pathconf));
4543 return 0;
4544 }
4545
Trond Myklebust0e574af2005-10-27 22:12:38 -04004546 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004547 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548}
4549
4550static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4551 struct nfs_pathconf *pathconf)
4552{
4553 struct nfs4_exception exception = { };
4554 int err;
4555
4556 do {
4557 err = nfs4_handle_exception(server,
4558 _nfs4_proc_pathconf(server, fhandle, pathconf),
4559 &exception);
4560 } while (exception.retry);
4561 return err;
4562}
4563
Trond Myklebust5521abf2013-03-16 20:54:34 -04004564int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004565 const struct nfs_open_context *ctx,
4566 const struct nfs_lock_context *l_ctx,
4567 fmode_t fmode)
4568{
NeilBrown17393472016-10-13 15:26:47 +11004569 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004570}
4571EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4572
Trond Myklebust5521abf2013-03-16 20:54:34 -04004573static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4574 const struct nfs_open_context *ctx,
4575 const struct nfs_lock_context *l_ctx,
4576 fmode_t fmode)
4577{
4578 nfs4_stateid current_stateid;
4579
Trond Myklebuste1253be2014-03-05 08:44:23 -05004580 /* If the current stateid represents a lost lock, then exit */
4581 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4582 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004583 return nfs4_stateid_match(stateid, &current_stateid);
4584}
4585
4586static bool nfs4_error_stateid_expired(int err)
4587{
4588 switch (err) {
4589 case -NFS4ERR_DELEG_REVOKED:
4590 case -NFS4ERR_ADMIN_REVOKED:
4591 case -NFS4ERR_BAD_STATEID:
4592 case -NFS4ERR_STALE_STATEID:
4593 case -NFS4ERR_OLD_STATEID:
4594 case -NFS4ERR_OPENMODE:
4595 case -NFS4ERR_EXPIRED:
4596 return true;
4597 }
4598 return false;
4599}
4600
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004601static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004603 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004605 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004606 if (task->tk_status < 0) {
4607 struct nfs4_exception exception = {
4608 .inode = hdr->inode,
4609 .state = hdr->args.context->state,
4610 .stateid = &hdr->args.stateid,
4611 };
4612 task->tk_status = nfs4_async_handle_exception(task,
4613 server, task->tk_status, &exception);
4614 if (exception.retry) {
4615 rpc_restart_call_prepare(task);
4616 return -EAGAIN;
4617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004619
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004621 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004622 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623}
4624
Trond Myklebust5521abf2013-03-16 20:54:34 -04004625static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004626 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004627{
4628
4629 if (!nfs4_error_stateid_expired(task->tk_status) ||
4630 nfs4_stateid_is_current(&args->stateid,
4631 args->context,
4632 args->lock_context,
4633 FMODE_READ))
4634 return false;
4635 rpc_restart_call_prepare(task);
4636 return true;
4637}
4638
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004639static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004640{
4641
4642 dprintk("--> %s\n", __func__);
4643
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004644 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004645 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004646 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004647 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004648 if (task->tk_status > 0)
4649 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004650 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4651 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004652}
4653
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004654static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4655 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004657 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004658 if (!hdr->pgio_done_cb)
4659 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004660 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004661 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662}
4663
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004664static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4665 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004666{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004667 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004668 &hdr->args.seq_args,
4669 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004670 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004671 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004672 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4673 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004674 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004675 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004676 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004677 return -EIO;
4678 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679}
4680
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004681static int nfs4_write_done_cb(struct rpc_task *task,
4682 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004684 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004685
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004686 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004687 if (task->tk_status < 0) {
4688 struct nfs4_exception exception = {
4689 .inode = hdr->inode,
4690 .state = hdr->args.context->state,
4691 .stateid = &hdr->args.stateid,
4692 };
4693 task->tk_status = nfs4_async_handle_exception(task,
4694 NFS_SERVER(inode), task->tk_status,
4695 &exception);
4696 if (exception.retry) {
4697 rpc_restart_call_prepare(task);
4698 return -EAGAIN;
4699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004701 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004702 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004703 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004704 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004705 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706}
4707
Trond Myklebust5521abf2013-03-16 20:54:34 -04004708static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004709 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004710{
4711
4712 if (!nfs4_error_stateid_expired(task->tk_status) ||
4713 nfs4_stateid_is_current(&args->stateid,
4714 args->context,
4715 args->lock_context,
4716 FMODE_WRITE))
4717 return false;
4718 rpc_restart_call_prepare(task);
4719 return true;
4720}
4721
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004722static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004723{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004724 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004725 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004726 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004727 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004728 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4729 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004730}
4731
Trond Myklebust5a37f852012-04-28 14:55:16 -04004732static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004733bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004734{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004735 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004736 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004737 return false;
4738 /* Otherwise, request attributes if and only if we don't hold
4739 * a delegation
4740 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004741 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004742}
Fred Isamana69aef12011-03-03 15:13:47 +00004743
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004744static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4745 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004747 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004748
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004749 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4750 hdr->args.bitmask = NULL;
4751 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004752 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004753 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004754
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004755 if (!hdr->pgio_done_cb)
4756 hdr->pgio_done_cb = nfs4_write_done_cb;
4757 hdr->res.server = server;
4758 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004760 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004761 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762}
4763
Fred Isaman0b7c0152012-04-20 14:47:39 -04004764static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4765{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004766 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004767 &data->args.seq_args,
4768 &data->res.seq_res,
4769 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770}
4771
Fred Isaman0b7c0152012-04-20 14:47:39 -04004772static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004775
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004776 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004777 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4778 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004779 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004780 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004782 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783}
4784
Fred Isaman0b7c0152012-04-20 14:47:39 -04004785static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004786{
4787 if (!nfs4_sequence_done(task, &data->res.seq_res))
4788 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004789 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004790}
4791
Fred Isaman0b7c0152012-04-20 14:47:39 -04004792static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004794 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004795
Fred Isaman0b7c0152012-04-20 14:47:39 -04004796 if (data->commit_done_cb == NULL)
4797 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004798 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004799 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004800 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801}
4802
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004803struct nfs4_renewdata {
4804 struct nfs_client *client;
4805 unsigned long timestamp;
4806};
4807
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808/*
4809 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4810 * standalone procedure for queueing an asynchronous RENEW.
4811 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004812static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004813{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004814 struct nfs4_renewdata *data = calldata;
4815 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004816
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004817 if (atomic_read(&clp->cl_count) > 1)
4818 nfs4_schedule_state_renewal(clp);
4819 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004820 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004821}
4822
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004823static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004825 struct nfs4_renewdata *data = calldata;
4826 struct nfs_client *clp = data->client;
4827 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004829 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004830 switch (task->tk_status) {
4831 case 0:
4832 break;
4833 case -NFS4ERR_LEASE_MOVED:
4834 nfs4_schedule_lease_moved_recovery(clp);
4835 break;
4836 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004837 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004838 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4839 return;
4840 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004841 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004842 return;
4843 }
4844 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004846 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847}
4848
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004849static const struct rpc_call_ops nfs4_renew_ops = {
4850 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004851 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004852};
4853
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004854static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855{
4856 struct rpc_message msg = {
4857 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4858 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004859 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004861 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004863 if (renew_flags == 0)
4864 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004865 if (!atomic_inc_not_zero(&clp->cl_count))
4866 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004867 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04004868 if (data == NULL) {
4869 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004870 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04004871 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004872 data->client = clp;
4873 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004874 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004875 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876}
4877
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004878static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879{
4880 struct rpc_message msg = {
4881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4882 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004883 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 };
4885 unsigned long now = jiffies;
4886 int status;
4887
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004888 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 if (status < 0)
4890 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004891 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 return 0;
4893}
4894
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004895static inline int nfs4_server_supports_acls(struct nfs_server *server)
4896{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004897 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004898}
4899
Trond Myklebust21f498c2012-08-24 10:59:25 -04004900/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4901 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004902 * the stack.
4903 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004904#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004905
Neil Hormane9e3d722011-03-04 19:26:03 -05004906static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004907 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004908{
4909 struct page *newpage, **spages;
4910 int rc = 0;
4911 size_t len;
4912 spages = pages;
4913
4914 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004915 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004916 newpage = alloc_page(GFP_KERNEL);
4917
4918 if (newpage == NULL)
4919 goto unwind;
4920 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05004921 buf += len;
4922 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05004923 *pages++ = newpage;
4924 rc++;
4925 } while (buflen != 0);
4926
4927 return rc;
4928
4929unwind:
4930 for(; rc > 0; rc--)
4931 __free_page(spages[rc-1]);
4932 return -ENOMEM;
4933}
4934
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004935struct nfs4_cached_acl {
4936 int cached;
4937 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004938 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004939};
4940
4941static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004942{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004943 struct nfs_inode *nfsi = NFS_I(inode);
4944
4945 spin_lock(&inode->i_lock);
4946 kfree(nfsi->nfs4_acl);
4947 nfsi->nfs4_acl = acl;
4948 spin_unlock(&inode->i_lock);
4949}
4950
4951static void nfs4_zap_acl_attr(struct inode *inode)
4952{
4953 nfs4_set_cached_acl(inode, NULL);
4954}
4955
4956static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4957{
4958 struct nfs_inode *nfsi = NFS_I(inode);
4959 struct nfs4_cached_acl *acl;
4960 int ret = -ENOENT;
4961
4962 spin_lock(&inode->i_lock);
4963 acl = nfsi->nfs4_acl;
4964 if (acl == NULL)
4965 goto out;
4966 if (buf == NULL) /* user is just asking for length */
4967 goto out_len;
4968 if (acl->cached == 0)
4969 goto out;
4970 ret = -ERANGE; /* see getxattr(2) man page */
4971 if (acl->len > buflen)
4972 goto out;
4973 memcpy(buf, acl->data, acl->len);
4974out_len:
4975 ret = acl->len;
4976out:
4977 spin_unlock(&inode->i_lock);
4978 return ret;
4979}
4980
Sachin Prabhu5794d212012-04-17 14:36:40 +01004981static 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 +00004982{
4983 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004984 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004985
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004986 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004987 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004988 if (acl == NULL)
4989 goto out;
4990 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004991 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004992 } else {
4993 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4994 if (acl == NULL)
4995 goto out;
4996 acl->cached = 0;
4997 }
4998 acl->len = acl_len;
4999out:
5000 nfs4_set_cached_acl(inode, acl);
5001}
5002
Andy Adamsonbf118a32011-12-07 11:55:27 -05005003/*
5004 * The getxattr API returns the required buffer length when called with a
5005 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5006 * the required buf. On a NULL buf, we send a page of data to the server
5007 * guessing that the ACL request can be serviced by a page. If so, we cache
5008 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5009 * the cache. If not so, we throw away the page, and cache the required
5010 * length. The next getxattr call will then produce another round trip to
5011 * the server, this time with the input buf of the required size.
5012 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005013static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005014{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005015 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005016 struct nfs_getaclargs args = {
5017 .fh = NFS_FH(inode),
5018 .acl_pages = pages,
5019 .acl_len = buflen,
5020 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005021 struct nfs_getaclres res = {
5022 .acl_len = buflen,
5023 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005024 struct rpc_message msg = {
5025 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5026 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005027 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005028 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005029 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005030 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005031
Trond Myklebust21f498c2012-08-24 10:59:25 -04005032 if (npages > ARRAY_SIZE(pages))
5033 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005034
Andy Adamsonbf118a32011-12-07 11:55:27 -05005035 for (i = 0; i < npages; i++) {
5036 pages[i] = alloc_page(GFP_KERNEL);
5037 if (!pages[i])
5038 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005039 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005040
5041 /* for decoding across pages */
5042 res.acl_scratch = alloc_page(GFP_KERNEL);
5043 if (!res.acl_scratch)
5044 goto out_free;
5045
Andy Adamsonbf118a32011-12-07 11:55:27 -05005046 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005047
Peng Taode040be2012-01-10 22:42:47 +08005048 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005049 __func__, buf, buflen, npages, args.acl_len);
5050 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5051 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005052 if (ret)
5053 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005054
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005055 /* Handle the case where the passed-in buffer is too short */
5056 if (res.acl_flags & NFS4_ACL_TRUNC) {
5057 /* Did the user only issue a request for the acl length? */
5058 if (buf == NULL)
5059 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005060 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005061 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005062 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005063 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005064 if (buf) {
5065 if (res.acl_len > buflen) {
5066 ret = -ERANGE;
5067 goto out_free;
5068 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005069 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005070 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005071out_ok:
5072 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005073out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005074 for (i = 0; i < npages; i++)
5075 if (pages[i])
5076 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005077 if (res.acl_scratch)
5078 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005079 return ret;
5080}
5081
Trond Myklebust16b42892006-08-24 12:27:15 -04005082static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5083{
5084 struct nfs4_exception exception = { };
5085 ssize_t ret;
5086 do {
5087 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005088 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005089 if (ret >= 0)
5090 break;
5091 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5092 } while (exception.retry);
5093 return ret;
5094}
5095
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005096static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5097{
5098 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005099 int ret;
5100
5101 if (!nfs4_server_supports_acls(server))
5102 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005103 ret = nfs_revalidate_inode(server, inode);
5104 if (ret < 0)
5105 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005106 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5107 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005108 ret = nfs4_read_cached_acl(inode, buf, buflen);
5109 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005110 /* -ENOENT is returned if there is no ACL or if there is an ACL
5111 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005112 return ret;
5113 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005114}
5115
Trond Myklebust16b42892006-08-24 12:27:15 -04005116static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005117{
5118 struct nfs_server *server = NFS_SERVER(inode);
5119 struct page *pages[NFS4ACL_MAXPAGES];
5120 struct nfs_setaclargs arg = {
5121 .fh = NFS_FH(inode),
5122 .acl_pages = pages,
5123 .acl_len = buflen,
5124 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005125 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005126 struct rpc_message msg = {
5127 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5128 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005129 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005130 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005131 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005132 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005133
5134 if (!nfs4_server_supports_acls(server))
5135 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005136 if (npages > ARRAY_SIZE(pages))
5137 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005138 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005139 if (i < 0)
5140 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005141 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005142 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005143
5144 /*
5145 * Free each page after tx, so the only ref left is
5146 * held by the network stack
5147 */
5148 for (; i > 0; i--)
5149 put_page(pages[i-1]);
5150
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005151 /*
5152 * Acl update can result in inode attribute update.
5153 * so mark the attribute cache invalid.
5154 */
5155 spin_lock(&inode->i_lock);
5156 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5157 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005158 nfs_access_zap_cache(inode);
5159 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005160 return ret;
5161}
5162
Trond Myklebust16b42892006-08-24 12:27:15 -04005163static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5164{
5165 struct nfs4_exception exception = { };
5166 int err;
5167 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005168 err = __nfs4_proc_set_acl(inode, buf, buflen);
5169 trace_nfs4_set_acl(inode, err);
5170 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005171 &exception);
5172 } while (exception.retry);
5173 return err;
5174}
5175
David Quigleyaa9c2662013-05-22 12:50:44 -04005176#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5177static int _nfs4_get_security_label(struct inode *inode, void *buf,
5178 size_t buflen)
5179{
5180 struct nfs_server *server = NFS_SERVER(inode);
5181 struct nfs_fattr fattr;
5182 struct nfs4_label label = {0, 0, buflen, buf};
5183
5184 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005185 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005186 .fh = NFS_FH(inode),
5187 .bitmask = bitmask,
5188 };
5189 struct nfs4_getattr_res res = {
5190 .fattr = &fattr,
5191 .label = &label,
5192 .server = server,
5193 };
5194 struct rpc_message msg = {
5195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005196 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005197 .rpc_resp = &res,
5198 };
5199 int ret;
5200
5201 nfs_fattr_init(&fattr);
5202
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005203 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005204 if (ret)
5205 return ret;
5206 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5207 return -ENOENT;
5208 if (buflen < label.len)
5209 return -ERANGE;
5210 return 0;
5211}
5212
5213static int nfs4_get_security_label(struct inode *inode, void *buf,
5214 size_t buflen)
5215{
5216 struct nfs4_exception exception = { };
5217 int err;
5218
5219 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5220 return -EOPNOTSUPP;
5221
5222 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005223 err = _nfs4_get_security_label(inode, buf, buflen);
5224 trace_nfs4_get_security_label(inode, err);
5225 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005226 &exception);
5227 } while (exception.retry);
5228 return err;
5229}
5230
5231static int _nfs4_do_set_security_label(struct inode *inode,
5232 struct nfs4_label *ilabel,
5233 struct nfs_fattr *fattr,
5234 struct nfs4_label *olabel)
5235{
5236
5237 struct iattr sattr = {0};
5238 struct nfs_server *server = NFS_SERVER(inode);
5239 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005240 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005241 .fh = NFS_FH(inode),
5242 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005243 .server = server,
5244 .bitmask = bitmask,
5245 .label = ilabel,
5246 };
5247 struct nfs_setattrres res = {
5248 .fattr = fattr,
5249 .label = olabel,
5250 .server = server,
5251 };
5252 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5254 .rpc_argp = &arg,
5255 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005256 };
5257 int status;
5258
Jeff Layton12207f62013-11-01 10:49:32 -04005259 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005260
Jeff Layton12207f62013-11-01 10:49:32 -04005261 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005262 if (status)
5263 dprintk("%s failed: %d\n", __func__, status);
5264
5265 return status;
5266}
5267
5268static int nfs4_do_set_security_label(struct inode *inode,
5269 struct nfs4_label *ilabel,
5270 struct nfs_fattr *fattr,
5271 struct nfs4_label *olabel)
5272{
5273 struct nfs4_exception exception = { };
5274 int err;
5275
5276 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005277 err = _nfs4_do_set_security_label(inode, ilabel,
5278 fattr, olabel);
5279 trace_nfs4_set_security_label(inode, err);
5280 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005281 &exception);
5282 } while (exception.retry);
5283 return err;
5284}
5285
5286static int
Al Viro59301222016-05-27 10:19:30 -04005287nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005288{
5289 struct nfs4_label ilabel, *olabel = NULL;
5290 struct nfs_fattr fattr;
5291 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005292 int status;
5293
5294 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5295 return -EOPNOTSUPP;
5296
5297 nfs_fattr_init(&fattr);
5298
5299 ilabel.pi = 0;
5300 ilabel.lfs = 0;
5301 ilabel.label = (char *)buf;
5302 ilabel.len = buflen;
5303
5304 cred = rpc_lookup_cred();
5305 if (IS_ERR(cred))
5306 return PTR_ERR(cred);
5307
5308 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5309 if (IS_ERR(olabel)) {
5310 status = -PTR_ERR(olabel);
5311 goto out;
5312 }
5313
5314 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5315 if (status == 0)
5316 nfs_setsecurity(inode, &fattr, olabel);
5317
5318 nfs4_label_free(olabel);
5319out:
5320 put_rpccred(cred);
5321 return status;
5322}
5323#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5324
5325
Chuck Leverf0920752012-05-21 22:45:41 -04005326static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5327 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005328{
5329 __be32 verf[2];
5330
Chuck Lever2c820d92012-05-21 22:45:33 -04005331 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5332 /* An impossible timestamp guarantees this value
5333 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005334 verf[0] = cpu_to_be32(U32_MAX);
5335 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005336 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005337 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005338 u64 ns = ktime_to_ns(nn->boot_time);
5339
5340 verf[0] = cpu_to_be32(ns >> 32);
5341 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005342 }
Chuck Levercd937102012-03-02 17:14:31 -05005343 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5344}
5345
Jeff Laytona3192682015-06-09 19:43:59 -04005346static int
5347nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005348{
Jeff Laytona3192682015-06-09 19:43:59 -04005349 size_t len;
5350 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005351
Trond Myklebustceb3a162015-01-03 15:16:04 -05005352 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005353 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005354
Jeff Laytona3192682015-06-09 19:43:59 -04005355 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005356 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005357 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5358 1 +
5359 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5360 1;
5361 rcu_read_unlock();
5362
5363 if (len > NFS4_OPAQUE_LIMIT + 1)
5364 return -EINVAL;
5365
5366 /*
5367 * Since this string is allocated at mount time, and held until the
5368 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5369 * about a memory-reclaim deadlock.
5370 */
5371 str = kmalloc(len, GFP_KERNEL);
5372 if (!str)
5373 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005374
Chuck Levere984a552012-09-14 17:24:21 -04005375 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005376 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005377 clp->cl_ipaddr,
5378 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5379 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005380 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005381
Jeff Laytona3192682015-06-09 19:43:59 -04005382 clp->cl_owner_id = str;
5383 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005384}
5385
Jeff Layton873e3852015-06-09 19:44:00 -04005386static int
5387nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005388{
Jeff Layton873e3852015-06-09 19:44:00 -04005389 size_t len;
5390 char *str;
5391
5392 len = 10 + 10 + 1 + 10 + 1 +
5393 strlen(nfs4_client_id_uniquifier) + 1 +
5394 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5395
5396 if (len > NFS4_OPAQUE_LIMIT + 1)
5397 return -EINVAL;
5398
5399 /*
5400 * Since this string is allocated at mount time, and held until the
5401 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5402 * about a memory-reclaim deadlock.
5403 */
5404 str = kmalloc(len, GFP_KERNEL);
5405 if (!str)
5406 return -ENOMEM;
5407
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005408 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005409 clp->rpc_ops->version, clp->cl_minorversion,
5410 nfs4_client_id_uniquifier,
5411 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005412 clp->cl_owner_id = str;
5413 return 0;
5414}
5415
5416static int
5417nfs4_init_uniform_client_string(struct nfs_client *clp)
5418{
Jeff Layton873e3852015-06-09 19:44:00 -04005419 size_t len;
5420 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005421
5422 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005423 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005424
5425 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005426 return nfs4_init_uniquifier_client_string(clp);
5427
5428 len = 10 + 10 + 1 + 10 + 1 +
5429 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5430
5431 if (len > NFS4_OPAQUE_LIMIT + 1)
5432 return -EINVAL;
5433
5434 /*
5435 * Since this string is allocated at mount time, and held until the
5436 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5437 * about a memory-reclaim deadlock.
5438 */
5439 str = kmalloc(len, GFP_KERNEL);
5440 if (!str)
5441 return -ENOMEM;
5442
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005443 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005444 clp->rpc_ops->version, clp->cl_minorversion,
5445 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005446 clp->cl_owner_id = str;
5447 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005448}
5449
Chuck Lever706cb8d2014-03-12 12:51:47 -04005450/*
5451 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5452 * services. Advertise one based on the address family of the
5453 * clientaddr.
5454 */
5455static unsigned int
5456nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5457{
5458 if (strchr(clp->cl_ipaddr, ':') != NULL)
5459 return scnprintf(buf, len, "tcp6");
5460 else
5461 return scnprintf(buf, len, "tcp");
5462}
5463
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005464static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5465{
5466 struct nfs4_setclientid *sc = calldata;
5467
5468 if (task->tk_status == 0)
5469 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5470}
5471
5472static const struct rpc_call_ops nfs4_setclientid_ops = {
5473 .rpc_call_done = nfs4_setclientid_done,
5474};
5475
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005476/**
5477 * nfs4_proc_setclientid - Negotiate client ID
5478 * @clp: state data structure
5479 * @program: RPC program for NFSv4 callback service
5480 * @port: IP port number for NFS4 callback service
5481 * @cred: RPC credential to use for this call
5482 * @res: where to place the result
5483 *
5484 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5485 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005486int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5487 unsigned short port, struct rpc_cred *cred,
5488 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489{
5490 nfs4_verifier sc_verifier;
5491 struct nfs4_setclientid setclientid = {
5492 .sc_verifier = &sc_verifier,
5493 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005494 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 };
5496 struct rpc_message msg = {
5497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5498 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005499 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005500 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005502 struct rpc_task *task;
5503 struct rpc_task_setup task_setup_data = {
5504 .rpc_client = clp->cl_rpcclient,
5505 .rpc_message = &msg,
5506 .callback_ops = &nfs4_setclientid_ops,
5507 .callback_data = &setclientid,
5508 .flags = RPC_TASK_TIMEOUT,
5509 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005510 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511
Chuck Leverde734832012-07-11 16:30:50 -04005512 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005513 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005514
5515 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5516 status = nfs4_init_uniform_client_string(clp);
5517 else
Jeff Laytona3192682015-06-09 19:43:59 -04005518 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005519
5520 if (status)
5521 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005522
Chuck Leverde734832012-07-11 16:30:50 -04005523 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005524 setclientid.sc_netid_len =
5525 nfs4_init_callback_netid(clp,
5526 setclientid.sc_netid,
5527 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005528 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005529 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 clp->cl_ipaddr, port >> 8, port & 255);
5531
Jeff Layton3a6bb732015-06-09 19:43:57 -04005532 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005533 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005534 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005535 task = rpc_run_task(&task_setup_data);
5536 if (IS_ERR(task)) {
5537 status = PTR_ERR(task);
5538 goto out;
5539 }
5540 status = task->tk_status;
5541 if (setclientid.sc_cred) {
5542 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5543 put_rpccred(setclientid.sc_cred);
5544 }
5545 rpc_put_task(task);
5546out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005547 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005548 dprintk("NFS reply setclientid: %d\n", status);
5549 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550}
5551
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005552/**
5553 * nfs4_proc_setclientid_confirm - Confirm client ID
5554 * @clp: state data structure
5555 * @res: result of a previous SETCLIENTID
5556 * @cred: RPC credential to use for this call
5557 *
5558 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5559 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005560int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005561 struct nfs4_setclientid_res *arg,
5562 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 struct rpc_message msg = {
5565 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005566 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005567 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 int status;
5570
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005571 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5572 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5573 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005574 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005575 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005576 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 return status;
5578}
5579
Trond Myklebustfe650402006-01-03 09:55:18 +01005580struct nfs4_delegreturndata {
5581 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005582 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005583 struct nfs_fh fh;
5584 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005585 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005586 struct {
5587 struct nfs4_layoutreturn_args arg;
5588 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005589 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005590 u32 roc_barrier;
5591 bool roc;
5592 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005593 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005594 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005595 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005596};
5597
Trond Myklebustfe650402006-01-03 09:55:18 +01005598static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5599{
5600 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005601
Trond Myklebust14516c32010-07-31 14:29:06 -04005602 if (!nfs4_sequence_done(task, &data->res.seq_res))
5603 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005604
Trond Myklebustca8acf82013-08-13 10:36:56 -04005605 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005606
5607 /* Handle Layoutreturn errors */
5608 if (data->args.lr_args && task->tk_status != 0) {
5609 switch(data->res.lr_ret) {
5610 default:
5611 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5612 break;
5613 case 0:
5614 data->args.lr_args = NULL;
5615 data->res.lr_res = NULL;
5616 break;
5617 case -NFS4ERR_ADMIN_REVOKED:
5618 case -NFS4ERR_DELEG_REVOKED:
5619 case -NFS4ERR_EXPIRED:
5620 case -NFS4ERR_BAD_STATEID:
5621 case -NFS4ERR_OLD_STATEID:
5622 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5623 case -NFS4ERR_WRONG_CRED:
5624 data->args.lr_args = NULL;
5625 data->res.lr_res = NULL;
5626 data->res.lr_ret = 0;
5627 rpc_restart_call_prepare(task);
5628 return;
5629 }
5630 }
5631
Ricardo Labiaga79708862009-12-07 09:23:21 -05005632 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005633 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005634 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005635 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005636 case -NFS4ERR_ADMIN_REVOKED:
5637 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005638 case -NFS4ERR_EXPIRED:
5639 nfs4_free_revoked_stateid(data->res.server,
5640 data->args.stateid,
5641 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005642 case -NFS4ERR_BAD_STATEID:
5643 case -NFS4ERR_OLD_STATEID:
5644 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005645 task->tk_status = 0;
5646 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005647 case -NFS4ERR_ACCESS:
5648 if (data->args.bitmask) {
5649 data->args.bitmask = NULL;
5650 data->res.fattr = NULL;
5651 task->tk_status = 0;
5652 rpc_restart_call_prepare(task);
5653 return;
5654 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005655 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005656 if (nfs4_async_handle_error(task, data->res.server,
5657 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005658 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005659 return;
5660 }
5661 }
5662 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005663}
5664
5665static void nfs4_delegreturn_release(void *calldata)
5666{
Peng Tao039b7562014-07-03 13:05:02 +08005667 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005668 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005669
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005670 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005671 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005672 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5673 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005674 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005675 nfs_iput_and_deactive(inode);
5676 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005677 kfree(calldata);
5678}
5679
Andy Adamson938e1012009-04-01 09:22:28 -04005680static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5681{
5682 struct nfs4_delegreturndata *d_data;
5683
5684 d_data = (struct nfs4_delegreturndata *)data;
5685
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005686 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005687 return;
5688
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005689 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005690 &d_data->args.seq_args,
5691 &d_data->res.seq_res,
5692 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005693}
Andy Adamson938e1012009-04-01 09:22:28 -04005694
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005695static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005696 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005697 .rpc_call_done = nfs4_delegreturn_done,
5698 .rpc_release = nfs4_delegreturn_release,
5699};
5700
Trond Myklebuste6f81072008-01-24 18:14:34 -05005701static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005702{
5703 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005704 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005705 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005706 struct rpc_message msg = {
5707 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5708 .rpc_cred = cred,
5709 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005710 struct rpc_task_setup task_setup_data = {
5711 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005712 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005713 .callback_ops = &nfs4_delegreturn_ops,
5714 .flags = RPC_TASK_ASYNC,
5715 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005716 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005717
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005718 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005719 if (data == NULL)
5720 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005721 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005722
5723 nfs4_state_protect(server->nfs_client,
5724 NFS_SP4_MACH_CRED_CLEANUP,
5725 &task_setup_data.rpc_client, &msg);
5726
Trond Myklebustfe650402006-01-03 09:55:18 +01005727 data->args.fhandle = &data->fh;
5728 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005729 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005730 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005731 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005732 data->res.fattr = &data->fattr;
5733 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005734 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005735 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005736 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005737 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005738 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005739 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005740 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005741 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005742 if (data->lr.roc) {
5743 data->args.lr_args = &data->lr.arg;
5744 data->res.lr_res = &data->lr.res;
5745 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005746 } else if (data->lr.roc) {
5747 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5748 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005749 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005750
Trond Myklebustc970aa82007-07-14 15:39:59 -04005751 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005752 msg.rpc_argp = &data->args;
5753 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005754 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005755 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005756 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005757 if (!issync)
5758 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005759 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005760 if (status != 0)
5761 goto out;
5762 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005763out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005764 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005765 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766}
5767
Trond Myklebuste6f81072008-01-24 18:14:34 -05005768int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769{
5770 struct nfs_server *server = NFS_SERVER(inode);
5771 struct nfs4_exception exception = { };
5772 int err;
5773 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005774 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005775 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 switch (err) {
5777 case -NFS4ERR_STALE_STATEID:
5778 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 case 0:
5780 return 0;
5781 }
5782 err = nfs4_handle_exception(server, err, &exception);
5783 } while (exception.retry);
5784 return err;
5785}
5786
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5788{
5789 struct inode *inode = state->inode;
5790 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005791 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005792 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005794 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005796 struct nfs_lockt_res res = {
5797 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 };
5799 struct rpc_message msg = {
5800 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005801 .rpc_argp = &arg,
5802 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 .rpc_cred = state->owner->so_cred,
5804 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 struct nfs4_lock_state *lsp;
5806 int status;
5807
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005808 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005809 status = nfs4_set_lock_state(state, request);
5810 if (status != 0)
5811 goto out;
5812 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005813 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005814 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005815 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005816 switch (status) {
5817 case 0:
5818 request->fl_type = F_UNLCK;
5819 break;
5820 case -NFS4ERR_DENIED:
5821 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005823 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005824 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005825out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 return status;
5827}
5828
5829static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5830{
5831 struct nfs4_exception exception = { };
5832 int err;
5833
5834 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005835 err = _nfs4_proc_getlk(state, cmd, request);
5836 trace_nfs4_get_lock(request, state, cmd, err);
5837 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 &exception);
5839 } while (exception.retry);
5840 return err;
5841}
5842
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005843struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005844 struct nfs_locku_args arg;
5845 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005846 struct nfs4_lock_state *lsp;
5847 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005848 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005849 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005850 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005851 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005852};
5853
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005854static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5855 struct nfs_open_context *ctx,
5856 struct nfs4_lock_state *lsp,
5857 struct nfs_seqid *seqid)
5858{
5859 struct nfs4_unlockdata *p;
5860 struct inode *inode = lsp->ls_state->inode;
5861
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005862 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005863 if (p == NULL)
5864 return NULL;
5865 p->arg.fh = NFS_FH(inode);
5866 p->arg.fl = &p->fl;
5867 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005868 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005869 p->lsp = lsp;
5870 atomic_inc(&lsp->ls_count);
5871 /* Ensure we don't close file until we're done freeing locks! */
5872 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005873 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005874 memcpy(&p->fl, fl, sizeof(p->fl));
5875 p->server = NFS_SERVER(inode);
5876 return p;
5877}
5878
Trond Myklebust06f814a2006-01-03 09:55:07 +01005879static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005880{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005881 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005882 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005883 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005884 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005885 put_nfs_open_context(calldata->ctx);
5886 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005887}
5888
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005889static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005890{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005891 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005892
Trond Myklebust14516c32010-07-31 14:29:06 -04005893 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5894 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005895 switch (task->tk_status) {
5896 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005897 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005898 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005899 if (nfs4_update_lock_stateid(calldata->lsp,
5900 &calldata->res.stateid))
5901 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005902 case -NFS4ERR_ADMIN_REVOKED:
5903 case -NFS4ERR_EXPIRED:
5904 nfs4_free_revoked_stateid(calldata->server,
5905 &calldata->arg.stateid,
5906 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005907 case -NFS4ERR_BAD_STATEID:
5908 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005909 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005910 if (!nfs4_stateid_match(&calldata->arg.stateid,
5911 &calldata->lsp->ls_stateid))
5912 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005913 break;
5914 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005915 if (nfs4_async_handle_error(task, calldata->server,
5916 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005917 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005918 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005919 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005920}
5921
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005922static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005923{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005924 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005926 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
5927 nfs_async_iocounter_wait(task, calldata->l_ctx))
5928 return;
5929
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005930 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005931 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005932 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005933 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005934 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005935 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005936 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005937 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005938 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04005939 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005940 &calldata->res.seq_res,
5941 task) != 0)
5942 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005943 return;
5944out_no_action:
5945 task->tk_action = NULL;
5946out_wait:
5947 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948}
5949
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005950static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005951 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005952 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005953 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005954};
5955
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005956static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5957 struct nfs_open_context *ctx,
5958 struct nfs4_lock_state *lsp,
5959 struct nfs_seqid *seqid)
5960{
5961 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005962 struct rpc_message msg = {
5963 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5964 .rpc_cred = ctx->cred,
5965 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005966 struct rpc_task_setup task_setup_data = {
5967 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005968 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005969 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005970 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005971 .flags = RPC_TASK_ASYNC,
5972 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005973
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005974 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5975 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5976
Frank Filz137d6ac2007-07-09 15:32:29 -07005977 /* Ensure this is an unlock - when canceling a lock, the
5978 * canceled lock is passed in, and it won't be an unlock.
5979 */
5980 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005981 if (fl->fl_flags & FL_CLOSE)
5982 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07005983
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005984 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5985 if (data == NULL) {
5986 nfs_free_seqid(seqid);
5987 return ERR_PTR(-ENOMEM);
5988 }
5989
Chuck Levera9c92d62013-08-09 12:48:18 -04005990 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005991 msg.rpc_argp = &data->arg;
5992 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005993 task_setup_data.callback_data = data;
5994 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005995}
5996
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5998{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005999 struct inode *inode = state->inode;
6000 struct nfs4_state_owner *sp = state->owner;
6001 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006002 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006003 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006004 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006005 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006006 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006007 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008
Trond Myklebust9b073572006-06-29 16:38:34 -04006009 status = nfs4_set_lock_state(state, request);
6010 /* Unlock _before_ we do the RPC call */
6011 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006012 /* Exclude nfs_delegation_claim_locks() */
6013 mutex_lock(&sp->so_delegreturn_mutex);
6014 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006015 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006016 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006017 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006018 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006019 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006020 }
6021 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006022 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006023 if (status != 0)
6024 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006025 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006026 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006027 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6028 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006029 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6030 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006031 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006032 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006033 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006034 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006035 status = PTR_ERR(task);
6036 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006037 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006038 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006039 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006040out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006041 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006042 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006043 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044}
6045
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006046struct nfs4_lockdata {
6047 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006048 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006049 struct nfs4_lock_state *lsp;
6050 struct nfs_open_context *ctx;
6051 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006052 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006053 int rpc_status;
6054 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006055 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006056};
6057
6058static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006059 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6060 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006061{
6062 struct nfs4_lockdata *p;
6063 struct inode *inode = lsp->ls_state->inode;
6064 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006065 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006066
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006067 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006068 if (p == NULL)
6069 return NULL;
6070
6071 p->arg.fh = NFS_FH(inode);
6072 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006073 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006074 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006075 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006076 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6077 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006078 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006079 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006080 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006081 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006082 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006083 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006084 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006085 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006086 atomic_inc(&lsp->ls_count);
6087 p->ctx = get_nfs_open_context(ctx);
6088 memcpy(&p->fl, fl, sizeof(p->fl));
6089 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006090out_free_seqid:
6091 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006092out_free:
6093 kfree(p);
6094 return NULL;
6095}
6096
6097static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6098{
6099 struct nfs4_lockdata *data = calldata;
6100 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101
Harvey Harrison3110ff82008-05-02 13:42:44 -07006102 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006103 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006104 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006105 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006106 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006107 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006108 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006109 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006110 nfs4_stateid_copy(&data->arg.open_stateid,
6111 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006112 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006113 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006114 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006115 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006116 nfs4_stateid_copy(&data->arg.lock_stateid,
6117 &data->lsp->ls_stateid);
6118 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006119 if (!nfs4_valid_open_stateid(state)) {
6120 data->rpc_status = -EBADF;
6121 task->tk_action = NULL;
6122 goto out_release_open_seqid;
6123 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006124 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006125 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006126 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006127 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006128 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006129 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006130out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006131 nfs_release_seqid(data->arg.open_seqid);
6132out_release_lock_seqid:
6133 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006134out_wait:
6135 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006136 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006137}
6138
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006139static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6140{
6141 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006142 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006143
Harvey Harrison3110ff82008-05-02 13:42:44 -07006144 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006145
Trond Myklebust14516c32010-07-31 14:29:06 -04006146 if (!nfs4_sequence_done(task, &data->res.seq_res))
6147 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006148
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006149 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006150 switch (task->tk_status) {
6151 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006152 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006153 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006154 if (data->arg.new_lock) {
6155 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006156 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006157 rpc_restart_call_prepare(task);
6158 break;
6159 }
6160 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006161 if (data->arg.new_lock_owner != 0) {
6162 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6163 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6164 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6165 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6166 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006167 break;
6168 case -NFS4ERR_BAD_STATEID:
6169 case -NFS4ERR_OLD_STATEID:
6170 case -NFS4ERR_STALE_STATEID:
6171 case -NFS4ERR_EXPIRED:
6172 if (data->arg.new_lock_owner != 0) {
6173 if (!nfs4_stateid_match(&data->arg.open_stateid,
6174 &lsp->ls_state->open_stateid))
6175 rpc_restart_call_prepare(task);
6176 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6177 &lsp->ls_stateid))
6178 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006179 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006180 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006181}
6182
6183static void nfs4_lock_release(void *calldata)
6184{
6185 struct nfs4_lockdata *data = calldata;
6186
Harvey Harrison3110ff82008-05-02 13:42:44 -07006187 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006188 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006189 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006190 struct rpc_task *task;
6191 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6192 data->arg.lock_seqid);
6193 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006194 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006195 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006196 } else
6197 nfs_free_seqid(data->arg.lock_seqid);
6198 nfs4_put_lock_state(data->lsp);
6199 put_nfs_open_context(data->ctx);
6200 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006201 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006202}
6203
6204static const struct rpc_call_ops nfs4_lock_ops = {
6205 .rpc_call_prepare = nfs4_lock_prepare,
6206 .rpc_call_done = nfs4_lock_done,
6207 .rpc_release = nfs4_lock_release,
6208};
6209
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006210static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6211{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006212 switch (error) {
6213 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006214 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006215 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006216 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006217 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006218 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006219 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006220 break;
6221 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006222 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006223 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006224 };
6225}
6226
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006227static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006228{
6229 struct nfs4_lockdata *data;
6230 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006231 struct rpc_message msg = {
6232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6233 .rpc_cred = state->owner->so_cred,
6234 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006235 struct rpc_task_setup task_setup_data = {
6236 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006237 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006238 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006239 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006240 .flags = RPC_TASK_ASYNC,
6241 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006242 int ret;
6243
Harvey Harrison3110ff82008-05-02 13:42:44 -07006244 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006245 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006246 fl->fl_u.nfs4_fl.owner,
6247 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006248 if (data == NULL)
6249 return -ENOMEM;
6250 if (IS_SETLKW(cmd))
6251 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006252 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006253 msg.rpc_argp = &data->arg;
6254 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006255 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006256 if (recovery_type > NFS_LOCK_NEW) {
6257 if (recovery_type == NFS_LOCK_RECLAIM)
6258 data->arg.reclaim = NFS_LOCK_RECLAIM;
6259 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006260 } else
6261 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006262 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006263 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006264 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006265 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006266 if (ret == 0) {
6267 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006268 if (ret)
6269 nfs4_handle_setlk_error(data->server, data->lsp,
6270 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006271 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006272 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006273 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006274 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006275 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006276 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277}
6278
6279static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6280{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006281 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006282 struct nfs4_exception exception = {
6283 .inode = state->inode,
6284 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006285 int err;
6286
6287 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006288 /* Cache the lock if possible... */
6289 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6290 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006291 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006292 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006293 break;
6294 nfs4_handle_exception(server, err, &exception);
6295 } while (exception.retry);
6296 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297}
6298
6299static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6300{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006301 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006302 struct nfs4_exception exception = {
6303 .inode = state->inode,
6304 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006305 int err;
6306
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006307 err = nfs4_set_lock_state(state, request);
6308 if (err != 0)
6309 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006310 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006311 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6312 return 0;
6313 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006314 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006315 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6316 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006317 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006318 switch (err) {
6319 default:
6320 goto out;
6321 case -NFS4ERR_GRACE:
6322 case -NFS4ERR_DELAY:
6323 nfs4_handle_exception(server, err, &exception);
6324 err = 0;
6325 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006326 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006327out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006328 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329}
6330
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006331#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006332static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6333{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006334 struct nfs4_lock_state *lsp;
6335 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006336
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006337 status = nfs4_set_lock_state(state, request);
6338 if (status != 0)
6339 return status;
6340 lsp = request->fl_u.nfs4_fl.owner;
6341 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6342 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6343 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006344 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006345}
6346#endif
6347
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6349{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006350 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006351 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006352 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006353 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354
Trond Myklebust01c3b862006-06-29 16:38:39 -04006355 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006356 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006357 if (status < 0)
6358 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006359 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006360 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006361 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006362 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006363 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006364 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006365 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006366 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006367 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006368 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006369 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006370 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006371 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006372 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006373out:
6374 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 return status;
6376}
6377
6378static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6379{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006380 struct nfs4_exception exception = {
6381 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006382 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006383 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 int err;
6385
6386 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006387 err = _nfs4_proc_setlk(state, cmd, request);
6388 if (err == -NFS4ERR_DENIED)
6389 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006391 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392 } while (exception.retry);
6393 return err;
6394}
6395
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006396#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6397#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6398
6399static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006400nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6401 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006402{
6403 int status = -ERESTARTSYS;
6404 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6405
6406 while(!signalled()) {
6407 status = nfs4_proc_setlk(state, cmd, request);
6408 if ((status != -EAGAIN) || IS_SETLK(cmd))
6409 break;
6410 freezable_schedule_timeout_interruptible(timeout);
6411 timeout *= 2;
6412 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6413 status = -ERESTARTSYS;
6414 }
6415 return status;
6416}
6417
Jeff Laytona1d617d82016-09-17 18:17:39 -04006418#ifdef CONFIG_NFS_V4_1
6419struct nfs4_lock_waiter {
6420 struct task_struct *task;
6421 struct inode *inode;
6422 struct nfs_lowner *owner;
6423 bool notified;
6424};
6425
6426static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006427nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006428{
6429 int ret;
6430 struct cb_notify_lock_args *cbnl = key;
6431 struct nfs4_lock_waiter *waiter = wait->private;
6432 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6433 *wowner = waiter->owner;
6434
6435 /* Only wake if the callback was for the same owner */
6436 if (lowner->clientid != wowner->clientid ||
6437 lowner->id != wowner->id ||
6438 lowner->s_dev != wowner->s_dev)
6439 return 0;
6440
6441 /* Make sure it's for the right inode */
6442 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6443 return 0;
6444
6445 waiter->notified = true;
6446
6447 /* override "private" so we can use default_wake_function */
6448 wait->private = waiter->task;
6449 ret = autoremove_wake_function(wait, mode, flags, key);
6450 wait->private = waiter;
6451 return ret;
6452}
6453
6454static int
6455nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6456{
6457 int status = -ERESTARTSYS;
6458 unsigned long flags;
6459 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6460 struct nfs_server *server = NFS_SERVER(state->inode);
6461 struct nfs_client *clp = server->nfs_client;
6462 wait_queue_head_t *q = &clp->cl_lock_waitq;
6463 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6464 .id = lsp->ls_seqid.owner_id,
6465 .s_dev = server->s_dev };
6466 struct nfs4_lock_waiter waiter = { .task = current,
6467 .inode = state->inode,
6468 .owner = &owner,
6469 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006470 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006471
6472 /* Don't bother with waitqueue if we don't expect a callback */
6473 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6474 return nfs4_retry_setlk_simple(state, cmd, request);
6475
6476 init_wait(&wait);
6477 wait.private = &waiter;
6478 wait.func = nfs4_wake_lock_waiter;
6479 add_wait_queue(q, &wait);
6480
6481 while(!signalled()) {
6482 status = nfs4_proc_setlk(state, cmd, request);
6483 if ((status != -EAGAIN) || IS_SETLK(cmd))
6484 break;
6485
6486 status = -ERESTARTSYS;
6487 spin_lock_irqsave(&q->lock, flags);
6488 if (waiter.notified) {
6489 spin_unlock_irqrestore(&q->lock, flags);
6490 continue;
6491 }
6492 set_current_state(TASK_INTERRUPTIBLE);
6493 spin_unlock_irqrestore(&q->lock, flags);
6494
6495 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6496 }
6497
6498 finish_wait(q, &wait);
6499 return status;
6500}
6501#else /* !CONFIG_NFS_V4_1 */
6502static inline int
6503nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6504{
6505 return nfs4_retry_setlk_simple(state, cmd, request);
6506}
6507#endif
6508
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509static int
6510nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6511{
6512 struct nfs_open_context *ctx;
6513 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514 int status;
6515
6516 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006517 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 state = ctx->state;
6519
Trond Myklebustd9531262009-07-21 19:22:38 -04006520 if (IS_GETLK(cmd)) {
6521 if (state != NULL)
6522 return nfs4_proc_getlk(state, F_GETLK, request);
6523 return 0;
6524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525
6526 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6527 return -EINVAL;
6528
Trond Myklebustd9531262009-07-21 19:22:38 -04006529 if (request->fl_type == F_UNLCK) {
6530 if (state != NULL)
6531 return nfs4_proc_unlck(state, cmd, request);
6532 return 0;
6533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534
Trond Myklebustd9531262009-07-21 19:22:38 -04006535 if (state == NULL)
6536 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006537
6538 if ((request->fl_flags & FL_POSIX) &&
6539 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6540 return -ENOLCK;
6541
Jeff Layton1ea67db2016-09-17 18:17:37 -04006542 status = nfs4_set_lock_state(state, request);
6543 if (status != 0)
6544 return status;
6545
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006546 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547}
6548
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006549int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006550{
6551 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006552 int err;
6553
6554 err = nfs4_set_lock_state(state, fl);
6555 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006556 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006557 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006558 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006559}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006560
Trond Myklebustcf470c32012-03-07 13:49:12 -05006561struct nfs_release_lockowner_data {
6562 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006563 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006564 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006565 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006566 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006567};
6568
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006569static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6570{
6571 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006572 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006573 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6574 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006575 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006576 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006577}
6578
6579static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6580{
6581 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006582 struct nfs_server *server = data->server;
6583
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006584 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006585
6586 switch (task->tk_status) {
6587 case 0:
6588 renew_lease(server, data->timestamp);
6589 break;
6590 case -NFS4ERR_STALE_CLIENTID:
6591 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006592 nfs4_schedule_lease_recovery(server->nfs_client);
6593 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006594 case -NFS4ERR_LEASE_MOVED:
6595 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006596 if (nfs4_async_handle_error(task, server,
6597 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006598 rpc_restart_call_prepare(task);
6599 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006600}
6601
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006602static void nfs4_release_lockowner_release(void *calldata)
6603{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006604 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006605 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006606 kfree(calldata);
6607}
6608
Trond Myklebust17280172012-03-11 13:11:00 -04006609static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006610 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6611 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006612 .rpc_release = nfs4_release_lockowner_release,
6613};
6614
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006615static void
6616nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006617{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006618 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006619 struct rpc_message msg = {
6620 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6621 };
6622
6623 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006624 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006625
Trond Myklebustcf470c32012-03-07 13:49:12 -05006626 data = kmalloc(sizeof(*data), GFP_NOFS);
6627 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006628 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006629 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006630 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006631 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6632 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6633 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006634
Trond Myklebustcf470c32012-03-07 13:49:12 -05006635 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006636 msg.rpc_resp = &data->res;
6637 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006638 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006639}
6640
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006641#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6642
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006643static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006644 struct dentry *unused, struct inode *inode,
6645 const char *key, const void *buf,
6646 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006647{
Al Viro59301222016-05-27 10:19:30 -04006648 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006649}
6650
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006651static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006652 struct dentry *unused, struct inode *inode,
6653 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006654{
Al Virob2968212016-04-10 20:48:24 -04006655 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006656}
6657
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006658static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006659{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006660 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006661}
6662
David Quigleyc9bccef2013-05-22 12:50:45 -04006663#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006664
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006665static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006666 struct dentry *unused, struct inode *inode,
6667 const char *key, const void *buf,
6668 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006669{
6670 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006671 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006672
6673 return -EOPNOTSUPP;
6674}
6675
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006676static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006677 struct dentry *unused, struct inode *inode,
6678 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006679{
6680 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006681 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006682 return -EOPNOTSUPP;
6683}
6684
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006685static ssize_t
6686nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006687{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006688 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006689
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006690 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6691 len = security_inode_listsecurity(inode, list, list_len);
6692 if (list_len && len > list_len)
6693 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006694 }
6695 return len;
6696}
6697
6698static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6699 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006700 .get = nfs4_xattr_get_nfs4_label,
6701 .set = nfs4_xattr_set_nfs4_label,
6702};
David Quigleyc9bccef2013-05-22 12:50:45 -04006703
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006704#else
6705
6706static ssize_t
6707nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6708{
6709 return 0;
6710}
6711
6712#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006713
Andy Adamson533eb462011-06-13 18:25:56 -04006714/*
6715 * nfs_fhget will use either the mounted_on_fileid or the fileid
6716 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006717static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6718{
Andy Adamson533eb462011-06-13 18:25:56 -04006719 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6720 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6721 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006722 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006723 return;
6724
6725 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006726 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006727 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6728 fattr->nlink = 2;
6729}
6730
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006731static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6732 const struct qstr *name,
6733 struct nfs4_fs_locations *fs_locations,
6734 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006735{
6736 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006737 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006738 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006739 };
6740 struct nfs4_fs_locations_arg args = {
6741 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006742 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006743 .page = page,
6744 .bitmask = bitmask,
6745 };
Benny Halevy22958462009-04-01 09:22:02 -04006746 struct nfs4_fs_locations_res res = {
6747 .fs_locations = fs_locations,
6748 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006749 struct rpc_message msg = {
6750 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6751 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006752 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006753 };
6754 int status;
6755
Harvey Harrison3110ff82008-05-02 13:42:44 -07006756 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006757
6758 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6759 * is not supported */
6760 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6761 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6762 else
6763 bitmask[0] |= FATTR4_WORD0_FILEID;
6764
Trond Myklebustc228fd32007-01-13 02:28:11 -05006765 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006766 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006767 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006768 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006769 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006770 return status;
6771}
6772
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006773int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6774 const struct qstr *name,
6775 struct nfs4_fs_locations *fs_locations,
6776 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006777{
6778 struct nfs4_exception exception = { };
6779 int err;
6780 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006781 err = _nfs4_proc_fs_locations(client, dir, name,
6782 fs_locations, page);
6783 trace_nfs4_get_fs_locations(dir, name, err);
6784 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006785 &exception);
6786 } while (exception.retry);
6787 return err;
6788}
6789
Chuck Leverb03d7352013-10-17 14:12:50 -04006790/*
6791 * This operation also signals the server that this client is
6792 * performing migration recovery. The server can stop returning
6793 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6794 * appended to this compound to identify the client ID which is
6795 * performing recovery.
6796 */
6797static int _nfs40_proc_get_locations(struct inode *inode,
6798 struct nfs4_fs_locations *locations,
6799 struct page *page, struct rpc_cred *cred)
6800{
6801 struct nfs_server *server = NFS_SERVER(inode);
6802 struct rpc_clnt *clnt = server->client;
6803 u32 bitmask[2] = {
6804 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6805 };
6806 struct nfs4_fs_locations_arg args = {
6807 .clientid = server->nfs_client->cl_clientid,
6808 .fh = NFS_FH(inode),
6809 .page = page,
6810 .bitmask = bitmask,
6811 .migration = 1, /* skip LOOKUP */
6812 .renew = 1, /* append RENEW */
6813 };
6814 struct nfs4_fs_locations_res res = {
6815 .fs_locations = locations,
6816 .migration = 1,
6817 .renew = 1,
6818 };
6819 struct rpc_message msg = {
6820 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6821 .rpc_argp = &args,
6822 .rpc_resp = &res,
6823 .rpc_cred = cred,
6824 };
6825 unsigned long now = jiffies;
6826 int status;
6827
6828 nfs_fattr_init(&locations->fattr);
6829 locations->server = server;
6830 locations->nlocations = 0;
6831
6832 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6833 nfs4_set_sequence_privileged(&args.seq_args);
6834 status = nfs4_call_sync_sequence(clnt, server, &msg,
6835 &args.seq_args, &res.seq_res);
6836 if (status)
6837 return status;
6838
6839 renew_lease(server, now);
6840 return 0;
6841}
6842
6843#ifdef CONFIG_NFS_V4_1
6844
6845/*
6846 * This operation also signals the server that this client is
6847 * performing migration recovery. The server can stop asserting
6848 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6849 * performing this operation is identified in the SEQUENCE
6850 * operation in this compound.
6851 *
6852 * When the client supports GETATTR(fs_locations_info), it can
6853 * be plumbed in here.
6854 */
6855static int _nfs41_proc_get_locations(struct inode *inode,
6856 struct nfs4_fs_locations *locations,
6857 struct page *page, struct rpc_cred *cred)
6858{
6859 struct nfs_server *server = NFS_SERVER(inode);
6860 struct rpc_clnt *clnt = server->client;
6861 u32 bitmask[2] = {
6862 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6863 };
6864 struct nfs4_fs_locations_arg args = {
6865 .fh = NFS_FH(inode),
6866 .page = page,
6867 .bitmask = bitmask,
6868 .migration = 1, /* skip LOOKUP */
6869 };
6870 struct nfs4_fs_locations_res res = {
6871 .fs_locations = locations,
6872 .migration = 1,
6873 };
6874 struct rpc_message msg = {
6875 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6876 .rpc_argp = &args,
6877 .rpc_resp = &res,
6878 .rpc_cred = cred,
6879 };
6880 int status;
6881
6882 nfs_fattr_init(&locations->fattr);
6883 locations->server = server;
6884 locations->nlocations = 0;
6885
6886 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6887 nfs4_set_sequence_privileged(&args.seq_args);
6888 status = nfs4_call_sync_sequence(clnt, server, &msg,
6889 &args.seq_args, &res.seq_res);
6890 if (status == NFS4_OK &&
6891 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6892 status = -NFS4ERR_LEASE_MOVED;
6893 return status;
6894}
6895
6896#endif /* CONFIG_NFS_V4_1 */
6897
6898/**
6899 * nfs4_proc_get_locations - discover locations for a migrated FSID
6900 * @inode: inode on FSID that is migrating
6901 * @locations: result of query
6902 * @page: buffer
6903 * @cred: credential to use for this operation
6904 *
6905 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6906 * operation failed, or a negative errno if a local error occurred.
6907 *
6908 * On success, "locations" is filled in, but if the server has
6909 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6910 * asserted.
6911 *
6912 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6913 * from this client that require migration recovery.
6914 */
6915int nfs4_proc_get_locations(struct inode *inode,
6916 struct nfs4_fs_locations *locations,
6917 struct page *page, struct rpc_cred *cred)
6918{
6919 struct nfs_server *server = NFS_SERVER(inode);
6920 struct nfs_client *clp = server->nfs_client;
6921 const struct nfs4_mig_recovery_ops *ops =
6922 clp->cl_mvops->mig_recovery_ops;
6923 struct nfs4_exception exception = { };
6924 int status;
6925
6926 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6927 (unsigned long long)server->fsid.major,
6928 (unsigned long long)server->fsid.minor,
6929 clp->cl_hostname);
6930 nfs_display_fhandle(NFS_FH(inode), __func__);
6931
6932 do {
6933 status = ops->get_locations(inode, locations, page, cred);
6934 if (status != -NFS4ERR_DELAY)
6935 break;
6936 nfs4_handle_exception(server, status, &exception);
6937 } while (exception.retry);
6938 return status;
6939}
6940
Chuck Lever44c99932013-10-17 14:13:30 -04006941/*
6942 * This operation also signals the server that this client is
6943 * performing "lease moved" recovery. The server can stop
6944 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6945 * is appended to this compound to identify the client ID which is
6946 * performing recovery.
6947 */
6948static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6949{
6950 struct nfs_server *server = NFS_SERVER(inode);
6951 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6952 struct rpc_clnt *clnt = server->client;
6953 struct nfs4_fsid_present_arg args = {
6954 .fh = NFS_FH(inode),
6955 .clientid = clp->cl_clientid,
6956 .renew = 1, /* append RENEW */
6957 };
6958 struct nfs4_fsid_present_res res = {
6959 .renew = 1,
6960 };
6961 struct rpc_message msg = {
6962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6963 .rpc_argp = &args,
6964 .rpc_resp = &res,
6965 .rpc_cred = cred,
6966 };
6967 unsigned long now = jiffies;
6968 int status;
6969
6970 res.fh = nfs_alloc_fhandle();
6971 if (res.fh == NULL)
6972 return -ENOMEM;
6973
6974 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6975 nfs4_set_sequence_privileged(&args.seq_args);
6976 status = nfs4_call_sync_sequence(clnt, server, &msg,
6977 &args.seq_args, &res.seq_res);
6978 nfs_free_fhandle(res.fh);
6979 if (status)
6980 return status;
6981
6982 do_renew_lease(clp, now);
6983 return 0;
6984}
6985
6986#ifdef CONFIG_NFS_V4_1
6987
6988/*
6989 * This operation also signals the server that this client is
6990 * performing "lease moved" recovery. The server can stop asserting
6991 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6992 * this operation is identified in the SEQUENCE operation in this
6993 * compound.
6994 */
6995static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6996{
6997 struct nfs_server *server = NFS_SERVER(inode);
6998 struct rpc_clnt *clnt = server->client;
6999 struct nfs4_fsid_present_arg args = {
7000 .fh = NFS_FH(inode),
7001 };
7002 struct nfs4_fsid_present_res res = {
7003 };
7004 struct rpc_message msg = {
7005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7006 .rpc_argp = &args,
7007 .rpc_resp = &res,
7008 .rpc_cred = cred,
7009 };
7010 int status;
7011
7012 res.fh = nfs_alloc_fhandle();
7013 if (res.fh == NULL)
7014 return -ENOMEM;
7015
7016 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7017 nfs4_set_sequence_privileged(&args.seq_args);
7018 status = nfs4_call_sync_sequence(clnt, server, &msg,
7019 &args.seq_args, &res.seq_res);
7020 nfs_free_fhandle(res.fh);
7021 if (status == NFS4_OK &&
7022 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7023 status = -NFS4ERR_LEASE_MOVED;
7024 return status;
7025}
7026
7027#endif /* CONFIG_NFS_V4_1 */
7028
7029/**
7030 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7031 * @inode: inode on FSID to check
7032 * @cred: credential to use for this operation
7033 *
7034 * Server indicates whether the FSID is present, moved, or not
7035 * recognized. This operation is necessary to clear a LEASE_MOVED
7036 * condition for this client ID.
7037 *
7038 * Returns NFS4_OK if the FSID is present on this server,
7039 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7040 * NFS4ERR code if some error occurred on the server, or a
7041 * negative errno if a local failure occurred.
7042 */
7043int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7044{
7045 struct nfs_server *server = NFS_SERVER(inode);
7046 struct nfs_client *clp = server->nfs_client;
7047 const struct nfs4_mig_recovery_ops *ops =
7048 clp->cl_mvops->mig_recovery_ops;
7049 struct nfs4_exception exception = { };
7050 int status;
7051
7052 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7053 (unsigned long long)server->fsid.major,
7054 (unsigned long long)server->fsid.minor,
7055 clp->cl_hostname);
7056 nfs_display_fhandle(NFS_FH(inode), __func__);
7057
7058 do {
7059 status = ops->fsid_present(inode, cred);
7060 if (status != -NFS4ERR_DELAY)
7061 break;
7062 nfs4_handle_exception(server, status, &exception);
7063 } while (exception.retry);
7064 return status;
7065}
7066
Andy Adamson5ec16a82013-08-08 10:57:55 -04007067/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007068 * If 'use_integrity' is true and the state managment nfs_client
7069 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7070 * and the machine credential as per RFC3530bis and RFC5661 Security
7071 * Considerations sections. Otherwise, just use the user cred with the
7072 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007073 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007074static 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 +00007075{
7076 int status;
7077 struct nfs4_secinfo_arg args = {
7078 .dir_fh = NFS_FH(dir),
7079 .name = name,
7080 };
7081 struct nfs4_secinfo_res res = {
7082 .flavors = flavors,
7083 };
7084 struct rpc_message msg = {
7085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7086 .rpc_argp = &args,
7087 .rpc_resp = &res,
7088 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007089 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007090 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007091
7092 if (use_integrity) {
7093 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007094 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7095 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007096 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007097
7098 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007099
7100 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7101 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7102
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007103 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7104 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007105 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007106
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007107 if (cred)
7108 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007109
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007110 return status;
7111}
7112
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007113int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7114 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007115{
7116 struct nfs4_exception exception = { };
7117 int err;
7118 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007119 err = -NFS4ERR_WRONGSEC;
7120
7121 /* try to use integrity protection with machine cred */
7122 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7123 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7124
7125 /*
7126 * if unable to use integrity protection, or SECINFO with
7127 * integrity protection returns NFS4ERR_WRONGSEC (which is
7128 * disallowed by spec, but exists in deployed servers) use
7129 * the current filesystem's rpc_client and the user cred.
7130 */
7131 if (err == -NFS4ERR_WRONGSEC)
7132 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7133
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007134 trace_nfs4_secinfo(dir, name, err);
7135 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007136 &exception);
7137 } while (exception.retry);
7138 return err;
7139}
7140
Andy Adamson557134a2009-04-01 09:21:53 -04007141#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007142/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007143 * Check the exchange flags returned by the server for invalid flags, having
7144 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7145 * DS flags set.
7146 */
7147static int nfs4_check_cl_exchange_flags(u32 flags)
7148{
7149 if (flags & ~EXCHGID4_FLAG_MASK_R)
7150 goto out_inval;
7151 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7152 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7153 goto out_inval;
7154 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7155 goto out_inval;
7156 return NFS_OK;
7157out_inval:
7158 return -NFS4ERR_INVAL;
7159}
7160
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007161static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007162nfs41_same_server_scope(struct nfs41_server_scope *a,
7163 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007164{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007165 if (a->server_scope_sz != b->server_scope_sz)
7166 return false;
7167 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007168}
7169
Andy Adamson02a95de2016-02-05 16:08:37 -05007170static void
7171nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7172{
7173}
7174
7175static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7176 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7177};
7178
Andy Adamson357f54d2010-12-14 10:11:57 -05007179/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007180 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007181 *
7182 * The 4.1 client currently uses the same TCP connection for the
7183 * fore and backchannel.
7184 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007185static
7186int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7187 struct rpc_xprt *xprt,
7188 struct nfs_client *clp,
7189 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007190{
7191 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007192 struct nfs41_bind_conn_to_session_args args = {
7193 .client = clp,
7194 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7195 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007196 struct nfs41_bind_conn_to_session_res res;
7197 struct rpc_message msg = {
7198 .rpc_proc =
7199 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007200 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007201 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007202 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007203 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007204 struct rpc_task_setup task_setup_data = {
7205 .rpc_client = clnt,
7206 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007207 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007208 .rpc_message = &msg,
7209 .flags = RPC_TASK_TIMEOUT,
7210 };
7211 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007212
Trond Myklebust71a097c2015-02-18 09:27:18 -08007213 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7214 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7215 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007216
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007217 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7218 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7219 args.dir = NFS4_CDFC4_FORE;
7220
7221 task = rpc_run_task(&task_setup_data);
7222 if (!IS_ERR(task)) {
7223 status = task->tk_status;
7224 rpc_put_task(task);
7225 } else
7226 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007227 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007228 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007229 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007230 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7231 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007232 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007233 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007234 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007235 dprintk("NFS: %s: Unexpected direction from server\n",
7236 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007237 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007238 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007239 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007240 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7241 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007242 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007243 }
7244 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007245
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007246 return status;
7247}
7248
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007249struct rpc_bind_conn_calldata {
7250 struct nfs_client *clp;
7251 struct rpc_cred *cred;
7252};
7253
7254static int
7255nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7256 struct rpc_xprt *xprt,
7257 void *calldata)
7258{
7259 struct rpc_bind_conn_calldata *p = calldata;
7260
7261 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7262}
7263
7264int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7265{
7266 struct rpc_bind_conn_calldata data = {
7267 .clp = clp,
7268 .cred = cred,
7269 };
7270 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7271 nfs4_proc_bind_conn_to_session_callback, &data);
7272}
7273
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007274/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007275 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7276 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007277 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007278static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7279 .how = SP4_MACH_CRED,
7280 .enforce.u.words = {
7281 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7282 1 << (OP_EXCHANGE_ID - 32) |
7283 1 << (OP_CREATE_SESSION - 32) |
7284 1 << (OP_DESTROY_SESSION - 32) |
7285 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007286 },
7287 .allow.u.words = {
7288 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007289 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007290 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007291 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007292 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007293 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007294 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007295 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007296 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007297 1 << (OP_FREE_STATEID - 32) |
7298 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007299 }
7300};
7301
7302/*
7303 * Select the state protection mode for client `clp' given the server results
7304 * from exchange_id in `sp'.
7305 *
7306 * Returns 0 on success, negative errno otherwise.
7307 */
7308static int nfs4_sp4_select_mode(struct nfs_client *clp,
7309 struct nfs41_state_protection *sp)
7310{
7311 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7312 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7313 1 << (OP_EXCHANGE_ID - 32) |
7314 1 << (OP_CREATE_SESSION - 32) |
7315 1 << (OP_DESTROY_SESSION - 32) |
7316 1 << (OP_DESTROY_CLIENTID - 32)
7317 };
7318 unsigned int i;
7319
7320 if (sp->how == SP4_MACH_CRED) {
7321 /* Print state protect result */
7322 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7323 for (i = 0; i <= LAST_NFS4_OP; i++) {
7324 if (test_bit(i, sp->enforce.u.longs))
7325 dfprintk(MOUNT, " enforce op %d\n", i);
7326 if (test_bit(i, sp->allow.u.longs))
7327 dfprintk(MOUNT, " allow op %d\n", i);
7328 }
7329
7330 /* make sure nothing is on enforce list that isn't supported */
7331 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7332 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7333 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7334 return -EINVAL;
7335 }
7336 }
7337
7338 /*
7339 * Minimal mode - state operations are allowed to use machine
7340 * credential. Note this already happens by default, so the
7341 * client doesn't have to do anything more than the negotiation.
7342 *
7343 * NOTE: we don't care if EXCHANGE_ID is in the list -
7344 * we're already using the machine cred for exchange_id
7345 * and will never use a different cred.
7346 */
7347 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7348 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7349 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7350 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7351 dfprintk(MOUNT, "sp4_mach_cred:\n");
7352 dfprintk(MOUNT, " minimal mode enabled\n");
7353 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7354 } else {
7355 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7356 return -EINVAL;
7357 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007358
7359 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007360 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7361 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007362 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7363 dfprintk(MOUNT, " cleanup mode enabled\n");
7364 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7365 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007366
Andrew Elble99ade3c2015-12-02 09:39:51 -05007367 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7368 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7369 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7370 &clp->cl_sp4_flags);
7371 }
7372
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007373 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7374 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7375 dfprintk(MOUNT, " secinfo mode enabled\n");
7376 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7377 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007378
7379 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7380 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7381 dfprintk(MOUNT, " stateid mode enabled\n");
7382 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7383 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007384
7385 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7386 dfprintk(MOUNT, " write mode enabled\n");
7387 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7388 }
7389
7390 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7391 dfprintk(MOUNT, " commit mode enabled\n");
7392 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7393 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007394 }
7395
7396 return 0;
7397}
7398
Andy Adamson8d89bd72016-09-09 09:22:18 -04007399struct nfs41_exchange_id_data {
7400 struct nfs41_exchange_id_res res;
7401 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007402 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007403 int rpc_status;
7404};
7405
7406static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007407{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007408 struct nfs41_exchange_id_data *cdata =
7409 (struct nfs41_exchange_id_data *)data;
7410 struct nfs_client *clp = cdata->args.client;
7411 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007412
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007413 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007414
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007415 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007416 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007417
7418 if (cdata->xprt && status == 0) {
7419 status = nfs4_detect_session_trunking(clp, &cdata->res,
7420 cdata->xprt);
7421 goto out;
7422 }
7423
Andy Adamson8d89bd72016-09-09 09:22:18 -04007424 if (status == 0)
7425 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007426
Chuck Lever177313f2012-05-21 22:44:58 -04007427 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007428 clp->cl_clientid = cdata->res.clientid;
7429 clp->cl_exchange_flags = cdata->res.flags;
Chuck Lever838edb92017-06-08 11:52:52 -04007430 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007431 /* Client ID is not confirmed */
Chuck Lever838edb92017-06-08 11:52:52 -04007432 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R))
Trond Myklebuste11259f2015-03-03 20:35:31 -05007433 clear_bit(NFS4_SESSION_ESTABLISHED,
Chuck Lever838edb92017-06-08 11:52:52 -04007434 &clp->cl_session->session_state);
Trond Myklebust32b01312012-05-26 13:41:04 -04007435
Chuck Leveracdeb692012-05-21 22:46:16 -04007436 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007437 clp->cl_serverowner = cdata->res.server_owner;
7438 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007439
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007440 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007441 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007442 clp->cl_implid = cdata->res.impl_id;
7443 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007444
Chuck Lever177313f2012-05-21 22:44:58 -04007445 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007446 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007447 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007448 dprintk("%s: server_scope mismatch detected\n",
7449 __func__);
7450 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007451 kfree(clp->cl_serverscope);
7452 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007453 }
7454
Chuck Lever177313f2012-05-21 22:44:58 -04007455 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007456 clp->cl_serverscope = cdata->res.server_scope;
7457 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007458 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007459 /* Save the EXCHANGE_ID verifier session trunk tests */
7460 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7461 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007462 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007463out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007464 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007465 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007466}
7467
7468static void nfs4_exchange_id_release(void *data)
7469{
7470 struct nfs41_exchange_id_data *cdata =
7471 (struct nfs41_exchange_id_data *)data;
7472
Andy Adamsonad0849a2016-09-09 09:22:28 -04007473 if (cdata->xprt) {
7474 xprt_put(cdata->xprt);
7475 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7476 }
Olga Kornievskaia63513232017-03-13 10:36:19 -04007477 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007478 kfree(cdata->res.impl_id);
7479 kfree(cdata->res.server_scope);
7480 kfree(cdata->res.server_owner);
7481 kfree(cdata);
7482}
7483
7484static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7485 .rpc_call_done = nfs4_exchange_id_done,
7486 .rpc_release = nfs4_exchange_id_release,
7487};
7488
Benny Halevy99fe60d2009-04-01 09:22:29 -04007489/*
7490 * _nfs4_proc_exchange_id()
7491 *
7492 * Wrapper for EXCHANGE_ID operation.
7493 */
7494static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007495 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007496{
7497 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007498 struct rpc_message msg = {
7499 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007500 .rpc_cred = cred,
7501 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007502 struct rpc_task_setup task_setup_data = {
7503 .rpc_client = clp->cl_rpcclient,
7504 .callback_ops = &nfs4_exchange_id_call_ops,
7505 .rpc_message = &msg,
7506 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7507 };
7508 struct nfs41_exchange_id_data *calldata;
7509 struct rpc_task *task;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007510 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007511
7512 if (!atomic_inc_not_zero(&clp->cl_count))
Anna Schumakere917f0d2017-04-07 14:15:22 -04007513 return -EIO;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007514
Andy Adamson8d89bd72016-09-09 09:22:18 -04007515 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04007516 if (!calldata) {
7517 nfs_put_client(clp);
Anna Schumakere917f0d2017-04-07 14:15:22 -04007518 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04007519 }
Benny Halevy99fe60d2009-04-01 09:22:29 -04007520
Andy Adamsonad0849a2016-09-09 09:22:28 -04007521 if (!xprt)
7522 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007523
7524 status = nfs4_init_uniform_client_string(clp);
7525 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007526 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007527
Andy Adamson8d89bd72016-09-09 09:22:18 -04007528 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7529 GFP_NOFS);
7530 status = -ENOMEM;
7531 if (unlikely(calldata->res.server_owner == NULL))
7532 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007533
Andy Adamson8d89bd72016-09-09 09:22:18 -04007534 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007535 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007536 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007537 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007538
Andy Adamson8d89bd72016-09-09 09:22:18 -04007539 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7540 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007541 goto out_server_scope;
7542
7543 switch (sp4_how) {
7544 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007545 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007546 break;
7547
7548 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007549 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007550 break;
7551
7552 default:
7553 /* unsupported! */
7554 WARN_ON_ONCE(1);
7555 status = -EINVAL;
7556 goto out_impl_id;
7557 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007558 if (xprt) {
7559 calldata->xprt = xprt;
7560 task_setup_data.rpc_xprt = xprt;
7561 task_setup_data.flags =
7562 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7563 calldata->args.verifier = &clp->cl_confirm;
7564 } else {
7565 calldata->args.verifier = &verifier;
7566 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007567 calldata->args.client = clp;
7568#ifdef CONFIG_NFS_V4_1_MIGRATION
7569 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7570 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7571 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7572#else
7573 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7574 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7575#endif
7576 msg.rpc_argp = &calldata->args;
7577 msg.rpc_resp = &calldata->res;
7578 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007579
Andy Adamson8d89bd72016-09-09 09:22:18 -04007580 task = rpc_run_task(&task_setup_data);
Olga Kornievskaia63513232017-03-13 10:36:19 -04007581 if (IS_ERR(task))
7582 return PTR_ERR(task);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007583
Andy Adamsonad0849a2016-09-09 09:22:28 -04007584 if (!xprt) {
7585 status = rpc_wait_for_completion_task(task);
7586 if (!status)
7587 status = calldata->rpc_status;
7588 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007589 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007590
Andy Adamson8d89bd72016-09-09 09:22:18 -04007591 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007592out:
Benny Halevy99fe60d2009-04-01 09:22:29 -04007593 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007594
7595out_impl_id:
7596 kfree(calldata->res.impl_id);
7597out_server_scope:
7598 kfree(calldata->res.server_scope);
7599out_server_owner:
7600 kfree(calldata->res.server_owner);
7601out_calldata:
7602 kfree(calldata);
Olga Kornievskaia63513232017-03-13 10:36:19 -04007603 nfs_put_client(clp);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007604 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007605}
7606
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007607/*
7608 * nfs4_proc_exchange_id()
7609 *
7610 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7611 *
7612 * Since the clientid has expired, all compounds using sessions
7613 * associated with the stale clientid will be returning
7614 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7615 * be in some phase of session reset.
7616 *
7617 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7618 */
7619int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7620{
7621 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7622 int status;
7623
7624 /* try SP4_MACH_CRED if krb5i/p */
7625 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7626 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007627 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007628 if (!status)
7629 return 0;
7630 }
7631
7632 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007633 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007634}
7635
Andy Adamson04fa2c62016-09-09 09:22:29 -04007636/**
7637 * nfs4_test_session_trunk
7638 *
7639 * This is an add_xprt_test() test function called from
7640 * rpc_clnt_setup_test_and_add_xprt.
7641 *
7642 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7643 * and is dereferrenced in nfs4_exchange_id_release
7644 *
7645 * Upon success, add the new transport to the rpc_clnt
7646 *
7647 * @clnt: struct rpc_clnt to get new transport
7648 * @xprt: the rpc_xprt to test
7649 * @data: call data for _nfs4_proc_exchange_id.
7650 */
7651int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7652 void *data)
7653{
7654 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7655 u32 sp4_how;
7656
7657 dprintk("--> %s try %s\n", __func__,
7658 xprt->address_strings[RPC_DISPLAY_ADDR]);
7659
7660 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7661
7662 /* Test connection for session trunking. Async exchange_id call */
7663 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7664}
7665EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7666
Trond Myklebust66245532012-05-25 17:18:09 -04007667static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7668 struct rpc_cred *cred)
7669{
7670 struct rpc_message msg = {
7671 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7672 .rpc_argp = clp,
7673 .rpc_cred = cred,
7674 };
7675 int status;
7676
7677 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007678 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007679 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007680 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007681 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7682 return status;
7683}
7684
7685static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7686 struct rpc_cred *cred)
7687{
7688 unsigned int loop;
7689 int ret;
7690
7691 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7692 ret = _nfs4_proc_destroy_clientid(clp, cred);
7693 switch (ret) {
7694 case -NFS4ERR_DELAY:
7695 case -NFS4ERR_CLIENTID_BUSY:
7696 ssleep(1);
7697 break;
7698 default:
7699 return ret;
7700 }
7701 }
7702 return 0;
7703}
7704
7705int nfs4_destroy_clientid(struct nfs_client *clp)
7706{
7707 struct rpc_cred *cred;
7708 int ret = 0;
7709
7710 if (clp->cl_mvops->minor_version < 1)
7711 goto out;
7712 if (clp->cl_exchange_flags == 0)
7713 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007714 if (clp->cl_preserve_clid)
7715 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007716 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007717 ret = nfs4_proc_destroy_clientid(clp, cred);
7718 if (cred)
7719 put_rpccred(cred);
7720 switch (ret) {
7721 case 0:
7722 case -NFS4ERR_STALE_CLIENTID:
7723 clp->cl_exchange_flags = 0;
7724 }
7725out:
7726 return ret;
7727}
7728
Andy Adamson2050f0c2009-04-01 09:22:30 -04007729struct nfs4_get_lease_time_data {
7730 struct nfs4_get_lease_time_args *args;
7731 struct nfs4_get_lease_time_res *res;
7732 struct nfs_client *clp;
7733};
7734
7735static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7736 void *calldata)
7737{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007738 struct nfs4_get_lease_time_data *data =
7739 (struct nfs4_get_lease_time_data *)calldata;
7740
7741 dprintk("--> %s\n", __func__);
7742 /* just setup sequence, do not trigger session recovery
7743 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007744 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007745 &data->args->la_seq_args,
7746 &data->res->lr_seq_res,
7747 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007748 dprintk("<-- %s\n", __func__);
7749}
7750
7751/*
7752 * Called from nfs4_state_manager thread for session setup, so don't recover
7753 * from sequence operation or clientid errors.
7754 */
7755static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7756{
7757 struct nfs4_get_lease_time_data *data =
7758 (struct nfs4_get_lease_time_data *)calldata;
7759
7760 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007761 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7762 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007763 switch (task->tk_status) {
7764 case -NFS4ERR_DELAY:
7765 case -NFS4ERR_GRACE:
7766 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7767 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7768 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007769 /* fall through */
7770 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007771 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007772 return;
7773 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007774 dprintk("<-- %s\n", __func__);
7775}
7776
Trond Myklebust17280172012-03-11 13:11:00 -04007777static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007778 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7779 .rpc_call_done = nfs4_get_lease_time_done,
7780};
7781
7782int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7783{
7784 struct rpc_task *task;
7785 struct nfs4_get_lease_time_args args;
7786 struct nfs4_get_lease_time_res res = {
7787 .lr_fsinfo = fsinfo,
7788 };
7789 struct nfs4_get_lease_time_data data = {
7790 .args = &args,
7791 .res = &res,
7792 .clp = clp,
7793 };
7794 struct rpc_message msg = {
7795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7796 .rpc_argp = &args,
7797 .rpc_resp = &res,
7798 };
7799 struct rpc_task_setup task_setup = {
7800 .rpc_client = clp->cl_rpcclient,
7801 .rpc_message = &msg,
7802 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007803 .callback_data = &data,
7804 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007805 };
7806 int status;
7807
Chuck Levera9c92d62013-08-09 12:48:18 -04007808 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007809 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007810 task = rpc_run_task(&task_setup);
7811
7812 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04007813 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007814
Anna Schumakerf6148712017-04-07 14:15:23 -04007815 status = task->tk_status;
7816 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007817 return status;
7818}
7819
Andy Adamsonfc931582009-04-01 09:22:31 -04007820/*
7821 * Initialize the values to be used by the client in CREATE_SESSION
7822 * If nfs4_init_session set the fore channel request and response sizes,
7823 * use them.
7824 *
7825 * Set the back channel max_resp_sz_cached to zero to force the client to
7826 * always set csa_cachethis to FALSE because the current implementation
7827 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7828 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007829static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7830 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007831{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007832 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007833 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007834
Andy Adamson18aad3d2013-06-26 12:21:49 -04007835 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7836 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7837
Andy Adamsonfc931582009-04-01 09:22:31 -04007838 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007839 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7840 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007841 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007842 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007843
7844 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007845 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007846 __func__,
7847 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007848 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007849
7850 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007851 args->bc_attrs.max_rqst_sz = max_bc_payload;
7852 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007853 args->bc_attrs.max_resp_sz_cached = 0;
7854 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007855 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007856
7857 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7858 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7859 __func__,
7860 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7861 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7862 args->bc_attrs.max_reqs);
7863}
7864
Trond Myklebust79969dd2015-02-18 11:30:18 -08007865static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7866 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007867{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007868 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007869 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007870
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007871 if (rcvd->max_resp_sz > sent->max_resp_sz)
7872 return -EINVAL;
7873 /*
7874 * Our requested max_ops is the minimum we need; we're not
7875 * prepared to break up compounds into smaller pieces than that.
7876 * So, no point even trying to continue if the server won't
7877 * cooperate:
7878 */
7879 if (rcvd->max_ops < sent->max_ops)
7880 return -EINVAL;
7881 if (rcvd->max_reqs == 0)
7882 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007883 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7884 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007885 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007886}
7887
Trond Myklebust79969dd2015-02-18 11:30:18 -08007888static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7889 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007890{
7891 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007892 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007893
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007894 if (!(res->flags & SESSION4_BACK_CHAN))
7895 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007896 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7897 return -EINVAL;
7898 if (rcvd->max_resp_sz < sent->max_resp_sz)
7899 return -EINVAL;
7900 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7901 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007902 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007903 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007904 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007905 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007906out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007907 return 0;
7908}
Andy Adamson8d353012009-04-01 09:22:32 -04007909
Andy Adamson8d353012009-04-01 09:22:32 -04007910static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007911 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007912{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007913 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007914
Trond Myklebust79969dd2015-02-18 11:30:18 -08007915 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007916 if (ret)
7917 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007918 return nfs4_verify_back_channel_attrs(args, res);
7919}
7920
7921static void nfs4_update_session(struct nfs4_session *session,
7922 struct nfs41_create_session_res *res)
7923{
7924 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007925 /* Mark client id and session as being confirmed */
7926 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7927 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007928 session->flags = res->flags;
7929 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007930 if (res->flags & SESSION4_BACK_CHAN)
7931 memcpy(&session->bc_attrs, &res->bc_attrs,
7932 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007933}
7934
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007935static int _nfs4_proc_create_session(struct nfs_client *clp,
7936 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007937{
7938 struct nfs4_session *session = clp->cl_session;
7939 struct nfs41_create_session_args args = {
7940 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007941 .clientid = clp->cl_clientid,
7942 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007943 .cb_program = NFS4_CALLBACK,
7944 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007945 struct nfs41_create_session_res res;
7946
Andy Adamsonfc931582009-04-01 09:22:31 -04007947 struct rpc_message msg = {
7948 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7949 .rpc_argp = &args,
7950 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007951 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007952 };
7953 int status;
7954
Chuck Lever6b26cc82016-05-02 14:40:40 -04007955 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007956 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007957
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007958 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007959 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007960
Trond Myklebustb519d402016-09-11 14:50:01 -04007961 switch (status) {
7962 case -NFS4ERR_STALE_CLIENTID:
7963 case -NFS4ERR_DELAY:
7964 case -ETIMEDOUT:
7965 case -EACCES:
7966 case -EAGAIN:
7967 goto out;
7968 };
7969
7970 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007971 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007972 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007973 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007974 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007975 if (status)
7976 goto out;
7977 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007978 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007979out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007980 return status;
7981}
7982
7983/*
7984 * Issues a CREATE_SESSION operation to the server.
7985 * It is the responsibility of the caller to verify the session is
7986 * expired before calling this routine.
7987 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007988int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007989{
7990 int status;
7991 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007992 struct nfs4_session *session = clp->cl_session;
7993
7994 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7995
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007996 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007997 if (status)
7998 goto out;
7999
Andy Adamsonaacd5532011-11-09 13:58:21 -05008000 /* Init or reset the session slot tables */
8001 status = nfs4_setup_session_slot_tables(session);
8002 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008003 if (status)
8004 goto out;
8005
8006 ptr = (unsigned *)&session->sess_id.data[0];
8007 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8008 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008009out:
8010 dprintk("<-- %s\n", __func__);
8011 return status;
8012}
8013
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008014/*
8015 * Issue the over-the-wire RPC DESTROY_SESSION.
8016 * The caller must serialize access to this routine.
8017 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008018int nfs4_proc_destroy_session(struct nfs4_session *session,
8019 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008020{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008021 struct rpc_message msg = {
8022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8023 .rpc_argp = session,
8024 .rpc_cred = cred,
8025 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008026 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008027
8028 dprintk("--> nfs4_proc_destroy_session\n");
8029
8030 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008031 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8032 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008033
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008034 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008035 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008036
8037 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008038 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008039 "Session has been destroyed regardless...\n", status);
8040
8041 dprintk("<-- nfs4_proc_destroy_session\n");
8042 return status;
8043}
8044
Trond Myklebust7b38c362012-05-23 13:23:31 -04008045/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008046 * Renew the cl_session lease.
8047 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008048struct nfs4_sequence_data {
8049 struct nfs_client *clp;
8050 struct nfs4_sequence_args args;
8051 struct nfs4_sequence_res res;
8052};
8053
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008054static void nfs41_sequence_release(void *data)
8055{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008056 struct nfs4_sequence_data *calldata = data;
8057 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008058
Alexandros Batsakis71358402010-02-05 03:45:05 -08008059 if (atomic_read(&clp->cl_count) > 1)
8060 nfs4_schedule_state_renewal(clp);
8061 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008062 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008063}
8064
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008065static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8066{
8067 switch(task->tk_status) {
8068 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008069 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8070 return -EAGAIN;
8071 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008072 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008073 }
8074 return 0;
8075}
8076
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008077static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008078{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008079 struct nfs4_sequence_data *calldata = data;
8080 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008081
Trond Myklebust14516c32010-07-31 14:29:06 -04008082 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8083 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008084
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008085 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008086 if (task->tk_status < 0) {
8087 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008088 if (atomic_read(&clp->cl_count) == 1)
8089 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008090
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008091 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8092 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008093 return;
8094 }
8095 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008096 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008097out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008098 dprintk("<-- %s\n", __func__);
8099}
8100
8101static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8102{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008103 struct nfs4_sequence_data *calldata = data;
8104 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008105 struct nfs4_sequence_args *args;
8106 struct nfs4_sequence_res *res;
8107
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008108 args = task->tk_msg.rpc_argp;
8109 res = task->tk_msg.rpc_resp;
8110
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008111 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008112}
8113
8114static const struct rpc_call_ops nfs41_sequence_ops = {
8115 .rpc_call_done = nfs41_sequence_call_done,
8116 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008117 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008118};
8119
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008120static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8121 struct rpc_cred *cred,
8122 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008123{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008124 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008125 struct rpc_message msg = {
8126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8127 .rpc_cred = cred,
8128 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008129 struct rpc_task_setup task_setup_data = {
8130 .rpc_client = clp->cl_rpcclient,
8131 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008132 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008133 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008134 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008135
Alexandros Batsakis71358402010-02-05 03:45:05 -08008136 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008137 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008138 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008139 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008140 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008141 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008142 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008143 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008144 if (is_privileged)
8145 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008146 msg.rpc_argp = &calldata->args;
8147 msg.rpc_resp = &calldata->res;
8148 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008149 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008150
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008151 return rpc_run_task(&task_setup_data);
8152}
8153
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008154static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008155{
8156 struct rpc_task *task;
8157 int ret = 0;
8158
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008159 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008160 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008161 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008162 if (IS_ERR(task))
8163 ret = PTR_ERR(task);
8164 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008165 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008166 dprintk("<-- %s status=%d\n", __func__, ret);
8167 return ret;
8168}
8169
8170static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8171{
8172 struct rpc_task *task;
8173 int ret;
8174
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008175 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008176 if (IS_ERR(task)) {
8177 ret = PTR_ERR(task);
8178 goto out;
8179 }
8180 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008181 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008182 ret = task->tk_status;
8183 rpc_put_task(task);
8184out:
8185 dprintk("<-- %s status=%d\n", __func__, ret);
8186 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008187}
8188
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008189struct nfs4_reclaim_complete_data {
8190 struct nfs_client *clp;
8191 struct nfs41_reclaim_complete_args arg;
8192 struct nfs41_reclaim_complete_res res;
8193};
8194
8195static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8196{
8197 struct nfs4_reclaim_complete_data *calldata = data;
8198
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008199 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008200 &calldata->arg.seq_args,
8201 &calldata->res.seq_res,
8202 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008203}
8204
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008205static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8206{
8207 switch(task->tk_status) {
8208 case 0:
8209 case -NFS4ERR_COMPLETE_ALREADY:
8210 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8211 break;
8212 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008213 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008214 /* fall through */
8215 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008216 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008217 case -NFS4ERR_BADSESSION:
8218 case -NFS4ERR_DEADSESSION:
8219 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8220 nfs4_schedule_session_recovery(clp->cl_session,
8221 task->tk_status);
8222 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008223 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008224 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008225 }
8226 return 0;
8227}
8228
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008229static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8230{
8231 struct nfs4_reclaim_complete_data *calldata = data;
8232 struct nfs_client *clp = calldata->clp;
8233 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8234
8235 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008236 if (!nfs41_sequence_done(task, res))
8237 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008238
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008239 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008240 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8241 rpc_restart_call_prepare(task);
8242 return;
8243 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008244 dprintk("<-- %s\n", __func__);
8245}
8246
8247static void nfs4_free_reclaim_complete_data(void *data)
8248{
8249 struct nfs4_reclaim_complete_data *calldata = data;
8250
8251 kfree(calldata);
8252}
8253
8254static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8255 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8256 .rpc_call_done = nfs4_reclaim_complete_done,
8257 .rpc_release = nfs4_free_reclaim_complete_data,
8258};
8259
8260/*
8261 * Issue a global reclaim complete.
8262 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008263static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8264 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008265{
8266 struct nfs4_reclaim_complete_data *calldata;
8267 struct rpc_task *task;
8268 struct rpc_message msg = {
8269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008270 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008271 };
8272 struct rpc_task_setup task_setup_data = {
8273 .rpc_client = clp->cl_rpcclient,
8274 .rpc_message = &msg,
8275 .callback_ops = &nfs4_reclaim_complete_call_ops,
8276 .flags = RPC_TASK_ASYNC,
8277 };
8278 int status = -ENOMEM;
8279
8280 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008281 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008282 if (calldata == NULL)
8283 goto out;
8284 calldata->clp = clp;
8285 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008286
Chuck Levera9c92d62013-08-09 12:48:18 -04008287 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008288 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008289 msg.rpc_argp = &calldata->arg;
8290 msg.rpc_resp = &calldata->res;
8291 task_setup_data.callback_data = calldata;
8292 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008293 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008294 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008295 goto out;
8296 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008297 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008298 if (status == 0)
8299 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008300 rpc_put_task(task);
8301out:
8302 dprintk("<-- %s status=%d\n", __func__, status);
8303 return status;
8304}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008305
8306static void
8307nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8308{
8309 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008310 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008311
8312 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008313 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008314 &lgp->res.seq_res, task);
8315 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008316}
8317
8318static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8319{
8320 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008321
8322 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008323 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008324 dprintk("<-- %s\n", __func__);
8325}
8326
8327static int
8328nfs4_layoutget_handle_exception(struct rpc_task *task,
8329 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8330{
Trond Myklebustee314c22012-10-01 17:25:48 -07008331 struct inode *inode = lgp->args.inode;
8332 struct nfs_server *server = NFS_SERVER(inode);
8333 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008334 int nfs4err = task->tk_status;
8335 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008336 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008337
Boaz Harroshed7e5422014-01-22 20:34:54 +02008338 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008339
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008340 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008341 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008342 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008343
8344 /*
8345 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8346 * on the file. set tk_status to -ENODATA to tell upper layer to
8347 * retry go inband.
8348 */
8349 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008350 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008351 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008352 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008353 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8354 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8355 */
8356 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008357 status = -EOVERFLOW;
8358 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008359 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008360 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008361 * (or clients) writing to the same RAID stripe except when
8362 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008363 *
8364 * Treat it like we would RECALLCONFLICT -- we retry for a little
8365 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008366 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008367 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008368 if (lgp->args.minlength == 0) {
8369 status = -EOVERFLOW;
8370 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008371 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008372 status = -EBUSY;
8373 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008374 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008375 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008376 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008377 case -NFS4ERR_DELEG_REVOKED:
8378 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008379 case -NFS4ERR_EXPIRED:
8380 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008381 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008382 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008383 lo = NFS_I(inode)->layout;
8384 /* If the open stateid was bad, then recover it. */
8385 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8386 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008387 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008388 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008389 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008390 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008391 break;
8392 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008393
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008394 /*
8395 * Mark the bad layout state as invalid, then retry
8396 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008397 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008398 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008399 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008400 pnfs_free_lseg_list(&head);
8401 status = -EAGAIN;
8402 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008403 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008404
Trond Myklebust8ac09252017-01-23 22:44:12 -05008405 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008406 err = nfs4_handle_exception(server, nfs4err, exception);
8407 if (!status) {
8408 if (exception->retry)
8409 status = -EAGAIN;
8410 else
8411 status = err;
8412 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008413out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008414 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008415 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008416}
8417
Idan Kedar85541162012-08-02 11:47:10 +03008418static size_t max_response_pages(struct nfs_server *server)
8419{
8420 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8421 return nfs_page_array_len(0, max_resp_sz);
8422}
8423
8424static void nfs4_free_pages(struct page **pages, size_t size)
8425{
8426 int i;
8427
8428 if (!pages)
8429 return;
8430
8431 for (i = 0; i < size; i++) {
8432 if (!pages[i])
8433 break;
8434 __free_page(pages[i]);
8435 }
8436 kfree(pages);
8437}
8438
8439static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8440{
8441 struct page **pages;
8442 int i;
8443
8444 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8445 if (!pages) {
8446 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8447 return NULL;
8448 }
8449
8450 for (i = 0; i < size; i++) {
8451 pages[i] = alloc_page(gfp_flags);
8452 if (!pages[i]) {
8453 dprintk("%s: failed to allocate page\n", __func__);
8454 nfs4_free_pages(pages, size);
8455 return NULL;
8456 }
8457 }
8458
8459 return pages;
8460}
8461
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008462static void nfs4_layoutget_release(void *calldata)
8463{
8464 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008465 struct inode *inode = lgp->args.inode;
8466 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008467 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008468
8469 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008470 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008471 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008472 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008473 put_nfs_open_context(lgp->args.ctx);
8474 kfree(calldata);
8475 dprintk("<-- %s\n", __func__);
8476}
8477
8478static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8479 .rpc_call_prepare = nfs4_layoutget_prepare,
8480 .rpc_call_done = nfs4_layoutget_done,
8481 .rpc_release = nfs4_layoutget_release,
8482};
8483
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008484struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008485nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008486{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008487 struct inode *inode = lgp->args.inode;
8488 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008489 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008490 struct rpc_task *task;
8491 struct rpc_message msg = {
8492 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8493 .rpc_argp = &lgp->args,
8494 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008495 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008496 };
8497 struct rpc_task_setup task_setup_data = {
8498 .rpc_client = server->client,
8499 .rpc_message = &msg,
8500 .callback_ops = &nfs4_layoutget_call_ops,
8501 .callback_data = lgp,
8502 .flags = RPC_TASK_ASYNC,
8503 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008504 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008505 struct nfs4_exception exception = {
8506 .inode = inode,
8507 .timeout = *timeout,
8508 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008509 int status = 0;
8510
8511 dprintk("--> %s\n", __func__);
8512
Peng Tao4bd5a982014-11-17 11:05:17 +08008513 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8514 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8515
Idan Kedar85541162012-08-02 11:47:10 +03008516 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8517 if (!lgp->args.layout.pages) {
8518 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008519 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008520 }
8521 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8522
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008523 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008524 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008525 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008526
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008527 task = rpc_run_task(&task_setup_data);
8528 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008529 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008530 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008531 if (status == 0) {
8532 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8533 *timeout = exception.timeout;
8534 }
8535
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008536 trace_nfs4_layoutget(lgp->args.ctx,
8537 &lgp->args.range,
8538 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008539 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008540 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008541
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008542 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8543 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008544 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008545 rpc_put_task(task);
8546 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008547 if (status)
8548 return ERR_PTR(status);
8549 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008550}
8551
Benny Halevycbe82602011-05-22 19:52:37 +03008552static void
8553nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8554{
8555 struct nfs4_layoutreturn *lrp = calldata;
8556
8557 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008558 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008559 &lrp->args.seq_args,
8560 &lrp->res.seq_res,
8561 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008562}
8563
8564static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8565{
8566 struct nfs4_layoutreturn *lrp = calldata;
8567 struct nfs_server *server;
8568
8569 dprintk("--> %s\n", __func__);
8570
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008571 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008572 return;
8573
8574 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008575 switch (task->tk_status) {
8576 default:
8577 task->tk_status = 0;
8578 case 0:
8579 break;
8580 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008581 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008582 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008583 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008584 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008585 return;
8586 }
Benny Halevycbe82602011-05-22 19:52:37 +03008587 dprintk("<-- %s\n", __func__);
8588}
8589
8590static void nfs4_layoutreturn_release(void *calldata)
8591{
8592 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008593 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008594
8595 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008596 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008597 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008598 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008599 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8600 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008601 pnfs_put_layout_hdr(lrp->args.layout);
8602 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008603 kfree(calldata);
8604 dprintk("<-- %s\n", __func__);
8605}
8606
8607static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8608 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8609 .rpc_call_done = nfs4_layoutreturn_done,
8610 .rpc_release = nfs4_layoutreturn_release,
8611};
8612
Peng Tao6c166052014-11-17 09:30:40 +08008613int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008614{
8615 struct rpc_task *task;
8616 struct rpc_message msg = {
8617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8618 .rpc_argp = &lrp->args,
8619 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008620 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008621 };
8622 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008623 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008624 .rpc_message = &msg,
8625 .callback_ops = &nfs4_layoutreturn_call_ops,
8626 .callback_data = lrp,
8627 };
Peng Tao6c166052014-11-17 09:30:40 +08008628 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008629
Andrew Elble99ade3c2015-12-02 09:39:51 -05008630 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8631 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8632 &task_setup_data.rpc_client, &msg);
8633
Benny Halevycbe82602011-05-22 19:52:37 +03008634 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008635 if (!sync) {
8636 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8637 if (!lrp->inode) {
8638 nfs4_layoutreturn_release(lrp);
8639 return -EAGAIN;
8640 }
8641 task_setup_data.flags |= RPC_TASK_ASYNC;
8642 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008643 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008644 task = rpc_run_task(&task_setup_data);
8645 if (IS_ERR(task))
8646 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008647 if (sync)
8648 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008649 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008650 dprintk("<-- %s status=%d\n", __func__, status);
8651 rpc_put_task(task);
8652 return status;
8653}
8654
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008655static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008656_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8657 struct pnfs_device *pdev,
8658 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008659{
8660 struct nfs4_getdeviceinfo_args args = {
8661 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008662 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8663 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008664 };
8665 struct nfs4_getdeviceinfo_res res = {
8666 .pdev = pdev,
8667 };
8668 struct rpc_message msg = {
8669 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8670 .rpc_argp = &args,
8671 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008672 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008673 };
8674 int status;
8675
8676 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008677 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008678 if (res.notification & ~args.notify_types)
8679 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008680 if (res.notification != args.notify_types)
8681 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008682
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008683 dprintk("<-- %s status=%d\n", __func__, status);
8684
8685 return status;
8686}
8687
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008688int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8689 struct pnfs_device *pdev,
8690 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008691{
8692 struct nfs4_exception exception = { };
8693 int err;
8694
8695 do {
8696 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008697 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008698 &exception);
8699 } while (exception.retry);
8700 return err;
8701}
8702EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8703
Andy Adamson863a3c62011-03-23 13:27:54 +00008704static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8705{
8706 struct nfs4_layoutcommit_data *data = calldata;
8707 struct nfs_server *server = NFS_SERVER(data->args.inode);
8708
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008709 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008710 &data->args.seq_args,
8711 &data->res.seq_res,
8712 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008713}
8714
8715static void
8716nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8717{
8718 struct nfs4_layoutcommit_data *data = calldata;
8719 struct nfs_server *server = NFS_SERVER(data->args.inode);
8720
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008721 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008722 return;
8723
8724 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008725 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8726 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8727 case -NFS4ERR_BADLAYOUT: /* no layout */
8728 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008729 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008730 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008731 break;
8732 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008733 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008734 rpc_restart_call_prepare(task);
8735 return;
8736 }
8737 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008738}
8739
8740static void nfs4_layoutcommit_release(void *calldata)
8741{
8742 struct nfs4_layoutcommit_data *data = calldata;
8743
Andy Adamsondb29c082011-07-30 20:52:38 -04008744 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008745 nfs_post_op_update_inode_force_wcc(data->args.inode,
8746 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008747 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008748 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008749 kfree(data);
8750}
8751
8752static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8753 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8754 .rpc_call_done = nfs4_layoutcommit_done,
8755 .rpc_release = nfs4_layoutcommit_release,
8756};
8757
8758int
Andy Adamsonef311532011-03-12 02:58:10 -05008759nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008760{
8761 struct rpc_message msg = {
8762 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8763 .rpc_argp = &data->args,
8764 .rpc_resp = &data->res,
8765 .rpc_cred = data->cred,
8766 };
8767 struct rpc_task_setup task_setup_data = {
8768 .task = &data->task,
8769 .rpc_client = NFS_CLIENT(data->args.inode),
8770 .rpc_message = &msg,
8771 .callback_ops = &nfs4_layoutcommit_ops,
8772 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008773 };
8774 struct rpc_task *task;
8775 int status = 0;
8776
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008777 dprintk("NFS: initiating layoutcommit call. sync %d "
8778 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008779 data->args.lastbytewritten,
8780 data->args.inode->i_ino);
8781
Trond Myklebust472e2592015-02-05 16:50:30 -05008782 if (!sync) {
8783 data->inode = nfs_igrab_and_active(data->args.inode);
8784 if (data->inode == NULL) {
8785 nfs4_layoutcommit_release(data);
8786 return -EAGAIN;
8787 }
8788 task_setup_data.flags = RPC_TASK_ASYNC;
8789 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008790 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008791 task = rpc_run_task(&task_setup_data);
8792 if (IS_ERR(task))
8793 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008794 if (sync)
8795 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008796 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008797 dprintk("%s: status %d\n", __func__, status);
8798 rpc_put_task(task);
8799 return status;
8800}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008801
Andy Adamson97431202013-08-08 10:57:56 -04008802/**
8803 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8804 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8805 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008806static int
8807_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008808 struct nfs_fsinfo *info,
8809 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008810{
8811 struct nfs41_secinfo_no_name_args args = {
8812 .style = SECINFO_STYLE_CURRENT_FH,
8813 };
8814 struct nfs4_secinfo_res res = {
8815 .flavors = flavors,
8816 };
8817 struct rpc_message msg = {
8818 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8819 .rpc_argp = &args,
8820 .rpc_resp = &res,
8821 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008822 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008823 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008824 int status;
8825
8826 if (use_integrity) {
8827 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008828 cred = nfs4_get_clid_cred(server->nfs_client);
8829 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008830 }
8831
8832 dprintk("--> %s\n", __func__);
8833 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8834 &res.seq_res, 0);
8835 dprintk("<-- %s status=%d\n", __func__, status);
8836
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008837 if (cred)
8838 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008839
8840 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008841}
8842
8843static int
8844nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8845 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8846{
8847 struct nfs4_exception exception = { };
8848 int err;
8849 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008850 /* first try using integrity protection */
8851 err = -NFS4ERR_WRONGSEC;
8852
8853 /* try to use integrity protection with machine cred */
8854 if (_nfs4_is_integrity_protected(server->nfs_client))
8855 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8856 flavors, true);
8857
8858 /*
8859 * if unable to use integrity protection, or SECINFO with
8860 * integrity protection returns NFS4ERR_WRONGSEC (which is
8861 * disallowed by spec, but exists in deployed servers) use
8862 * the current filesystem's rpc_client and the user cred.
8863 */
8864 if (err == -NFS4ERR_WRONGSEC)
8865 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8866 flavors, false);
8867
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008868 switch (err) {
8869 case 0:
8870 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008871 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008872 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008873 default:
8874 err = nfs4_handle_exception(server, err, &exception);
8875 }
8876 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008877out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008878 return err;
8879}
8880
8881static int
8882nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8883 struct nfs_fsinfo *info)
8884{
8885 int err;
8886 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008887 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008888 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008889 struct nfs4_secinfo4 *secinfo;
8890 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008891
8892 page = alloc_page(GFP_KERNEL);
8893 if (!page) {
8894 err = -ENOMEM;
8895 goto out;
8896 }
8897
8898 flavors = page_address(page);
8899 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8900
8901 /*
8902 * Fall back on "guess and check" method if
8903 * the server doesn't support SECINFO_NO_NAME
8904 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008905 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008906 err = nfs4_find_root_sec(server, fhandle, info);
8907 goto out_freepage;
8908 }
8909 if (err)
8910 goto out_freepage;
8911
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008912 for (i = 0; i < flavors->num_flavors; i++) {
8913 secinfo = &flavors->flavors[i];
8914
8915 switch (secinfo->flavor) {
8916 case RPC_AUTH_NULL:
8917 case RPC_AUTH_UNIX:
8918 case RPC_AUTH_GSS:
8919 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8920 &secinfo->flavor_info);
8921 break;
8922 default:
8923 flavor = RPC_AUTH_MAXFLAVOR;
8924 break;
8925 }
8926
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008927 if (!nfs_auth_info_match(&server->auth_info, flavor))
8928 flavor = RPC_AUTH_MAXFLAVOR;
8929
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008930 if (flavor != RPC_AUTH_MAXFLAVOR) {
8931 err = nfs4_lookup_root_sec(server, fhandle,
8932 info, flavor);
8933 if (!err)
8934 break;
8935 }
8936 }
8937
8938 if (flavor == RPC_AUTH_MAXFLAVOR)
8939 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008940
8941out_freepage:
8942 put_page(page);
8943 if (err == -EACCES)
8944 return -EPERM;
8945out:
8946 return err;
8947}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008948
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008949static int _nfs41_test_stateid(struct nfs_server *server,
8950 nfs4_stateid *stateid,
8951 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008952{
8953 int status;
8954 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008955 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008956 };
8957 struct nfs41_test_stateid_res res;
8958 struct rpc_message msg = {
8959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8960 .rpc_argp = &args,
8961 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008962 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008963 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008964 struct rpc_clnt *rpc_client = server->client;
8965
8966 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8967 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008968
Chuck Lever38527b12012-07-11 16:30:23 -04008969 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008970 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008971 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008972 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008973 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008974 if (status != NFS_OK) {
8975 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008976 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008977 }
8978 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008979 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008980}
8981
Trond Myklebust43912bb2016-09-22 13:38:56 -04008982static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8983 int err, struct nfs4_exception *exception)
8984{
8985 exception->retry = 0;
8986 switch(err) {
8987 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008988 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008989 nfs4_handle_exception(server, err, exception);
8990 break;
8991 case -NFS4ERR_BADSESSION:
8992 case -NFS4ERR_BADSLOT:
8993 case -NFS4ERR_BAD_HIGH_SLOT:
8994 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8995 case -NFS4ERR_DEADSESSION:
8996 nfs4_do_handle_exception(server, err, exception);
8997 }
8998}
8999
Chuck Lever38527b12012-07-11 16:30:23 -04009000/**
9001 * nfs41_test_stateid - perform a TEST_STATEID operation
9002 *
9003 * @server: server / transport on which to perform the operation
9004 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009005 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009006 *
9007 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9008 * Otherwise a negative NFS4ERR value is returned if the operation
9009 * failed or the state ID is not currently valid.
9010 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009011static int nfs41_test_stateid(struct nfs_server *server,
9012 nfs4_stateid *stateid,
9013 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009014{
9015 struct nfs4_exception exception = { };
9016 int err;
9017 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009018 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009019 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009020 } while (exception.retry);
9021 return err;
9022}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009023
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009024struct nfs_free_stateid_data {
9025 struct nfs_server *server;
9026 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009027 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009028};
9029
9030static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9031{
9032 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009033 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009034 &data->args.seq_args,
9035 &data->res.seq_res,
9036 task);
9037}
9038
9039static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9040{
9041 struct nfs_free_stateid_data *data = calldata;
9042
9043 nfs41_sequence_done(task, &data->res.seq_res);
9044
9045 switch (task->tk_status) {
9046 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009047 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009048 rpc_restart_call_prepare(task);
9049 }
9050}
9051
9052static void nfs41_free_stateid_release(void *calldata)
9053{
9054 kfree(calldata);
9055}
9056
Trond Myklebust17f26b12013-08-21 15:48:42 -04009057static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009058 .rpc_call_prepare = nfs41_free_stateid_prepare,
9059 .rpc_call_done = nfs41_free_stateid_done,
9060 .rpc_release = nfs41_free_stateid_release,
9061};
9062
9063static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009064 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009065 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009066 bool privileged)
9067{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009068 struct rpc_message msg = {
9069 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009070 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009071 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009072 struct rpc_task_setup task_setup = {
9073 .rpc_client = server->client,
9074 .rpc_message = &msg,
9075 .callback_ops = &nfs41_free_stateid_ops,
9076 .flags = RPC_TASK_ASYNC,
9077 };
9078 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009079
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009080 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9081 &task_setup.rpc_client, &msg);
9082
Chuck Lever38527b12012-07-11 16:30:23 -04009083 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009084 data = kmalloc(sizeof(*data), GFP_NOFS);
9085 if (!data)
9086 return ERR_PTR(-ENOMEM);
9087 data->server = server;
9088 nfs4_stateid_copy(&data->args.stateid, stateid);
9089
9090 task_setup.callback_data = data;
9091
9092 msg.rpc_argp = &data->args;
9093 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009094 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009095 if (privileged)
9096 nfs4_set_sequence_privileged(&data->args.seq_args);
9097
9098 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009099}
9100
Chuck Lever38527b12012-07-11 16:30:23 -04009101/**
9102 * nfs41_free_stateid - perform a FREE_STATEID operation
9103 *
9104 * @server: server / transport on which to perform the operation
9105 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009106 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009107 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009108 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009109 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009110 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009111static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009112 const nfs4_stateid *stateid,
9113 struct rpc_cred *cred,
9114 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009115{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009116 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009117
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009118 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009119 if (IS_ERR(task))
9120 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009121 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009122 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009123}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009124
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009125static void
9126nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009127{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009128 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009129
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009130 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009131 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009132}
9133
Trond Myklebust36281ca2012-03-04 18:13:56 -05009134static bool nfs41_match_stateid(const nfs4_stateid *s1,
9135 const nfs4_stateid *s2)
9136{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009137 if (s1->type != s2->type)
9138 return false;
9139
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009140 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009141 return false;
9142
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009143 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009144 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009145
Anna Schumaker045c5512017-01-11 16:59:48 -05009146 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009147}
9148
Andy Adamson557134a2009-04-01 09:21:53 -04009149#endif /* CONFIG_NFS_V4_1 */
9150
Trond Myklebust36281ca2012-03-04 18:13:56 -05009151static bool nfs4_match_stateid(const nfs4_stateid *s1,
9152 const nfs4_stateid *s2)
9153{
Trond Myklebustf597c532012-03-04 18:13:56 -05009154 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009155}
9156
9157
Trond Myklebust17280172012-03-11 13:11:00 -04009158static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009159 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009160 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009161 .recover_open = nfs4_open_reclaim,
9162 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009163 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009164 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009165};
9166
Andy Adamson591d71c2009-04-01 09:22:47 -04009167#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009168static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009169 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9170 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9171 .recover_open = nfs4_open_reclaim,
9172 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009173 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009174 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009175 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009176};
9177#endif /* CONFIG_NFS_V4_1 */
9178
Trond Myklebust17280172012-03-11 13:11:00 -04009179static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009180 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009181 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009182 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009183 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009184 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009185};
9186
Andy Adamson591d71c2009-04-01 09:22:47 -04009187#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009188static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009189 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9190 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009191 .recover_open = nfs41_open_expired,
9192 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009193 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009194};
9195#endif /* CONFIG_NFS_V4_1 */
9196
Trond Myklebust17280172012-03-11 13:11:00 -04009197static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009198 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009199 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009200 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009201};
9202
9203#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009204static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009205 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009206 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009207 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009208};
9209#endif
9210
Chuck Leverec011fe2013-10-17 14:12:39 -04009211static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009212 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009213 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009214};
9215
9216#if defined(CONFIG_NFS_V4_1)
9217static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009218 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009219 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009220};
9221#endif /* CONFIG_NFS_V4_1 */
9222
Trond Myklebust97dc1352010-06-16 09:52:26 -04009223static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9224 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009225 .init_caps = NFS_CAP_READDIRPLUS
9226 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009227 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009228 .init_client = nfs40_init_client,
9229 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009230 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009231 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009232 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009233 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009234 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009235 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009236 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9237 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9238 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009239 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009240};
9241
9242#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009243static struct nfs_seqid *
9244nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9245{
9246 return NULL;
9247}
9248
Trond Myklebust97dc1352010-06-16 09:52:26 -04009249static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9250 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009251 .init_caps = NFS_CAP_READDIRPLUS
9252 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009253 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009254 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009255 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009256 .init_client = nfs41_init_client,
9257 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009258 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009259 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009260 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009261 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009262 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009263 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009264 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009265 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9266 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9267 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009268 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009269};
9270#endif
9271
Steve Dickson42c2c422013-05-22 12:50:38 -04009272#if defined(CONFIG_NFS_V4_2)
9273static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9274 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009275 .init_caps = NFS_CAP_READDIRPLUS
9276 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009277 | NFS_CAP_POSIX_LOCK
9278 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009279 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009280 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009281 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009282 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009283 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009284 | NFS_CAP_LAYOUTSTATS
9285 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009286 .init_client = nfs41_init_client,
9287 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009288 .match_stateid = nfs41_match_stateid,
9289 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009290 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009291 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009292 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009293 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009294 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009295 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9296 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9297 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009298 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009299};
9300#endif
9301
Trond Myklebust97dc1352010-06-16 09:52:26 -04009302const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9303 [0] = &nfs_v4_0_minor_ops,
9304#if defined(CONFIG_NFS_V4_1)
9305 [1] = &nfs_v4_1_minor_ops,
9306#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009307#if defined(CONFIG_NFS_V4_2)
9308 [2] = &nfs_v4_2_minor_ops,
9309#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009310};
9311
Trond Myklebust13997822016-07-24 17:10:52 -04009312static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009313{
9314 ssize_t error, error2;
9315
9316 error = generic_listxattr(dentry, list, size);
9317 if (error < 0)
9318 return error;
9319 if (list) {
9320 list += error;
9321 size -= error;
9322 }
9323
9324 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9325 if (error2 < 0)
9326 return error2;
9327 return error + error2;
9328}
9329
Trond Myklebust17f26b12013-08-21 15:48:42 -04009330static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009331 .create = nfs_create,
9332 .lookup = nfs_lookup,
9333 .atomic_open = nfs_atomic_open,
9334 .link = nfs_link,
9335 .unlink = nfs_unlink,
9336 .symlink = nfs_symlink,
9337 .mkdir = nfs_mkdir,
9338 .rmdir = nfs_rmdir,
9339 .mknod = nfs_mknod,
9340 .rename = nfs_rename,
9341 .permission = nfs_permission,
9342 .getattr = nfs_getattr,
9343 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009344 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009345};
9346
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009347static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009348 .permission = nfs_permission,
9349 .getattr = nfs_getattr,
9350 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009351 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009352};
9353
David Howells509de812006-08-22 20:06:11 -04009354const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009355 .version = 4, /* protocol version */
9356 .dentry_ops = &nfs4_dentry_operations,
9357 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009358 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009359 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009360 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009361 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009362 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009363 .getattr = nfs4_proc_getattr,
9364 .setattr = nfs4_proc_setattr,
9365 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009366 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009367 .access = nfs4_proc_access,
9368 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009369 .create = nfs4_proc_create,
9370 .remove = nfs4_proc_remove,
9371 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009372 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009373 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009374 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009375 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009376 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009377 .link = nfs4_proc_link,
9378 .symlink = nfs4_proc_symlink,
9379 .mkdir = nfs4_proc_mkdir,
9380 .rmdir = nfs4_proc_remove,
9381 .readdir = nfs4_proc_readdir,
9382 .mknod = nfs4_proc_mknod,
9383 .statfs = nfs4_proc_statfs,
9384 .fsinfo = nfs4_proc_fsinfo,
9385 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009386 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009387 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009388 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009389 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009390 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009391 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009392 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009393 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009394 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009395 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009396 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009397 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009398 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009399 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009400 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009401 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009402 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009403 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009404 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009405 .create_server = nfs4_create_server,
9406 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009407};
9408
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009409static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009410 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009411 .list = nfs4_xattr_list_nfs4_acl,
9412 .get = nfs4_xattr_get_nfs4_acl,
9413 .set = nfs4_xattr_set_nfs4_acl,
9414};
9415
9416const struct xattr_handler *nfs4_xattr_handlers[] = {
9417 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009418#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9419 &nfs4_xattr_nfs4_label_handler,
9420#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009421 NULL
9422};
9423
Linus Torvalds1da177e2005-04-16 15:20:36 -07009424/*
9425 * Local variables:
9426 * c-basic-offset: 8
9427 * End:
9428 */