blob: 15e91f003adc5871e74b62a6267ba03f71be9253 [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 Schumaker9dd9107f2017-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 Schumaker9dd9107f2017-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 Myklebust6926afd12012-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 Myklebust6926afd12012-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 Myklebust6926afd12012-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 Myklebust6926afd12012-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;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002239 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002240
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 */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002253 if (S_ISDIR(state->inode->i_mode))
2254 mask = NFS4_ACCESS_LOOKUP;
2255 else
2256 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002257 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002258 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002259
2260 cache.cred = cred;
2261 cache.jiffies = jiffies;
2262 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2263 nfs_access_add_cache(state->inode, &cache);
2264
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002265 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2266 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002267 return 0;
2268
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002269 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002270}
2271
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002272/*
2273 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2274 */
2275static int _nfs4_proc_open(struct nfs4_opendata *data)
2276{
David Howells2b0143b2015-03-17 22:25:59 +00002277 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002278 struct nfs_server *server = NFS_SERVER(dir);
2279 struct nfs_openargs *o_arg = &data->o_arg;
2280 struct nfs_openres *o_res = &data->o_res;
2281 int status;
2282
2283 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002284 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002285 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002286 if (status != 0) {
2287 if (status == -NFS4ERR_BADNAME &&
2288 !(o_arg->open_flags & O_CREAT))
2289 return -ENOENT;
2290 return status;
2291 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002292
Trond Myklebust6926afd12012-01-07 13:22:46 -05002293 nfs_fattr_map_and_free_names(server, &data->f_attr);
2294
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002295 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002296 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002297 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002298 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002299 data->file_created = true;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002300 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002301 update_changeattr(dir, &o_res->cinfo,
2302 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002303 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002304 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2305 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002307 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002309 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002311 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2312 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002313 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002314 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002315 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316}
2317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318/*
2319 * OPEN_EXPIRED:
2320 * reclaim state on the server after a network partition.
2321 * Assumes caller holds the appropriate lock
2322 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002323static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002325 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002326 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002328 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002329 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002330 if (IS_ERR(opendata))
2331 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002332 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002333 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002334 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002335 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002336 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337}
2338
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002339static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002340{
Trond Myklebust539cd032007-06-05 11:46:42 -04002341 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002342 struct nfs4_exception exception = { };
2343 int err;
2344
2345 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002346 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002347 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002348 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2349 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002350 switch (err) {
2351 default:
2352 goto out;
2353 case -NFS4ERR_GRACE:
2354 case -NFS4ERR_DELAY:
2355 nfs4_handle_exception(server, err, &exception);
2356 err = 0;
2357 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002358 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002359out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002360 return err;
2361}
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2364{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002366 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Trond Myklebust864472e2006-01-03 09:55:15 +01002368 ctx = nfs4_state_find_open_context(state);
2369 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002370 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002371 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002372 put_nfs_open_context(ctx);
2373 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374}
2375
Trond Myklebust41020b62016-09-22 13:38:58 -04002376static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2377 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002378{
Trond Myklebust41020b62016-09-22 13:38:58 -04002379 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002380 write_seqlock(&state->seqlock);
2381 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2382 write_sequnlock(&state->seqlock);
2383 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2384}
2385
2386static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2387{
2388 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002389 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002390}
2391
2392static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2393{
2394 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2395 nfs40_clear_delegation_stateid(state);
2396 return nfs4_open_expired(sp, state);
2397}
2398
Trond Myklebust45870d62016-09-22 13:38:59 -04002399static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2400 nfs4_stateid *stateid,
2401 struct rpc_cred *cred)
2402{
2403 return -NFS4ERR_BAD_STATEID;
2404}
2405
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002406#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002407static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2408 nfs4_stateid *stateid,
2409 struct rpc_cred *cred)
2410{
2411 int status;
2412
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002413 switch (stateid->type) {
2414 default:
2415 break;
2416 case NFS4_INVALID_STATEID_TYPE:
2417 case NFS4_SPECIAL_STATEID_TYPE:
2418 return -NFS4ERR_BAD_STATEID;
2419 case NFS4_REVOKED_STATEID_TYPE:
2420 goto out_free;
2421 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002422
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002423 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002424 switch (status) {
2425 case -NFS4ERR_EXPIRED:
2426 case -NFS4ERR_ADMIN_REVOKED:
2427 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002428 break;
2429 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002430 return status;
2431 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002432out_free:
2433 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002434 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002435 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002436}
2437
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002438static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002439{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002440 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002441 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002442 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002443 struct rpc_cred *cred;
2444 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002445
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002446 /* Get the delegation credential for use by test/free_stateid */
2447 rcu_read_lock();
2448 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002449 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002450 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002451 return;
2452 }
2453
2454 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002455 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2456 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2457 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002458 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002459 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002460 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002461 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002462
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002463 cred = get_rpccred(delegation->cred);
2464 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002465 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002466 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002467 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002468 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002469
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002470 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002471}
2472
2473/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002474 * nfs41_check_expired_locks - possibly free a lock stateid
2475 *
2476 * @state: NFSv4 state for an inode
2477 *
2478 * Returns NFS_OK if recovery for this stateid is now finished.
2479 * Otherwise a negative NFS4ERR value is returned.
2480 */
2481static int nfs41_check_expired_locks(struct nfs4_state *state)
2482{
2483 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002484 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002485 struct nfs_server *server = NFS_SERVER(state->inode);
2486
2487 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2488 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002489
2490 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002491 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2492 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2493 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2494
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002495 atomic_inc(&lsp->ls_count);
2496 spin_unlock(&state->state_lock);
2497
2498 nfs4_put_lock_state(prev);
2499 prev = lsp;
2500
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002501 status = nfs41_test_and_free_expired_stateid(server,
2502 &lsp->ls_stateid,
2503 cred);
2504 trace_nfs4_test_lock_stateid(state, lsp, status);
2505 if (status == -NFS4ERR_EXPIRED ||
2506 status == -NFS4ERR_BAD_STATEID) {
2507 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002508 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002509 if (!recover_lost_locks)
2510 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2511 } else if (status != NFS_OK) {
2512 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002513 nfs4_put_lock_state(prev);
2514 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002515 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002516 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002517 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002518 }
2519 spin_unlock(&state->state_lock);
2520 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002521out:
2522 return ret;
2523}
2524
2525/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002526 * nfs41_check_open_stateid - possibly free an open stateid
2527 *
2528 * @state: NFSv4 state for an inode
2529 *
2530 * Returns NFS_OK if recovery for this stateid is now finished.
2531 * Otherwise a negative NFS4ERR value is returned.
2532 */
2533static int nfs41_check_open_stateid(struct nfs4_state *state)
2534{
2535 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002536 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002537 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002538 int status;
2539
Trond Myklebustb134fc42016-09-22 13:39:16 -04002540 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002541 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2542 if (nfs4_have_delegation(state->inode, state->state))
2543 return NFS_OK;
2544 return -NFS4ERR_OPENMODE;
2545 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002546 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002547 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002548 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002549 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002550 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002551 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2552 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2553 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002554 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002555 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002556 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002557 if (status != NFS_OK)
2558 return status;
2559 if (nfs_open_stateid_recover_openmode(state))
2560 return -NFS4ERR_OPENMODE;
2561 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002562}
2563
2564static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2565{
Chuck Levereb64cf92012-07-11 16:30:05 -04002566 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002567
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002568 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002569 status = nfs41_check_expired_locks(state);
2570 if (status != NFS_OK)
2571 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002572 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002573 if (status != NFS_OK)
2574 status = nfs4_open_expired(sp, state);
2575 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002576}
2577#endif
2578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002580 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2581 * fields corresponding to attributes that were used to store the verifier.
2582 * Make sure we clobber those fields in the later setattr call
2583 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002584static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2585 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002586{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002587 const u32 *attrset = opendata->o_res.attrset;
2588
2589 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002590 !(sattr->ia_valid & ATTR_ATIME_SET))
2591 sattr->ia_valid |= ATTR_ATIME;
2592
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002593 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002594 !(sattr->ia_valid & ATTR_MTIME_SET))
2595 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002596
2597 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002598 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
Benjamin Coddington501e7a42017-06-02 11:21:34 -04002599 (attrset[1] & FATTR4_WORD1_MODE ||
2600 attrset[2] & FATTR4_WORD2_MODE_UMASK))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002601 sattr->ia_valid &= ~ATTR_MODE;
2602
2603 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2604 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002605}
2606
Trond Myklebustc21443c2013-02-07 14:26:21 -05002607static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2608 fmode_t fmode,
2609 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002610 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002611{
2612 struct nfs4_state_owner *sp = opendata->owner;
2613 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002614 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002615 struct nfs4_state *state;
2616 unsigned int seq;
2617 int ret;
2618
2619 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2620
2621 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002622 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002623 goto out;
2624
2625 state = nfs4_opendata_to_nfs4_state(opendata);
2626 ret = PTR_ERR(state);
2627 if (IS_ERR(state))
2628 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002629 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002630 if (server->caps & NFS_CAP_POSIX_LOCK)
2631 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002632 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2633 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002634
Trond Myklebust275bb302013-05-29 13:11:28 -04002635 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002636 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002637 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002638 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002639 alias = d_exact_alias(dentry, state->inode);
2640 if (!alias)
2641 alias = d_splice_alias(igrab(state->inode), dentry);
2642 /* d_splice_alias() can't fail here - it's a non-directory */
2643 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002644 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002645 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002646 }
2647 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002648 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002649 }
2650
Trond Myklebustc21443c2013-02-07 14:26:21 -05002651 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2652 if (ret != 0)
2653 goto out;
2654
David Howells2b0143b2015-03-17 22:25:59 +00002655 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002656 nfs_inode_attach_open_context(ctx);
2657 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2658 nfs4_schedule_stateid_recovery(server, state);
2659 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002660out:
2661 return ret;
2662}
2663
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002664/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002665 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 */
Andy Adamson82be4172012-05-23 05:02:35 -04002667static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002668 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002669 int flags,
2670 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002671 struct nfs4_label *label,
2672 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673{
2674 struct nfs4_state_owner *sp;
2675 struct nfs4_state *state = NULL;
2676 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002677 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002678 struct dentry *dentry = ctx->dentry;
2679 struct rpc_cred *cred = ctx->cred;
2680 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2681 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002682 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002683 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002684 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
2686 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002688 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2689 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2691 goto out_err;
2692 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002693 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002694 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002695 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002696 if (d_really_is_positive(dentry))
2697 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002698 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002699 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002700 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002701 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002702 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002703 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002704 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
David Quigley14c43f72013-05-22 12:50:43 -04002706 if (label) {
2707 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2708 if (IS_ERR(olabel)) {
2709 status = PTR_ERR(olabel);
2710 goto err_opendata_put;
2711 }
2712 }
2713
Trond Myklebuste911b812014-03-26 13:24:37 -07002714 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2715 if (!opendata->f_attr.mdsthreshold) {
2716 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2717 if (!opendata->f_attr.mdsthreshold)
2718 goto err_free_label;
2719 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002720 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002721 }
David Howells2b0143b2015-03-17 22:25:59 +00002722 if (d_really_is_positive(dentry))
2723 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002724
Trond Myklebust3efb9722013-05-29 13:17:04 -04002725 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002726 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002727 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002728 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002729
NeilBrownefcbc042015-07-30 13:00:56 +10002730 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002731 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002732 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002733 /*
2734 * send create attributes which was not set by open
2735 * with an extra setattr.
2736 */
2737 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2738 nfs_fattr_init(opendata->o_res.f_attr);
2739 status = nfs4_do_setattr(state->inode, cred,
2740 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002741 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002742 if (status == 0) {
2743 nfs_setattr_update_inode(state->inode, sattr,
2744 opendata->o_res.f_attr);
2745 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2746 }
David Quigley1775fd32013-05-22 12:50:42 -04002747 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002748 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002749 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002750 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002751
Trond Myklebuste911b812014-03-26 13:24:37 -07002752 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002753 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002754 opendata->f_attr.mdsthreshold = NULL;
2755 }
Andy Adamson82be4172012-05-23 05:02:35 -04002756
David Quigley14c43f72013-05-22 12:50:43 -04002757 nfs4_label_free(olabel);
2758
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002759 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002762err_free_label:
2763 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002764err_opendata_put:
2765 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002766err_put_state_owner:
2767 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 return status;
2770}
2771
2772
Andy Adamson82be4172012-05-23 05:02:35 -04002773static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002774 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002775 int flags,
2776 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002777 struct nfs4_label *label,
2778 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002780 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 struct nfs4_exception exception = { };
2782 struct nfs4_state *res;
2783 int status;
2784
2785 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002786 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002787 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002788 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 if (status == 0)
2790 break;
2791 /* NOTE: BAD_SEQID means the server and client disagree about the
2792 * book-keeping w.r.t. state-changing operations
2793 * (OPEN/CLOSE/LOCK/LOCKU...)
2794 * It is actually a sign of a bug on the client or on the server.
2795 *
2796 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002797 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 * have unhashed the old state_owner for us, and that we can
2799 * therefore safely retry using a new one. We should still warn
2800 * the user though...
2801 */
2802 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002803 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002804 " returned a bad sequence-id error!\n",
2805 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 exception.retry = 1;
2807 continue;
2808 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002809 /*
2810 * BAD_STATEID on OPEN means that the server cancelled our
2811 * state before it received the OPEN_CONFIRM.
2812 * Recover by retrying the request as per the discussion
2813 * on Page 181 of RFC3530.
2814 */
2815 if (status == -NFS4ERR_BAD_STATEID) {
2816 exception.retry = 1;
2817 continue;
2818 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002819 if (status == -EAGAIN) {
2820 /* We must have found a delegation */
2821 exception.retry = 1;
2822 continue;
2823 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002824 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2825 continue;
2826 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 status, &exception));
2828 } while (exception.retry);
2829 return res;
2830}
2831
Trond Myklebust8487c472016-06-26 08:44:35 -04002832static int _nfs4_do_setattr(struct inode *inode,
2833 struct nfs_setattrargs *arg,
2834 struct nfs_setattrres *res,
2835 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002836 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002837{
2838 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002839 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04002840 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2841 .rpc_argp = arg,
2842 .rpc_resp = res,
2843 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002844 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002845 struct rpc_cred *delegation_cred = NULL;
2846 unsigned long timestamp = jiffies;
2847 fmode_t fmode;
2848 bool truncate;
2849 int status;
2850
2851 nfs_fattr_init(res->fattr);
2852
2853 /* Servers should only apply open mode checks for file size changes */
2854 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2855 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2856
2857 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2858 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002859 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002860 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002861 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002862 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002863 l_ctx = nfs_get_lock_context(ctx);
2864 if (IS_ERR(l_ctx))
2865 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002866 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2867 &arg->stateid, &delegation_cred);
2868 nfs_put_lock_context(l_ctx);
2869 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002870 return -EBADF;
2871 } else
2872 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2873 if (delegation_cred)
2874 msg.rpc_cred = delegation_cred;
2875
2876 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2877
2878 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002879 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002880 renew_lease(server, timestamp);
2881 trace_nfs4_setattr(inode, &arg->stateid, status);
2882 return status;
2883}
2884
2885static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2886 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002887 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002888 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002890 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002891 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002892 struct nfs_setattrargs arg = {
2893 .fh = NFS_FH(inode),
2894 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 .server = server,
2896 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002897 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002898 };
2899 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002901 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002903 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002904 struct nfs4_exception exception = {
2905 .state = state,
2906 .inode = inode,
2907 .stateid = &arg.stateid,
2908 };
2909 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
David Quigleyaa9c2662013-05-22 12:50:44 -04002911 arg.bitmask = nfs4_bitmask(server, ilabel);
2912 if (ilabel)
2913 arg.bitmask = nfs4_bitmask(server, olabel);
2914
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 do {
NeilBrown29b59f92016-10-13 15:26:47 +11002916 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04002917 switch (err) {
2918 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002919 if (!(sattr->ia_valid & ATTR_SIZE)) {
2920 pr_warn_once("NFSv4: server %s is incorrectly "
2921 "applying open mode checks to "
2922 "a SETATTR that is not "
2923 "changing file size.\n",
2924 server->nfs_client->cl_hostname);
2925 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002926 if (state && !(state->state & FMODE_WRITE)) {
2927 err = -EBADF;
2928 if (sattr->ia_valid & ATTR_OPEN)
2929 err = -EACCES;
2930 goto out;
2931 }
2932 }
2933 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002935out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 return err;
2937}
2938
Peng Tao500d7012015-09-22 11:35:22 +08002939static bool
2940nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2941{
2942 if (inode == NULL || !nfs_have_layout(inode))
2943 return false;
2944
2945 return pnfs_wait_on_layoutreturn(inode, task);
2946}
2947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948struct nfs4_closedata {
2949 struct inode *inode;
2950 struct nfs4_state *state;
2951 struct nfs_closeargs arg;
2952 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05002953 struct {
2954 struct nfs4_layoutreturn_args arg;
2955 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04002956 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05002957 u32 roc_barrier;
2958 bool roc;
2959 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002960 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002961 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962};
2963
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002964static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002965{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002966 struct nfs4_closedata *calldata = data;
2967 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002968 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002969
Trond Myklebustcf805162016-11-15 14:56:07 -05002970 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05002971 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
2972 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07002973 nfs4_put_open_state(calldata->state);
2974 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002975 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002976 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002977 kfree(calldata);
2978}
2979
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002980static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002982 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002985 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Chuck Levera3ca5652012-03-01 17:00:40 -05002987 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002988 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2989 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002990 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05002991
2992 /* Handle Layoutreturn errors */
2993 if (calldata->arg.lr_args && task->tk_status != 0) {
2994 switch (calldata->res.lr_ret) {
2995 default:
2996 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
2997 break;
2998 case 0:
2999 calldata->arg.lr_args = NULL;
3000 calldata->res.lr_res = NULL;
3001 break;
3002 case -NFS4ERR_ADMIN_REVOKED:
3003 case -NFS4ERR_DELEG_REVOKED:
3004 case -NFS4ERR_EXPIRED:
3005 case -NFS4ERR_BAD_STATEID:
3006 case -NFS4ERR_OLD_STATEID:
3007 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3008 case -NFS4ERR_WRONG_CRED:
3009 calldata->arg.lr_args = NULL;
3010 calldata->res.lr_res = NULL;
3011 calldata->res.lr_ret = 0;
3012 rpc_restart_call_prepare(task);
3013 return;
3014 }
3015 }
3016
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003017 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 * the state_owner. we keep this around to process errors
3019 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 switch (task->tk_status) {
3021 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003022 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003023 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003024 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003025 case -NFS4ERR_ACCESS:
3026 if (calldata->arg.bitmask != NULL) {
3027 calldata->arg.bitmask = NULL;
3028 calldata->res.fattr = NULL;
3029 task->tk_status = 0;
3030 rpc_restart_call_prepare(task);
3031 goto out_release;
3032
3033 }
3034 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003035 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003037 case -NFS4ERR_EXPIRED:
3038 nfs4_free_revoked_stateid(server,
3039 &calldata->arg.stateid,
3040 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003041 case -NFS4ERR_OLD_STATEID:
3042 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003043 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003044 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003045 rpc_restart_call_prepare(task);
3046 goto out_release;
3047 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003048 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003049 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003051 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003052 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003053 goto out_release;
3054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003056 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3057 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003058out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003059 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003060 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003061 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062}
3063
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003064static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003066 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003067 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003068 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003069 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003070 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003071
Chuck Levera3ca5652012-03-01 17:00:40 -05003072 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003073 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003074 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003075
Trond Myklebust88069f72009-12-08 08:33:16 -05003076 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003077 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003078 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3079 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3080 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003081 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003082 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003083 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003084 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003085 if (state->n_rdonly == 0)
3086 call_close |= is_rdonly;
3087 else if (is_rdonly)
3088 calldata->arg.fmode |= FMODE_READ;
3089 if (state->n_wronly == 0)
3090 call_close |= is_wronly;
3091 else if (is_wronly)
3092 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003093 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3094 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003095 } else if (is_rdwr)
3096 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3097
Trond Myklebust5cc78612016-11-14 11:19:56 -05003098 if (!nfs4_valid_open_stateid(state) ||
3099 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003100 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003101 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003102
3103 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003104 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003105 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003106 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003107
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003108 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003109 nfs_release_seqid(calldata->arg.seqid);
3110 goto out_wait;
3111 }
3112
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003113 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003114 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003115
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003116 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003117 /* Close-to-open cache consistency revalidation */
3118 if (!nfs4_have_delegation(inode, FMODE_READ))
3119 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3120 else
3121 calldata->arg.bitmask = NULL;
3122 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003123
Trond Myklebust6ae37332015-01-30 14:21:14 -05003124 calldata->arg.share_access =
3125 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3126 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003127
Trond Myklebustd8d84982016-12-19 12:14:44 -05003128 if (calldata->res.fattr == NULL)
3129 calldata->arg.bitmask = NULL;
3130 else if (calldata->arg.bitmask == NULL)
3131 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003132 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003133 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003134 &calldata->arg.seq_args,
3135 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003136 task) != 0)
3137 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003138 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003139 return;
3140out_no_action:
3141 task->tk_action = NULL;
3142out_wait:
3143 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144}
3145
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003146static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003147 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003148 .rpc_call_done = nfs4_close_done,
3149 .rpc_release = nfs4_free_closedata,
3150};
3151
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152/*
3153 * It is possible for data to be read/written from a mem-mapped file
3154 * after the sys_close call (which hits the vfs layer as a flush).
3155 * This means that we can't safely call nfsv4 close on a file until
3156 * the inode is cleared. This in turn means that we are not good
3157 * NFSv4 citizens - we do not indicate to the server to update the file's
3158 * share state even when we are done with one of the three share
3159 * stateid's in the inode.
3160 *
3161 * NOTE: Caller must be holding the sp->so_owner semaphore!
3162 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003163int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003165 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003166 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003168 struct nfs4_state_owner *sp = state->owner;
3169 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003170 struct rpc_message msg = {
3171 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3172 .rpc_cred = state->owner->so_cred,
3173 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003174 struct rpc_task_setup task_setup_data = {
3175 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003176 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003177 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003178 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003179 .flags = RPC_TASK_ASYNC,
3180 };
Trond Myklebust95121352005-10-18 14:20:12 -07003181 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003183 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3184 &task_setup_data.rpc_client, &msg);
3185
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003186 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003188 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003189 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003190 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003192 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003194 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3195 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003196 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003197 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003198 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003199 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003200 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003201 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003202 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003203 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003204 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003205 calldata->lr.roc = pnfs_roc(state->inode,
3206 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3207 if (calldata->lr.roc) {
3208 calldata->arg.lr_args = &calldata->lr.arg;
3209 calldata->res.lr_res = &calldata->lr.res;
3210 }
Al Viro643168c2011-06-22 18:20:23 -04003211 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003212
Trond Myklebust1174dd12010-12-21 10:52:24 -05003213 msg.rpc_argp = &calldata->arg;
3214 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003215 task_setup_data.callback_data = calldata;
3216 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003217 if (IS_ERR(task))
3218 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003219 status = 0;
3220 if (wait)
3221 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003222 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003223 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003224out_free_calldata:
3225 kfree(calldata);
3226out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003227 nfs4_put_open_state(state);
3228 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003229 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230}
3231
Trond Myklebust2b484292010-09-17 10:56:51 -04003232static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003233nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3234 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003237 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3238
3239 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
Trond Myklebust565277f2007-10-15 18:17:53 -04003241 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003242 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003243
3244 nfs4_label_release_security(label);
3245
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003246 if (IS_ERR(state))
3247 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003248 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249}
3250
Trond Myklebust1185a552009-12-03 15:54:02 -05003251static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003252{
3253 if (ctx->state == NULL)
3254 return;
3255 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003256 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003257 else
Al Viro643168c2011-06-22 18:20:23 -04003258 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003259}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
Trond Myklebustb944dba2013-11-04 15:20:20 -05003261#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3262#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003263#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3266{
Kinglong Mee8c612822015-08-26 21:12:58 +08003267 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003268 struct nfs4_server_caps_arg args = {
3269 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003270 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003271 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 struct nfs4_server_caps_res res = {};
3273 struct rpc_message msg = {
3274 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003275 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 .rpc_resp = &res,
3277 };
3278 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003279 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
Kinglong Mee8c612822015-08-26 21:12:58 +08003281 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3282 FATTR4_WORD0_FH_EXPIRE_TYPE |
3283 FATTR4_WORD0_LINK_SUPPORT |
3284 FATTR4_WORD0_SYMLINK_SUPPORT |
3285 FATTR4_WORD0_ACLSUPPORT;
3286 if (minorversion)
3287 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3288
Bryan Schumaker7c513052011-03-24 17:12:24 +00003289 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003291 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003292 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003293 case 0:
3294 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3295 res.attr_bitmask[2] = 0;
3296 break;
3297 case 1:
3298 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3299 break;
3300 case 2:
3301 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003304 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3305 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3306 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3307 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003308 NFS_CAP_CTIME|NFS_CAP_MTIME|
3309 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003310 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3311 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 server->caps |= NFS_CAP_ACLS;
3313 if (res.has_links != 0)
3314 server->caps |= NFS_CAP_HARDLINKS;
3315 if (res.has_symlinks != 0)
3316 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003317 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3318 server->caps |= NFS_CAP_FILEID;
3319 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3320 server->caps |= NFS_CAP_MODE;
3321 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3322 server->caps |= NFS_CAP_NLINK;
3323 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3324 server->caps |= NFS_CAP_OWNER;
3325 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3326 server->caps |= NFS_CAP_OWNER_GROUP;
3327 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3328 server->caps |= NFS_CAP_ATIME;
3329 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3330 server->caps |= NFS_CAP_CTIME;
3331 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3332 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003333#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3334 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3335 server->caps |= NFS_CAP_SECURITY_LABEL;
3336#endif
3337 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3338 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003339 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003340
Trond Myklebusta65318b2009-03-11 14:10:28 -04003341 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3342 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3343 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003344 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003345
3346 /* Avoid a regression due to buggy server */
3347 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3348 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003349 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3350 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003351
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003353 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003355
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 return status;
3357}
3358
Trond Myklebust55a97592006-06-09 09:34:19 -04003359int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360{
3361 struct nfs4_exception exception = { };
3362 int err;
3363 do {
3364 err = nfs4_handle_exception(server,
3365 _nfs4_server_capabilities(server, fhandle),
3366 &exception);
3367 } while (exception.retry);
3368 return err;
3369}
3370
3371static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3372 struct nfs_fsinfo *info)
3373{
David Quigleyaa9c2662013-05-22 12:50:44 -04003374 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003376 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 };
3378 struct nfs4_lookup_res res = {
3379 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003380 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 .fh = fhandle,
3382 };
3383 struct rpc_message msg = {
3384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3385 .rpc_argp = &args,
3386 .rpc_resp = &res,
3387 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003388
David Quigleyaa9c2662013-05-22 12:50:44 -04003389 bitmask[0] = nfs4_fattr_bitmap[0];
3390 bitmask[1] = nfs4_fattr_bitmap[1];
3391 /*
3392 * Process the label in the upcoming getfattr
3393 */
3394 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3395
Trond Myklebust0e574af2005-10-27 22:12:38 -04003396 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003397 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398}
3399
3400static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3401 struct nfs_fsinfo *info)
3402{
3403 struct nfs4_exception exception = { };
3404 int err;
3405 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003406 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003407 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003408 switch (err) {
3409 case 0:
3410 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003411 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003412 default:
3413 err = nfs4_handle_exception(server, err, &exception);
3414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003416out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 return err;
3418}
3419
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003420static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3421 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3422{
Trond Myklebustc2190662013-08-26 19:23:04 -04003423 struct rpc_auth_create_args auth_args = {
3424 .pseudoflavor = flavor,
3425 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003426 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003427
Trond Myklebustc2190662013-08-26 19:23:04 -04003428 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003429 if (IS_ERR(auth))
3430 return -EACCES;
3431 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003432}
3433
Chuck Lever9a744ba2013-03-16 15:56:02 -04003434/*
3435 * Retry pseudoroot lookup with various security flavors. We do this when:
3436 *
3437 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3438 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3439 *
3440 * Returns zero on success, or a negative NFS4ERR value, or a
3441 * negative errno value.
3442 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003443static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003444 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003446 /* Per 3530bis 15.33.5 */
3447 static const rpc_authflavor_t flav_array[] = {
3448 RPC_AUTH_GSS_KRB5P,
3449 RPC_AUTH_GSS_KRB5I,
3450 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003451 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003452 RPC_AUTH_NULL,
3453 };
3454 int status = -EPERM;
3455 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003457 if (server->auth_info.flavor_len > 0) {
3458 /* try each flavor specified by user */
3459 for (i = 0; i < server->auth_info.flavor_len; i++) {
3460 status = nfs4_lookup_root_sec(server, fhandle, info,
3461 server->auth_info.flavors[i]);
3462 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3463 continue;
3464 break;
3465 }
3466 } else {
3467 /* no flavors specified by user, try default list */
3468 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3469 status = nfs4_lookup_root_sec(server, fhandle, info,
3470 flav_array[i]);
3471 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3472 continue;
3473 break;
3474 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003475 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003476
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003477 /*
3478 * -EACCESS could mean that the user doesn't have correct permissions
3479 * to access the mount. It could also mean that we tried to mount
3480 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3481 * existing mount programs don't handle -EACCES very well so it should
3482 * be mapped to -EPERM instead.
3483 */
3484 if (status == -EACCES)
3485 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003486 return status;
3487}
3488
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003489/**
3490 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3491 * @server: initialized nfs_server handle
3492 * @fhandle: we fill in the pseudo-fs root file handle
3493 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003494 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003495 *
3496 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003497 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003498int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003499 struct nfs_fsinfo *info,
3500 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003501{
Andre Przywarac7757072015-04-23 17:17:40 +01003502 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003503
Andre Przywarac7757072015-04-23 17:17:40 +01003504 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003505 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003506
3507 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003508 status = server->nfs_client->cl_mvops->find_root_sec(server,
3509 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003510
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 if (status == 0)
3512 status = nfs4_server_capabilities(server, fhandle);
3513 if (status == 0)
3514 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003515
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003516 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517}
3518
Bryan Schumakerbae36242012-05-10 15:07:31 -04003519static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3520 struct nfs_fsinfo *info)
3521{
3522 int error;
3523 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003524 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003525
3526 error = nfs4_server_capabilities(server, mntfh);
3527 if (error < 0) {
3528 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3529 return error;
3530 }
3531
David Quigley14c43f72013-05-22 12:50:43 -04003532 label = nfs4_label_alloc(server, GFP_KERNEL);
3533 if (IS_ERR(label))
3534 return PTR_ERR(label);
3535
David Quigley1775fd32013-05-22 12:50:42 -04003536 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003537 if (error < 0) {
3538 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003539 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003540 }
3541
3542 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3543 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3544 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3545
David Quigley14c43f72013-05-22 12:50:43 -04003546err_free_label:
3547 nfs4_label_free(label);
3548
Bryan Schumakerbae36242012-05-10 15:07:31 -04003549 return error;
3550}
3551
Manoj Naik6b97fd32006-06-09 09:34:29 -04003552/*
3553 * Get locations and (maybe) other attributes of a referral.
3554 * Note that we'll actually follow the referral later when
3555 * we detect fsid mismatch in inode revalidation
3556 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003557static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3558 const struct qstr *name, struct nfs_fattr *fattr,
3559 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003560{
3561 int status = -ENOMEM;
3562 struct page *page = NULL;
3563 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003564
3565 page = alloc_page(GFP_KERNEL);
3566 if (page == NULL)
3567 goto out;
3568 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3569 if (locations == NULL)
3570 goto out;
3571
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003572 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003573 if (status != 0)
3574 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003575
3576 /*
3577 * If the fsid didn't change, this is a migration event, not a
3578 * referral. Cause us to drop into the exception handler, which
3579 * will kick off migration recovery.
3580 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003581 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003582 dprintk("%s: server did not return a different fsid for"
3583 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003584 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003585 goto out;
3586 }
Andy Adamson533eb462011-06-13 18:25:56 -04003587 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3588 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003589
Andy Adamson533eb462011-06-13 18:25:56 -04003590 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003591 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003592 memset(fhandle, 0, sizeof(struct nfs_fh));
3593out:
3594 if (page)
3595 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003596 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003597 return status;
3598}
3599
David Quigley1775fd32013-05-22 12:50:42 -04003600static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3601 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602{
3603 struct nfs4_getattr_arg args = {
3604 .fh = fhandle,
3605 .bitmask = server->attr_bitmask,
3606 };
3607 struct nfs4_getattr_res res = {
3608 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003609 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 .server = server,
3611 };
3612 struct rpc_message msg = {
3613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3614 .rpc_argp = &args,
3615 .rpc_resp = &res,
3616 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003617
3618 args.bitmask = nfs4_bitmask(server, label);
3619
Trond Myklebust0e574af2005-10-27 22:12:38 -04003620 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003621 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622}
3623
David Quigley1775fd32013-05-22 12:50:42 -04003624static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3625 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626{
3627 struct nfs4_exception exception = { };
3628 int err;
3629 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003630 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3631 trace_nfs4_getattr(server, fhandle, fattr, err);
3632 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 &exception);
3634 } while (exception.retry);
3635 return err;
3636}
3637
3638/*
3639 * The file is not closed if it is opened due to the a request to change
3640 * the size of the file. The open call will not be needed once the
3641 * VFS layer lookup-intents are implemented.
3642 *
3643 * Close is called when the inode is destroyed.
3644 * If we haven't opened the file for O_WRONLY, we
3645 * need to in the size_change case to obtain a stateid.
3646 *
3647 * Got race?
3648 * Because OPEN is always done by name in nfsv4, it is
3649 * possible that we opened a different file by the same
3650 * name. We can recognize this race condition, but we
3651 * can't do anything about it besides returning an error.
3652 *
3653 * This will be fixed with VFS changes (lookup-intent).
3654 */
3655static int
3656nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3657 struct iattr *sattr)
3658{
David Howells2b0143b2015-03-17 22:25:59 +00003659 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003660 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003661 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003662 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 int status;
3664
Peng Tao88ac8152014-09-12 11:04:10 +08003665 if (pnfs_ld_layoutret_on_setattr(inode) &&
3666 sattr->ia_valid & ATTR_SIZE &&
3667 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003668 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003669
Trond Myklebust0e574af2005-10-27 22:12:38 -04003670 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
Andy Adamson26699402012-05-30 16:12:24 -04003672 /* Deal with open(O_TRUNC) */
3673 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003674 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003675
3676 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003677 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003678 return 0;
3679
Trond Myklebustd5308382005-11-04 15:33:38 -05003680 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003681 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003682
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003683 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003684 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003685 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003686 }
3687
David Quigley14c43f72013-05-22 12:50:43 -04003688 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3689 if (IS_ERR(label))
3690 return PTR_ERR(label);
3691
NeilBrown29b59f92016-10-13 15:26:47 +11003692 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003693 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003694 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003695 nfs_setsecurity(inode, fattr, label);
3696 }
David Quigley14c43f72013-05-22 12:50:43 -04003697 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 return status;
3699}
3700
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003701static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3702 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003703 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003704{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003705 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003706 int status;
3707 struct nfs4_lookup_arg args = {
3708 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003709 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003710 .name = name,
3711 };
3712 struct nfs4_lookup_res res = {
3713 .server = server,
3714 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003715 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003716 .fh = fhandle,
3717 };
3718 struct rpc_message msg = {
3719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3720 .rpc_argp = &args,
3721 .rpc_resp = &res,
3722 };
3723
David Quigleyaa9c2662013-05-22 12:50:44 -04003724 args.bitmask = nfs4_bitmask(server, label);
3725
David Howells2b3de442006-08-22 20:06:09 -04003726 nfs_fattr_init(fattr);
3727
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003729 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 dprintk("NFS reply lookup: %d\n", status);
3731 return status;
3732}
3733
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003734static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003735{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003736 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003737 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003738 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3739 fattr->nlink = 2;
3740}
3741
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003742static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003743 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003744 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003745{
3746 struct nfs4_exception exception = { };
3747 struct rpc_clnt *client = *clnt;
3748 int err;
3749 do {
David Quigley1775fd32013-05-22 12:50:42 -04003750 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003751 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003752 switch (err) {
3753 case -NFS4ERR_BADNAME:
3754 err = -ENOENT;
3755 goto out;
3756 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003757 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003758 if (err == -NFS4ERR_MOVED)
3759 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003760 goto out;
3761 case -NFS4ERR_WRONGSEC:
3762 err = -EPERM;
3763 if (client != *clnt)
3764 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003765 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003766 if (IS_ERR(client))
3767 return PTR_ERR(client);
3768
3769 exception.retry = 1;
3770 break;
3771 default:
3772 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3773 }
3774 } while (exception.retry);
3775
3776out:
3777 if (err == 0)
3778 *clnt = client;
3779 else if (client != *clnt)
3780 rpc_shutdown_client(client);
3781
3782 return err;
3783}
3784
Al Virobeffb8f2016-07-20 16:34:42 -04003785static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003786 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3787 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003789 int status;
3790 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003791
David Quigley1775fd32013-05-22 12:50:42 -04003792 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003793 if (client != NFS_CLIENT(dir)) {
3794 rpc_shutdown_client(client);
3795 nfs_fixup_secinfo_attributes(fattr);
3796 }
3797 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798}
3799
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003800struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003801nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003802 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3803{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003804 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003805 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003806
David Quigley1775fd32013-05-22 12:50:42 -04003807 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003808 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003809 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003810 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003811}
3812
Jeff Layton5b5faaf2017-06-29 06:34:52 -07003813static int _nfs4_proc_lookupp(struct inode *inode,
3814 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3815 struct nfs4_label *label)
3816{
3817 struct rpc_clnt *clnt = NFS_CLIENT(inode);
3818 struct nfs_server *server = NFS_SERVER(inode);
3819 int status;
3820 struct nfs4_lookupp_arg args = {
3821 .bitmask = server->attr_bitmask,
3822 .fh = NFS_FH(inode),
3823 };
3824 struct nfs4_lookupp_res res = {
3825 .server = server,
3826 .fattr = fattr,
3827 .label = label,
3828 .fh = fhandle,
3829 };
3830 struct rpc_message msg = {
3831 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
3832 .rpc_argp = &args,
3833 .rpc_resp = &res,
3834 };
3835
3836 args.bitmask = nfs4_bitmask(server, label);
3837
3838 nfs_fattr_init(fattr);
3839
3840 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
3841 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
3842 &res.seq_res, 0);
3843 dprintk("NFS reply lookupp: %d\n", status);
3844 return status;
3845}
3846
3847static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
3848 struct nfs_fattr *fattr, struct nfs4_label *label)
3849{
3850 struct nfs4_exception exception = { };
3851 int err;
3852 do {
3853 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
3854 trace_nfs4_lookupp(inode, err);
3855 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3856 &exception);
3857 } while (exception.retry);
3858 return err;
3859}
3860
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3862{
Trond Myklebust76b32992007-08-10 17:45:11 -04003863 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 struct nfs4_accessargs args = {
3865 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003866 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003868 struct nfs4_accessres res = {
3869 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003870 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 struct rpc_message msg = {
3872 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3873 .rpc_argp = &args,
3874 .rpc_resp = &res,
3875 .rpc_cred = entry->cred,
3876 };
3877 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003878 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
3880 /*
3881 * Determine which access bits we want to ask for...
3882 */
3883 if (mode & MAY_READ)
3884 args.access |= NFS4_ACCESS_READ;
3885 if (S_ISDIR(inode->i_mode)) {
3886 if (mode & MAY_WRITE)
3887 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3888 if (mode & MAY_EXEC)
3889 args.access |= NFS4_ACCESS_LOOKUP;
3890 } else {
3891 if (mode & MAY_WRITE)
3892 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3893 if (mode & MAY_EXEC)
3894 args.access |= NFS4_ACCESS_EXECUTE;
3895 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003896
3897 res.fattr = nfs_alloc_fattr();
3898 if (res.fattr == NULL)
3899 return -ENOMEM;
3900
Bryan Schumaker7c513052011-03-24 17:12:24 +00003901 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003903 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003904 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003906 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 return status;
3908}
3909
3910static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3911{
3912 struct nfs4_exception exception = { };
3913 int err;
3914 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003915 err = _nfs4_proc_access(inode, entry);
3916 trace_nfs4_access(inode, err);
3917 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 &exception);
3919 } while (exception.retry);
3920 return err;
3921}
3922
3923/*
3924 * TODO: For the time being, we don't try to get any attributes
3925 * along with any of the zero-copy operations READ, READDIR,
3926 * READLINK, WRITE.
3927 *
3928 * In the case of the first three, we want to put the GETATTR
3929 * after the read-type operation -- this is because it is hard
3930 * to predict the length of a GETATTR response in v4, and thus
3931 * align the READ data correctly. This means that the GETATTR
3932 * may end up partially falling into the page cache, and we should
3933 * shift it into the 'tail' of the xdr_buf before processing.
3934 * To do this efficiently, we need to know the total length
3935 * of data received, which doesn't seem to be available outside
3936 * of the RPC layer.
3937 *
3938 * In the case of WRITE, we also want to put the GETATTR after
3939 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003940 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 *
3942 * Both of these changes to the XDR layer would in fact be quite
3943 * minor, but I decided to leave them for a subsequent patch.
3944 */
3945static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3946 unsigned int pgbase, unsigned int pglen)
3947{
3948 struct nfs4_readlink args = {
3949 .fh = NFS_FH(inode),
3950 .pgbase = pgbase,
3951 .pglen = pglen,
3952 .pages = &page,
3953 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003954 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 struct rpc_message msg = {
3956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3957 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003958 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 };
3960
Bryan Schumaker7c513052011-03-24 17:12:24 +00003961 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 -07003962}
3963
3964static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3965 unsigned int pgbase, unsigned int pglen)
3966{
3967 struct nfs4_exception exception = { };
3968 int err;
3969 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003970 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3971 trace_nfs4_readlink(inode, err);
3972 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 &exception);
3974 } while (exception.retry);
3975 return err;
3976}
3977
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003979 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981static int
3982nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003983 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003985 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04003986 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003987 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 int status = 0;
3990
NeilBrown532d4de2016-10-13 15:26:47 +11003991 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003992 if (IS_ERR(ctx))
3993 return PTR_ERR(ctx);
3994
David Quigleyaa9c2662013-05-22 12:50:44 -04003995 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3996
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003997 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
3998 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003999 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 if (IS_ERR(state)) {
4001 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004002 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004005 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004006 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 return status;
4008}
4009
Al Virobeffb8f2016-07-20 16:34:42 -04004010static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011{
Trond Myklebust16e42952005-10-27 22:12:44 -04004012 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004013 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004015 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004017 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004018 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004019 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4022 .rpc_argp = &args,
4023 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004025 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004026 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004027
Bryan Schumaker7c513052011-03-24 17:12:24 +00004028 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004029 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004030 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 return status;
4032}
4033
Al Virobeffb8f2016-07-20 16:34:42 -04004034static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035{
4036 struct nfs4_exception exception = { };
4037 int err;
4038 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004039 err = _nfs4_proc_remove(dir, name);
4040 trace_nfs4_remove(dir, name, err);
4041 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 &exception);
4043 } while (exception.retry);
4044 return err;
4045}
4046
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004047static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004049 struct nfs_server *server = NFS_SERVER(dir);
4050 struct nfs_removeargs *args = msg->rpc_argp;
4051 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004053 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004055 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004056
4057 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058}
4059
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004060static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4061{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004062 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004063 &data->args.seq_args,
4064 &data->res.seq_res,
4065 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066}
4067
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004068static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004070 struct nfs_unlinkdata *data = task->tk_calldata;
4071 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004072
Trond Myklebust14516c32010-07-31 14:29:06 -04004073 if (!nfs4_sequence_done(task, &res->seq_res))
4074 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004075 if (nfs4_async_handle_error(task, res->server, NULL,
4076 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004077 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004078 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004079 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004080 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081}
4082
Jeff Laytond3d41522010-09-17 17:31:57 -04004083static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4084{
4085 struct nfs_server *server = NFS_SERVER(dir);
4086 struct nfs_renameargs *arg = msg->rpc_argp;
4087 struct nfs_renameres *res = msg->rpc_resp;
4088
4089 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004090 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004091 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004092}
4093
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004094static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4095{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004096 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004097 &data->args.seq_args,
4098 &data->res.seq_res,
4099 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004100}
4101
4102static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4103 struct inode *new_dir)
4104{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004105 struct nfs_renamedata *data = task->tk_calldata;
4106 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004107
4108 if (!nfs4_sequence_done(task, &res->seq_res))
4109 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004110 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004111 return 0;
4112
Trond Myklebustc733c492017-01-11 12:32:26 -05004113 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004114 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004115 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004116 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004117 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004118 return 1;
4119}
4120
Al Virobeffb8f2016-07-20 16:34:42 -04004121static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004123 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 struct nfs4_link_arg arg = {
4125 .fh = NFS_FH(inode),
4126 .dir_fh = NFS_FH(dir),
4127 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004128 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004130 struct nfs4_link_res res = {
4131 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004132 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004133 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 struct rpc_message msg = {
4135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4136 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004137 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004139 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140
Trond Myklebust136f2622010-04-16 16:22:49 -04004141 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004142 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004143 goto out;
4144
David Quigley14c43f72013-05-22 12:50:43 -04004145 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4146 if (IS_ERR(res.label)) {
4147 status = PTR_ERR(res.label);
4148 goto out;
4149 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004150 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004151
Bryan Schumaker7c513052011-03-24 17:12:24 +00004152 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004153 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004154 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004155 status = nfs_post_op_update_inode(inode, res.fattr);
4156 if (!status)
4157 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004158 }
David Quigley14c43f72013-05-22 12:50:43 -04004159
4160
4161 nfs4_label_free(res.label);
4162
Trond Myklebust136f2622010-04-16 16:22:49 -04004163out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004164 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 return status;
4166}
4167
Al Virobeffb8f2016-07-20 16:34:42 -04004168static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169{
4170 struct nfs4_exception exception = { };
4171 int err;
4172 do {
4173 err = nfs4_handle_exception(NFS_SERVER(inode),
4174 _nfs4_proc_link(inode, dir, name),
4175 &exception);
4176 } while (exception.retry);
4177 return err;
4178}
4179
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004180struct nfs4_createdata {
4181 struct rpc_message msg;
4182 struct nfs4_create_arg arg;
4183 struct nfs4_create_res res;
4184 struct nfs_fh fh;
4185 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004186 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004187};
4188
4189static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004190 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004191{
4192 struct nfs4_createdata *data;
4193
4194 data = kzalloc(sizeof(*data), GFP_KERNEL);
4195 if (data != NULL) {
4196 struct nfs_server *server = NFS_SERVER(dir);
4197
David Quigley14c43f72013-05-22 12:50:43 -04004198 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4199 if (IS_ERR(data->label))
4200 goto out_free;
4201
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004202 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4203 data->msg.rpc_argp = &data->arg;
4204 data->msg.rpc_resp = &data->res;
4205 data->arg.dir_fh = NFS_FH(dir);
4206 data->arg.server = server;
4207 data->arg.name = name;
4208 data->arg.attrs = sattr;
4209 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004210 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004211 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004212 data->res.server = server;
4213 data->res.fh = &data->fh;
4214 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004215 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004216 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004217 }
4218 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004219out_free:
4220 kfree(data);
4221 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004222}
4223
4224static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4225{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004226 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004227 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004228 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004229 update_changeattr(dir, &data->res.dir_cinfo,
4230 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004231 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004232 }
4233 return status;
4234}
4235
4236static void nfs4_free_createdata(struct nfs4_createdata *data)
4237{
David Quigley14c43f72013-05-22 12:50:43 -04004238 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004239 kfree(data);
4240}
4241
Chuck Lever4f390c12006-08-22 20:06:22 -04004242static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004243 struct page *page, unsigned int len, struct iattr *sattr,
4244 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004246 struct nfs4_createdata *data;
4247 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Chuck Lever94a6d752006-08-22 20:06:23 -04004249 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004250 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004251
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004252 status = -ENOMEM;
4253 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4254 if (data == NULL)
4255 goto out;
4256
4257 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4258 data->arg.u.symlink.pages = &page;
4259 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004260 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004262 status = nfs4_do_create(dir, dentry, data);
4263
4264 nfs4_free_createdata(data);
4265out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 return status;
4267}
4268
Chuck Lever4f390c12006-08-22 20:06:22 -04004269static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004270 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271{
4272 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004273 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004275
4276 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4277
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004279 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4280 trace_nfs4_symlink(dir, &dentry->d_name, err);
4281 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 &exception);
4283 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004284
4285 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 return err;
4287}
4288
4289static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004290 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004292 struct nfs4_createdata *data;
4293 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004295 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4296 if (data == NULL)
4297 goto out;
4298
David Quigley1775fd32013-05-22 12:50:42 -04004299 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004300 status = nfs4_do_create(dir, dentry, data);
4301
4302 nfs4_free_createdata(data);
4303out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 return status;
4305}
4306
4307static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4308 struct iattr *sattr)
4309{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004310 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004312 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004314
David Quigleyaa9c2662013-05-22 12:50:44 -04004315 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4316
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004317 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4318 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004320 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4321 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4322 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 &exception);
4324 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004325 nfs4_label_release_security(label);
4326
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 return err;
4328}
4329
4330static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004331 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332{
David Howells2b0143b2015-03-17 22:25:59 +00004333 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 struct nfs4_readdir_arg args = {
4335 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004336 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 .pgbase = 0,
4338 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004339 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004340 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 };
4342 struct nfs4_readdir_res res;
4343 struct rpc_message msg = {
4344 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4345 .rpc_argp = &args,
4346 .rpc_resp = &res,
4347 .rpc_cred = cred,
4348 };
4349 int status;
4350
Al Viro6de14722013-09-16 10:53:17 -04004351 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4352 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004353 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004354 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004356 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 -05004357 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004358 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004359 status += args.pgbase;
4360 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004361
4362 nfs_invalidate_atime(dir);
4363
Harvey Harrison3110ff82008-05-02 13:42:44 -07004364 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 return status;
4366}
4367
4368static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004369 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370{
4371 struct nfs4_exception exception = { };
4372 int err;
4373 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004374 err = _nfs4_proc_readdir(dentry, cred, cookie,
4375 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004376 trace_nfs4_readdir(d_inode(dentry), err);
4377 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 &exception);
4379 } while (exception.retry);
4380 return err;
4381}
4382
4383static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004384 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004386 struct nfs4_createdata *data;
4387 int mode = sattr->ia_mode;
4388 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004390 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4391 if (data == NULL)
4392 goto out;
4393
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004395 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004397 data->arg.ftype = NF4BLK;
4398 data->arg.u.device.specdata1 = MAJOR(rdev);
4399 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 }
4401 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004402 data->arg.ftype = NF4CHR;
4403 data->arg.u.device.specdata1 = MAJOR(rdev);
4404 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004405 } else if (!S_ISSOCK(mode)) {
4406 status = -EINVAL;
4407 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 }
David Quigley1775fd32013-05-22 12:50:42 -04004409
David Quigleyaa9c2662013-05-22 12:50:44 -04004410 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004411 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004412out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004413 nfs4_free_createdata(data);
4414out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 return status;
4416}
4417
4418static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4419 struct iattr *sattr, dev_t rdev)
4420{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004421 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004423 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004425
David Quigleyaa9c2662013-05-22 12:50:44 -04004426 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4427
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004428 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4429 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004431 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4432 trace_nfs4_mknod(dir, &dentry->d_name, err);
4433 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 &exception);
4435 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004436
4437 nfs4_label_release_security(label);
4438
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 return err;
4440}
4441
4442static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4443 struct nfs_fsstat *fsstat)
4444{
4445 struct nfs4_statfs_arg args = {
4446 .fh = fhandle,
4447 .bitmask = server->attr_bitmask,
4448 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004449 struct nfs4_statfs_res res = {
4450 .fsstat = fsstat,
4451 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 struct rpc_message msg = {
4453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4454 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004455 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 };
4457
Trond Myklebust0e574af2005-10-27 22:12:38 -04004458 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004459 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460}
4461
4462static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4463{
4464 struct nfs4_exception exception = { };
4465 int err;
4466 do {
4467 err = nfs4_handle_exception(server,
4468 _nfs4_proc_statfs(server, fhandle, fsstat),
4469 &exception);
4470 } while (exception.retry);
4471 return err;
4472}
4473
4474static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4475 struct nfs_fsinfo *fsinfo)
4476{
4477 struct nfs4_fsinfo_arg args = {
4478 .fh = fhandle,
4479 .bitmask = server->attr_bitmask,
4480 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004481 struct nfs4_fsinfo_res res = {
4482 .fsinfo = fsinfo,
4483 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 struct rpc_message msg = {
4485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4486 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004487 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 };
4489
Bryan Schumaker7c513052011-03-24 17:12:24 +00004490 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491}
4492
4493static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4494{
4495 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004496 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 int err;
4498
4499 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004500 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004501 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004502 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004503 nfs4_set_lease_period(server->nfs_client,
4504 fsinfo->lease_time * HZ,
4505 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004506 break;
4507 }
4508 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 } while (exception.retry);
4510 return err;
4511}
4512
4513static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4514{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004515 int error;
4516
Trond Myklebust0e574af2005-10-27 22:12:38 -04004517 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004518 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004519 if (error == 0) {
4520 /* block layout checks this! */
4521 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004522 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004523 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004524
4525 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526}
4527
4528static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4529 struct nfs_pathconf *pathconf)
4530{
4531 struct nfs4_pathconf_arg args = {
4532 .fh = fhandle,
4533 .bitmask = server->attr_bitmask,
4534 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004535 struct nfs4_pathconf_res res = {
4536 .pathconf = pathconf,
4537 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 struct rpc_message msg = {
4539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4540 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004541 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 };
4543
4544 /* None of the pathconf attributes are mandatory to implement */
4545 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4546 memset(pathconf, 0, sizeof(*pathconf));
4547 return 0;
4548 }
4549
Trond Myklebust0e574af2005-10-27 22:12:38 -04004550 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004551 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552}
4553
4554static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4555 struct nfs_pathconf *pathconf)
4556{
4557 struct nfs4_exception exception = { };
4558 int err;
4559
4560 do {
4561 err = nfs4_handle_exception(server,
4562 _nfs4_proc_pathconf(server, fhandle, pathconf),
4563 &exception);
4564 } while (exception.retry);
4565 return err;
4566}
4567
Trond Myklebust5521abf2013-03-16 20:54:34 -04004568int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004569 const struct nfs_open_context *ctx,
4570 const struct nfs_lock_context *l_ctx,
4571 fmode_t fmode)
4572{
NeilBrown17393472016-10-13 15:26:47 +11004573 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004574}
4575EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4576
Trond Myklebust5521abf2013-03-16 20:54:34 -04004577static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4578 const struct nfs_open_context *ctx,
4579 const struct nfs_lock_context *l_ctx,
4580 fmode_t fmode)
4581{
4582 nfs4_stateid current_stateid;
4583
Trond Myklebuste1253be2014-03-05 08:44:23 -05004584 /* If the current stateid represents a lost lock, then exit */
4585 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4586 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004587 return nfs4_stateid_match(stateid, &current_stateid);
4588}
4589
4590static bool nfs4_error_stateid_expired(int err)
4591{
4592 switch (err) {
4593 case -NFS4ERR_DELEG_REVOKED:
4594 case -NFS4ERR_ADMIN_REVOKED:
4595 case -NFS4ERR_BAD_STATEID:
4596 case -NFS4ERR_STALE_STATEID:
4597 case -NFS4ERR_OLD_STATEID:
4598 case -NFS4ERR_OPENMODE:
4599 case -NFS4ERR_EXPIRED:
4600 return true;
4601 }
4602 return false;
4603}
4604
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004605static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004607 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004609 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004610 if (task->tk_status < 0) {
4611 struct nfs4_exception exception = {
4612 .inode = hdr->inode,
4613 .state = hdr->args.context->state,
4614 .stateid = &hdr->args.stateid,
4615 };
4616 task->tk_status = nfs4_async_handle_exception(task,
4617 server, task->tk_status, &exception);
4618 if (exception.retry) {
4619 rpc_restart_call_prepare(task);
4620 return -EAGAIN;
4621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004623
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004625 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004626 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627}
4628
Trond Myklebust5521abf2013-03-16 20:54:34 -04004629static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004630 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004631{
4632
4633 if (!nfs4_error_stateid_expired(task->tk_status) ||
4634 nfs4_stateid_is_current(&args->stateid,
4635 args->context,
4636 args->lock_context,
4637 FMODE_READ))
4638 return false;
4639 rpc_restart_call_prepare(task);
4640 return true;
4641}
4642
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004643static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004644{
4645
4646 dprintk("--> %s\n", __func__);
4647
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004648 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004649 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004650 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004651 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004652 if (task->tk_status > 0)
4653 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004654 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4655 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004656}
4657
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004658static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4659 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004661 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004662 if (!hdr->pgio_done_cb)
4663 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004664 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004665 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666}
4667
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004668static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4669 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004670{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004671 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004672 &hdr->args.seq_args,
4673 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004674 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004675 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004676 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4677 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004678 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004679 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004680 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004681 return -EIO;
4682 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683}
4684
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004685static int nfs4_write_done_cb(struct rpc_task *task,
4686 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004688 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004689
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004690 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004691 if (task->tk_status < 0) {
4692 struct nfs4_exception exception = {
4693 .inode = hdr->inode,
4694 .state = hdr->args.context->state,
4695 .stateid = &hdr->args.stateid,
4696 };
4697 task->tk_status = nfs4_async_handle_exception(task,
4698 NFS_SERVER(inode), task->tk_status,
4699 &exception);
4700 if (exception.retry) {
4701 rpc_restart_call_prepare(task);
4702 return -EAGAIN;
4703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004705 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004706 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004707 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004708 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004709 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710}
4711
Trond Myklebust5521abf2013-03-16 20:54:34 -04004712static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004713 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004714{
4715
4716 if (!nfs4_error_stateid_expired(task->tk_status) ||
4717 nfs4_stateid_is_current(&args->stateid,
4718 args->context,
4719 args->lock_context,
4720 FMODE_WRITE))
4721 return false;
4722 rpc_restart_call_prepare(task);
4723 return true;
4724}
4725
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004726static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004727{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004728 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004729 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004730 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004731 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004732 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4733 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004734}
4735
Trond Myklebust5a37f852012-04-28 14:55:16 -04004736static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004737bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004738{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004739 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004740 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004741 return false;
4742 /* Otherwise, request attributes if and only if we don't hold
4743 * a delegation
4744 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004745 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004746}
Fred Isamana69aef12011-03-03 15:13:47 +00004747
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004748static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4749 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004751 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004752
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004753 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4754 hdr->args.bitmask = NULL;
4755 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004756 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004757 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004758
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004759 if (!hdr->pgio_done_cb)
4760 hdr->pgio_done_cb = nfs4_write_done_cb;
4761 hdr->res.server = server;
4762 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004764 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004765 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766}
4767
Fred Isaman0b7c0152012-04-20 14:47:39 -04004768static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4769{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004770 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004771 &data->args.seq_args,
4772 &data->res.seq_res,
4773 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774}
4775
Fred Isaman0b7c0152012-04-20 14:47:39 -04004776static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004779
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004780 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004781 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4782 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004783 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004784 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004786 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787}
4788
Fred Isaman0b7c0152012-04-20 14:47:39 -04004789static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004790{
4791 if (!nfs4_sequence_done(task, &data->res.seq_res))
4792 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004793 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004794}
4795
Fred Isaman0b7c0152012-04-20 14:47:39 -04004796static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004798 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004799
Fred Isaman0b7c0152012-04-20 14:47:39 -04004800 if (data->commit_done_cb == NULL)
4801 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004802 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004803 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004804 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805}
4806
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004807struct nfs4_renewdata {
4808 struct nfs_client *client;
4809 unsigned long timestamp;
4810};
4811
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812/*
4813 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4814 * standalone procedure for queueing an asynchronous RENEW.
4815 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004816static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004817{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004818 struct nfs4_renewdata *data = calldata;
4819 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004820
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004821 if (atomic_read(&clp->cl_count) > 1)
4822 nfs4_schedule_state_renewal(clp);
4823 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004824 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004825}
4826
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004827static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004829 struct nfs4_renewdata *data = calldata;
4830 struct nfs_client *clp = data->client;
4831 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004833 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004834 switch (task->tk_status) {
4835 case 0:
4836 break;
4837 case -NFS4ERR_LEASE_MOVED:
4838 nfs4_schedule_lease_moved_recovery(clp);
4839 break;
4840 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004841 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004842 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4843 return;
4844 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004845 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004846 return;
4847 }
4848 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004850 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851}
4852
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004853static const struct rpc_call_ops nfs4_renew_ops = {
4854 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004855 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004856};
4857
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004858static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859{
4860 struct rpc_message msg = {
4861 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4862 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004863 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004865 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004867 if (renew_flags == 0)
4868 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004869 if (!atomic_inc_not_zero(&clp->cl_count))
4870 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004871 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04004872 if (data == NULL) {
4873 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004874 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04004875 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004876 data->client = clp;
4877 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004878 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004879 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880}
4881
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004882static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883{
4884 struct rpc_message msg = {
4885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4886 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004887 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 };
4889 unsigned long now = jiffies;
4890 int status;
4891
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004892 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 if (status < 0)
4894 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004895 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 return 0;
4897}
4898
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004899static inline int nfs4_server_supports_acls(struct nfs_server *server)
4900{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004901 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004902}
4903
Trond Myklebust21f498c2012-08-24 10:59:25 -04004904/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4905 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004906 * the stack.
4907 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004908#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004909
Neil Hormane9e3d722011-03-04 19:26:03 -05004910static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004911 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004912{
4913 struct page *newpage, **spages;
4914 int rc = 0;
4915 size_t len;
4916 spages = pages;
4917
4918 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004919 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004920 newpage = alloc_page(GFP_KERNEL);
4921
4922 if (newpage == NULL)
4923 goto unwind;
4924 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05004925 buf += len;
4926 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05004927 *pages++ = newpage;
4928 rc++;
4929 } while (buflen != 0);
4930
4931 return rc;
4932
4933unwind:
4934 for(; rc > 0; rc--)
4935 __free_page(spages[rc-1]);
4936 return -ENOMEM;
4937}
4938
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004939struct nfs4_cached_acl {
4940 int cached;
4941 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004942 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004943};
4944
4945static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004946{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004947 struct nfs_inode *nfsi = NFS_I(inode);
4948
4949 spin_lock(&inode->i_lock);
4950 kfree(nfsi->nfs4_acl);
4951 nfsi->nfs4_acl = acl;
4952 spin_unlock(&inode->i_lock);
4953}
4954
4955static void nfs4_zap_acl_attr(struct inode *inode)
4956{
4957 nfs4_set_cached_acl(inode, NULL);
4958}
4959
4960static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4961{
4962 struct nfs_inode *nfsi = NFS_I(inode);
4963 struct nfs4_cached_acl *acl;
4964 int ret = -ENOENT;
4965
4966 spin_lock(&inode->i_lock);
4967 acl = nfsi->nfs4_acl;
4968 if (acl == NULL)
4969 goto out;
4970 if (buf == NULL) /* user is just asking for length */
4971 goto out_len;
4972 if (acl->cached == 0)
4973 goto out;
4974 ret = -ERANGE; /* see getxattr(2) man page */
4975 if (acl->len > buflen)
4976 goto out;
4977 memcpy(buf, acl->data, acl->len);
4978out_len:
4979 ret = acl->len;
4980out:
4981 spin_unlock(&inode->i_lock);
4982 return ret;
4983}
4984
Sachin Prabhu5794d212012-04-17 14:36:40 +01004985static 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 +00004986{
4987 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004988 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004989
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004990 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004991 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004992 if (acl == NULL)
4993 goto out;
4994 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004995 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004996 } else {
4997 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4998 if (acl == NULL)
4999 goto out;
5000 acl->cached = 0;
5001 }
5002 acl->len = acl_len;
5003out:
5004 nfs4_set_cached_acl(inode, acl);
5005}
5006
Andy Adamsonbf118a32011-12-07 11:55:27 -05005007/*
5008 * The getxattr API returns the required buffer length when called with a
5009 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5010 * the required buf. On a NULL buf, we send a page of data to the server
5011 * guessing that the ACL request can be serviced by a page. If so, we cache
5012 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5013 * the cache. If not so, we throw away the page, and cache the required
5014 * length. The next getxattr call will then produce another round trip to
5015 * the server, this time with the input buf of the required size.
5016 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005017static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005018{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005019 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005020 struct nfs_getaclargs args = {
5021 .fh = NFS_FH(inode),
5022 .acl_pages = pages,
5023 .acl_len = buflen,
5024 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005025 struct nfs_getaclres res = {
5026 .acl_len = buflen,
5027 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005028 struct rpc_message msg = {
5029 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5030 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005031 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005032 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005033 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005034 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005035
Trond Myklebust21f498c2012-08-24 10:59:25 -04005036 if (npages > ARRAY_SIZE(pages))
5037 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005038
Andy Adamsonbf118a32011-12-07 11:55:27 -05005039 for (i = 0; i < npages; i++) {
5040 pages[i] = alloc_page(GFP_KERNEL);
5041 if (!pages[i])
5042 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005043 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005044
5045 /* for decoding across pages */
5046 res.acl_scratch = alloc_page(GFP_KERNEL);
5047 if (!res.acl_scratch)
5048 goto out_free;
5049
Andy Adamsonbf118a32011-12-07 11:55:27 -05005050 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005051
Peng Taode040be2012-01-10 22:42:47 +08005052 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005053 __func__, buf, buflen, npages, args.acl_len);
5054 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5055 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005056 if (ret)
5057 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005058
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005059 /* Handle the case where the passed-in buffer is too short */
5060 if (res.acl_flags & NFS4_ACL_TRUNC) {
5061 /* Did the user only issue a request for the acl length? */
5062 if (buf == NULL)
5063 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005064 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005065 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005066 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005067 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005068 if (buf) {
5069 if (res.acl_len > buflen) {
5070 ret = -ERANGE;
5071 goto out_free;
5072 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005073 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005074 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005075out_ok:
5076 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005077out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005078 for (i = 0; i < npages; i++)
5079 if (pages[i])
5080 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005081 if (res.acl_scratch)
5082 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005083 return ret;
5084}
5085
Trond Myklebust16b42892006-08-24 12:27:15 -04005086static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5087{
5088 struct nfs4_exception exception = { };
5089 ssize_t ret;
5090 do {
5091 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005092 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005093 if (ret >= 0)
5094 break;
5095 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5096 } while (exception.retry);
5097 return ret;
5098}
5099
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005100static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5101{
5102 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005103 int ret;
5104
5105 if (!nfs4_server_supports_acls(server))
5106 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005107 ret = nfs_revalidate_inode(server, inode);
5108 if (ret < 0)
5109 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005110 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5111 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005112 ret = nfs4_read_cached_acl(inode, buf, buflen);
5113 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005114 /* -ENOENT is returned if there is no ACL or if there is an ACL
5115 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005116 return ret;
5117 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005118}
5119
Trond Myklebust16b42892006-08-24 12:27:15 -04005120static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005121{
5122 struct nfs_server *server = NFS_SERVER(inode);
5123 struct page *pages[NFS4ACL_MAXPAGES];
5124 struct nfs_setaclargs arg = {
5125 .fh = NFS_FH(inode),
5126 .acl_pages = pages,
5127 .acl_len = buflen,
5128 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005129 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005130 struct rpc_message msg = {
5131 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5132 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005133 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005134 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005135 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005136 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005137
5138 if (!nfs4_server_supports_acls(server))
5139 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005140 if (npages > ARRAY_SIZE(pages))
5141 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005142 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005143 if (i < 0)
5144 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005145 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005146 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005147
5148 /*
5149 * Free each page after tx, so the only ref left is
5150 * held by the network stack
5151 */
5152 for (; i > 0; i--)
5153 put_page(pages[i-1]);
5154
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005155 /*
5156 * Acl update can result in inode attribute update.
5157 * so mark the attribute cache invalid.
5158 */
5159 spin_lock(&inode->i_lock);
5160 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5161 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005162 nfs_access_zap_cache(inode);
5163 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005164 return ret;
5165}
5166
Trond Myklebust16b42892006-08-24 12:27:15 -04005167static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5168{
5169 struct nfs4_exception exception = { };
5170 int err;
5171 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005172 err = __nfs4_proc_set_acl(inode, buf, buflen);
5173 trace_nfs4_set_acl(inode, err);
5174 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005175 &exception);
5176 } while (exception.retry);
5177 return err;
5178}
5179
David Quigleyaa9c2662013-05-22 12:50:44 -04005180#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5181static int _nfs4_get_security_label(struct inode *inode, void *buf,
5182 size_t buflen)
5183{
5184 struct nfs_server *server = NFS_SERVER(inode);
5185 struct nfs_fattr fattr;
5186 struct nfs4_label label = {0, 0, buflen, buf};
5187
5188 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005189 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005190 .fh = NFS_FH(inode),
5191 .bitmask = bitmask,
5192 };
5193 struct nfs4_getattr_res res = {
5194 .fattr = &fattr,
5195 .label = &label,
5196 .server = server,
5197 };
5198 struct rpc_message msg = {
5199 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005200 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005201 .rpc_resp = &res,
5202 };
5203 int ret;
5204
5205 nfs_fattr_init(&fattr);
5206
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005207 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005208 if (ret)
5209 return ret;
5210 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5211 return -ENOENT;
5212 if (buflen < label.len)
5213 return -ERANGE;
5214 return 0;
5215}
5216
5217static int nfs4_get_security_label(struct inode *inode, void *buf,
5218 size_t buflen)
5219{
5220 struct nfs4_exception exception = { };
5221 int err;
5222
5223 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5224 return -EOPNOTSUPP;
5225
5226 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005227 err = _nfs4_get_security_label(inode, buf, buflen);
5228 trace_nfs4_get_security_label(inode, err);
5229 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005230 &exception);
5231 } while (exception.retry);
5232 return err;
5233}
5234
5235static int _nfs4_do_set_security_label(struct inode *inode,
5236 struct nfs4_label *ilabel,
5237 struct nfs_fattr *fattr,
5238 struct nfs4_label *olabel)
5239{
5240
5241 struct iattr sattr = {0};
5242 struct nfs_server *server = NFS_SERVER(inode);
5243 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005244 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005245 .fh = NFS_FH(inode),
5246 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005247 .server = server,
5248 .bitmask = bitmask,
5249 .label = ilabel,
5250 };
5251 struct nfs_setattrres res = {
5252 .fattr = fattr,
5253 .label = olabel,
5254 .server = server,
5255 };
5256 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005257 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5258 .rpc_argp = &arg,
5259 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005260 };
5261 int status;
5262
Jeff Layton12207f62013-11-01 10:49:32 -04005263 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005264
Jeff Layton12207f62013-11-01 10:49:32 -04005265 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005266 if (status)
5267 dprintk("%s failed: %d\n", __func__, status);
5268
5269 return status;
5270}
5271
5272static int nfs4_do_set_security_label(struct inode *inode,
5273 struct nfs4_label *ilabel,
5274 struct nfs_fattr *fattr,
5275 struct nfs4_label *olabel)
5276{
5277 struct nfs4_exception exception = { };
5278 int err;
5279
5280 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005281 err = _nfs4_do_set_security_label(inode, ilabel,
5282 fattr, olabel);
5283 trace_nfs4_set_security_label(inode, err);
5284 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005285 &exception);
5286 } while (exception.retry);
5287 return err;
5288}
5289
5290static int
Al Viro59301222016-05-27 10:19:30 -04005291nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005292{
5293 struct nfs4_label ilabel, *olabel = NULL;
5294 struct nfs_fattr fattr;
5295 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005296 int status;
5297
5298 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5299 return -EOPNOTSUPP;
5300
5301 nfs_fattr_init(&fattr);
5302
5303 ilabel.pi = 0;
5304 ilabel.lfs = 0;
5305 ilabel.label = (char *)buf;
5306 ilabel.len = buflen;
5307
5308 cred = rpc_lookup_cred();
5309 if (IS_ERR(cred))
5310 return PTR_ERR(cred);
5311
5312 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5313 if (IS_ERR(olabel)) {
5314 status = -PTR_ERR(olabel);
5315 goto out;
5316 }
5317
5318 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5319 if (status == 0)
5320 nfs_setsecurity(inode, &fattr, olabel);
5321
5322 nfs4_label_free(olabel);
5323out:
5324 put_rpccred(cred);
5325 return status;
5326}
5327#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5328
5329
Chuck Leverf0920752012-05-21 22:45:41 -04005330static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5331 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005332{
5333 __be32 verf[2];
5334
Chuck Lever2c820d92012-05-21 22:45:33 -04005335 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5336 /* An impossible timestamp guarantees this value
5337 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005338 verf[0] = cpu_to_be32(U32_MAX);
5339 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005340 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005341 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005342 u64 ns = ktime_to_ns(nn->boot_time);
5343
5344 verf[0] = cpu_to_be32(ns >> 32);
5345 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005346 }
Chuck Levercd937102012-03-02 17:14:31 -05005347 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5348}
5349
Jeff Laytona3192682015-06-09 19:43:59 -04005350static int
5351nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005352{
Jeff Laytona3192682015-06-09 19:43:59 -04005353 size_t len;
5354 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005355
Trond Myklebustceb3a162015-01-03 15:16:04 -05005356 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005357 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005358
Jeff Laytona3192682015-06-09 19:43:59 -04005359 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005360 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005361 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5362 1 +
5363 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5364 1;
5365 rcu_read_unlock();
5366
5367 if (len > NFS4_OPAQUE_LIMIT + 1)
5368 return -EINVAL;
5369
5370 /*
5371 * Since this string is allocated at mount time, and held until the
5372 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5373 * about a memory-reclaim deadlock.
5374 */
5375 str = kmalloc(len, GFP_KERNEL);
5376 if (!str)
5377 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005378
Chuck Levere984a552012-09-14 17:24:21 -04005379 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005380 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005381 clp->cl_ipaddr,
5382 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5383 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005384 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005385
Jeff Laytona3192682015-06-09 19:43:59 -04005386 clp->cl_owner_id = str;
5387 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005388}
5389
Jeff Layton873e3852015-06-09 19:44:00 -04005390static int
5391nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005392{
Jeff Layton873e3852015-06-09 19:44:00 -04005393 size_t len;
5394 char *str;
5395
5396 len = 10 + 10 + 1 + 10 + 1 +
5397 strlen(nfs4_client_id_uniquifier) + 1 +
5398 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5399
5400 if (len > NFS4_OPAQUE_LIMIT + 1)
5401 return -EINVAL;
5402
5403 /*
5404 * Since this string is allocated at mount time, and held until the
5405 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5406 * about a memory-reclaim deadlock.
5407 */
5408 str = kmalloc(len, GFP_KERNEL);
5409 if (!str)
5410 return -ENOMEM;
5411
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005412 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005413 clp->rpc_ops->version, clp->cl_minorversion,
5414 nfs4_client_id_uniquifier,
5415 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005416 clp->cl_owner_id = str;
5417 return 0;
5418}
5419
5420static int
5421nfs4_init_uniform_client_string(struct nfs_client *clp)
5422{
Jeff Layton873e3852015-06-09 19:44:00 -04005423 size_t len;
5424 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005425
5426 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005427 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005428
5429 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005430 return nfs4_init_uniquifier_client_string(clp);
5431
5432 len = 10 + 10 + 1 + 10 + 1 +
5433 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5434
5435 if (len > NFS4_OPAQUE_LIMIT + 1)
5436 return -EINVAL;
5437
5438 /*
5439 * Since this string is allocated at mount time, and held until the
5440 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5441 * about a memory-reclaim deadlock.
5442 */
5443 str = kmalloc(len, GFP_KERNEL);
5444 if (!str)
5445 return -ENOMEM;
5446
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005447 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005448 clp->rpc_ops->version, clp->cl_minorversion,
5449 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005450 clp->cl_owner_id = str;
5451 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005452}
5453
Chuck Lever706cb8d2014-03-12 12:51:47 -04005454/*
5455 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5456 * services. Advertise one based on the address family of the
5457 * clientaddr.
5458 */
5459static unsigned int
5460nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5461{
5462 if (strchr(clp->cl_ipaddr, ':') != NULL)
5463 return scnprintf(buf, len, "tcp6");
5464 else
5465 return scnprintf(buf, len, "tcp");
5466}
5467
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005468static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5469{
5470 struct nfs4_setclientid *sc = calldata;
5471
5472 if (task->tk_status == 0)
5473 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5474}
5475
5476static const struct rpc_call_ops nfs4_setclientid_ops = {
5477 .rpc_call_done = nfs4_setclientid_done,
5478};
5479
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005480/**
5481 * nfs4_proc_setclientid - Negotiate client ID
5482 * @clp: state data structure
5483 * @program: RPC program for NFSv4 callback service
5484 * @port: IP port number for NFS4 callback service
5485 * @cred: RPC credential to use for this call
5486 * @res: where to place the result
5487 *
5488 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5489 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005490int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5491 unsigned short port, struct rpc_cred *cred,
5492 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493{
5494 nfs4_verifier sc_verifier;
5495 struct nfs4_setclientid setclientid = {
5496 .sc_verifier = &sc_verifier,
5497 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005498 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 };
5500 struct rpc_message msg = {
5501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5502 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005503 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005504 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005506 struct rpc_task *task;
5507 struct rpc_task_setup task_setup_data = {
5508 .rpc_client = clp->cl_rpcclient,
5509 .rpc_message = &msg,
5510 .callback_ops = &nfs4_setclientid_ops,
5511 .callback_data = &setclientid,
5512 .flags = RPC_TASK_TIMEOUT,
5513 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005514 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515
Chuck Leverde734832012-07-11 16:30:50 -04005516 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005517 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005518
5519 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5520 status = nfs4_init_uniform_client_string(clp);
5521 else
Jeff Laytona3192682015-06-09 19:43:59 -04005522 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005523
5524 if (status)
5525 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005526
Chuck Leverde734832012-07-11 16:30:50 -04005527 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005528 setclientid.sc_netid_len =
5529 nfs4_init_callback_netid(clp,
5530 setclientid.sc_netid,
5531 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005532 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005533 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 clp->cl_ipaddr, port >> 8, port & 255);
5535
Jeff Layton3a6bb732015-06-09 19:43:57 -04005536 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005537 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005538 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005539 task = rpc_run_task(&task_setup_data);
5540 if (IS_ERR(task)) {
5541 status = PTR_ERR(task);
5542 goto out;
5543 }
5544 status = task->tk_status;
5545 if (setclientid.sc_cred) {
5546 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5547 put_rpccred(setclientid.sc_cred);
5548 }
5549 rpc_put_task(task);
5550out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005551 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005552 dprintk("NFS reply setclientid: %d\n", status);
5553 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554}
5555
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005556/**
5557 * nfs4_proc_setclientid_confirm - Confirm client ID
5558 * @clp: state data structure
5559 * @res: result of a previous SETCLIENTID
5560 * @cred: RPC credential to use for this call
5561 *
5562 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5563 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005564int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005565 struct nfs4_setclientid_res *arg,
5566 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 struct rpc_message msg = {
5569 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005570 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005571 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 int status;
5574
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005575 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5576 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5577 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005578 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005579 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005580 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 return status;
5582}
5583
Trond Myklebustfe650402006-01-03 09:55:18 +01005584struct nfs4_delegreturndata {
5585 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005586 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005587 struct nfs_fh fh;
5588 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005589 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005590 struct {
5591 struct nfs4_layoutreturn_args arg;
5592 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005593 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005594 u32 roc_barrier;
5595 bool roc;
5596 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005597 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005598 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005599 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005600};
5601
Trond Myklebustfe650402006-01-03 09:55:18 +01005602static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5603{
5604 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005605
Trond Myklebust14516c32010-07-31 14:29:06 -04005606 if (!nfs4_sequence_done(task, &data->res.seq_res))
5607 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005608
Trond Myklebustca8acf82013-08-13 10:36:56 -04005609 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005610
5611 /* Handle Layoutreturn errors */
5612 if (data->args.lr_args && task->tk_status != 0) {
5613 switch(data->res.lr_ret) {
5614 default:
5615 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5616 break;
5617 case 0:
5618 data->args.lr_args = NULL;
5619 data->res.lr_res = NULL;
5620 break;
5621 case -NFS4ERR_ADMIN_REVOKED:
5622 case -NFS4ERR_DELEG_REVOKED:
5623 case -NFS4ERR_EXPIRED:
5624 case -NFS4ERR_BAD_STATEID:
5625 case -NFS4ERR_OLD_STATEID:
5626 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5627 case -NFS4ERR_WRONG_CRED:
5628 data->args.lr_args = NULL;
5629 data->res.lr_res = NULL;
5630 data->res.lr_ret = 0;
5631 rpc_restart_call_prepare(task);
5632 return;
5633 }
5634 }
5635
Ricardo Labiaga79708862009-12-07 09:23:21 -05005636 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005637 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005638 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005639 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005640 case -NFS4ERR_ADMIN_REVOKED:
5641 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005642 case -NFS4ERR_EXPIRED:
5643 nfs4_free_revoked_stateid(data->res.server,
5644 data->args.stateid,
5645 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005646 case -NFS4ERR_BAD_STATEID:
5647 case -NFS4ERR_OLD_STATEID:
5648 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005649 task->tk_status = 0;
5650 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005651 case -NFS4ERR_ACCESS:
5652 if (data->args.bitmask) {
5653 data->args.bitmask = NULL;
5654 data->res.fattr = NULL;
5655 task->tk_status = 0;
5656 rpc_restart_call_prepare(task);
5657 return;
5658 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005659 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005660 if (nfs4_async_handle_error(task, data->res.server,
5661 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005662 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005663 return;
5664 }
5665 }
5666 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005667}
5668
5669static void nfs4_delegreturn_release(void *calldata)
5670{
Peng Tao039b7562014-07-03 13:05:02 +08005671 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005672 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005673
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005674 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005675 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005676 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5677 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005678 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005679 nfs_iput_and_deactive(inode);
5680 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005681 kfree(calldata);
5682}
5683
Andy Adamson938e1012009-04-01 09:22:28 -04005684static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5685{
5686 struct nfs4_delegreturndata *d_data;
5687
5688 d_data = (struct nfs4_delegreturndata *)data;
5689
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005690 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005691 return;
5692
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005693 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005694 &d_data->args.seq_args,
5695 &d_data->res.seq_res,
5696 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005697}
Andy Adamson938e1012009-04-01 09:22:28 -04005698
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005699static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005700 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005701 .rpc_call_done = nfs4_delegreturn_done,
5702 .rpc_release = nfs4_delegreturn_release,
5703};
5704
Trond Myklebuste6f81072008-01-24 18:14:34 -05005705static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005706{
5707 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005708 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005709 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005710 struct rpc_message msg = {
5711 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5712 .rpc_cred = cred,
5713 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005714 struct rpc_task_setup task_setup_data = {
5715 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005716 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005717 .callback_ops = &nfs4_delegreturn_ops,
5718 .flags = RPC_TASK_ASYNC,
5719 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005720 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005721
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005722 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005723 if (data == NULL)
5724 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005725 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005726
5727 nfs4_state_protect(server->nfs_client,
5728 NFS_SP4_MACH_CRED_CLEANUP,
5729 &task_setup_data.rpc_client, &msg);
5730
Trond Myklebustfe650402006-01-03 09:55:18 +01005731 data->args.fhandle = &data->fh;
5732 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005733 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005734 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005735 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005736 data->res.fattr = &data->fattr;
5737 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005738 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005739 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005740 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005741 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005742 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005743 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005744 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005745 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005746 if (data->lr.roc) {
5747 data->args.lr_args = &data->lr.arg;
5748 data->res.lr_res = &data->lr.res;
5749 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005750 } else if (data->lr.roc) {
5751 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5752 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005753 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005754
Trond Myklebustc970aa82007-07-14 15:39:59 -04005755 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005756 msg.rpc_argp = &data->args;
5757 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005758 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005759 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005760 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005761 if (!issync)
5762 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005763 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005764 if (status != 0)
5765 goto out;
5766 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005767out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005768 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005769 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770}
5771
Trond Myklebuste6f81072008-01-24 18:14:34 -05005772int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773{
5774 struct nfs_server *server = NFS_SERVER(inode);
5775 struct nfs4_exception exception = { };
5776 int err;
5777 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005778 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005779 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 switch (err) {
5781 case -NFS4ERR_STALE_STATEID:
5782 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 case 0:
5784 return 0;
5785 }
5786 err = nfs4_handle_exception(server, err, &exception);
5787 } while (exception.retry);
5788 return err;
5789}
5790
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5792{
5793 struct inode *inode = state->inode;
5794 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005795 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005796 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005798 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005800 struct nfs_lockt_res res = {
5801 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 };
5803 struct rpc_message msg = {
5804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005805 .rpc_argp = &arg,
5806 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 .rpc_cred = state->owner->so_cred,
5808 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 struct nfs4_lock_state *lsp;
5810 int status;
5811
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005812 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005813 status = nfs4_set_lock_state(state, request);
5814 if (status != 0)
5815 goto out;
5816 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005817 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005818 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005819 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005820 switch (status) {
5821 case 0:
5822 request->fl_type = F_UNLCK;
5823 break;
5824 case -NFS4ERR_DENIED:
5825 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005827 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005828 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005829out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 return status;
5831}
5832
5833static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5834{
5835 struct nfs4_exception exception = { };
5836 int err;
5837
5838 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005839 err = _nfs4_proc_getlk(state, cmd, request);
5840 trace_nfs4_get_lock(request, state, cmd, err);
5841 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 &exception);
5843 } while (exception.retry);
5844 return err;
5845}
5846
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005847struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005848 struct nfs_locku_args arg;
5849 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005850 struct nfs4_lock_state *lsp;
5851 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005852 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005853 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005854 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005855 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005856};
5857
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005858static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5859 struct nfs_open_context *ctx,
5860 struct nfs4_lock_state *lsp,
5861 struct nfs_seqid *seqid)
5862{
5863 struct nfs4_unlockdata *p;
5864 struct inode *inode = lsp->ls_state->inode;
5865
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005866 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005867 if (p == NULL)
5868 return NULL;
5869 p->arg.fh = NFS_FH(inode);
5870 p->arg.fl = &p->fl;
5871 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005872 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005873 p->lsp = lsp;
5874 atomic_inc(&lsp->ls_count);
5875 /* Ensure we don't close file until we're done freeing locks! */
5876 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005877 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005878 memcpy(&p->fl, fl, sizeof(p->fl));
5879 p->server = NFS_SERVER(inode);
5880 return p;
5881}
5882
Trond Myklebust06f814a2006-01-03 09:55:07 +01005883static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005884{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005885 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005886 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005887 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005888 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005889 put_nfs_open_context(calldata->ctx);
5890 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005891}
5892
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005893static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005894{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005895 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005896
Trond Myklebust14516c32010-07-31 14:29:06 -04005897 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5898 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005899 switch (task->tk_status) {
5900 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005901 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005902 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005903 if (nfs4_update_lock_stateid(calldata->lsp,
5904 &calldata->res.stateid))
5905 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005906 case -NFS4ERR_ADMIN_REVOKED:
5907 case -NFS4ERR_EXPIRED:
5908 nfs4_free_revoked_stateid(calldata->server,
5909 &calldata->arg.stateid,
5910 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005911 case -NFS4ERR_BAD_STATEID:
5912 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005913 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005914 if (!nfs4_stateid_match(&calldata->arg.stateid,
5915 &calldata->lsp->ls_stateid))
5916 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005917 break;
5918 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005919 if (nfs4_async_handle_error(task, calldata->server,
5920 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005921 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005922 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005923 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005924}
5925
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005926static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005927{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005928 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005930 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
5931 nfs_async_iocounter_wait(task, calldata->l_ctx))
5932 return;
5933
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005934 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005935 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005936 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005937 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005938 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005939 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005940 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005941 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005942 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04005943 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005944 &calldata->res.seq_res,
5945 task) != 0)
5946 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005947 return;
5948out_no_action:
5949 task->tk_action = NULL;
5950out_wait:
5951 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952}
5953
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005954static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005955 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005956 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005957 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005958};
5959
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005960static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5961 struct nfs_open_context *ctx,
5962 struct nfs4_lock_state *lsp,
5963 struct nfs_seqid *seqid)
5964{
5965 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005966 struct rpc_message msg = {
5967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5968 .rpc_cred = ctx->cred,
5969 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005970 struct rpc_task_setup task_setup_data = {
5971 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005972 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005973 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005974 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005975 .flags = RPC_TASK_ASYNC,
5976 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005977
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005978 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5979 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5980
Frank Filz137d6ac2007-07-09 15:32:29 -07005981 /* Ensure this is an unlock - when canceling a lock, the
5982 * canceled lock is passed in, and it won't be an unlock.
5983 */
5984 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005985 if (fl->fl_flags & FL_CLOSE)
5986 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07005987
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005988 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5989 if (data == NULL) {
5990 nfs_free_seqid(seqid);
5991 return ERR_PTR(-ENOMEM);
5992 }
5993
Chuck Levera9c92d62013-08-09 12:48:18 -04005994 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005995 msg.rpc_argp = &data->arg;
5996 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005997 task_setup_data.callback_data = data;
5998 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005999}
6000
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6002{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006003 struct inode *inode = state->inode;
6004 struct nfs4_state_owner *sp = state->owner;
6005 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006006 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006007 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006008 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006009 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006010 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006011 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012
Trond Myklebust9b073572006-06-29 16:38:34 -04006013 status = nfs4_set_lock_state(state, request);
6014 /* Unlock _before_ we do the RPC call */
6015 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006016 /* Exclude nfs_delegation_claim_locks() */
6017 mutex_lock(&sp->so_delegreturn_mutex);
6018 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006019 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006020 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006021 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 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006024 }
6025 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006026 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006027 if (status != 0)
6028 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006029 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006030 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006031 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6032 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006033 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6034 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006035 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006036 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006037 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006038 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006039 status = PTR_ERR(task);
6040 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006041 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006042 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006043 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006044out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006045 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006046 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006047 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048}
6049
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006050struct nfs4_lockdata {
6051 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006052 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006053 struct nfs4_lock_state *lsp;
6054 struct nfs_open_context *ctx;
6055 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006056 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006057 int rpc_status;
6058 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006059 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006060};
6061
6062static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006063 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6064 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006065{
6066 struct nfs4_lockdata *p;
6067 struct inode *inode = lsp->ls_state->inode;
6068 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006069 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006070
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006071 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006072 if (p == NULL)
6073 return NULL;
6074
6075 p->arg.fh = NFS_FH(inode);
6076 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006077 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006078 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006079 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006080 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6081 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006082 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006083 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006084 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006085 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006086 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006087 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006088 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006089 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006090 atomic_inc(&lsp->ls_count);
6091 p->ctx = get_nfs_open_context(ctx);
6092 memcpy(&p->fl, fl, sizeof(p->fl));
6093 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006094out_free_seqid:
6095 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006096out_free:
6097 kfree(p);
6098 return NULL;
6099}
6100
6101static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6102{
6103 struct nfs4_lockdata *data = calldata;
6104 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105
Harvey Harrison3110ff82008-05-02 13:42:44 -07006106 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006107 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006108 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006109 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006110 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006111 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006112 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006113 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006114 nfs4_stateid_copy(&data->arg.open_stateid,
6115 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006116 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006117 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006118 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006119 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006120 nfs4_stateid_copy(&data->arg.lock_stateid,
6121 &data->lsp->ls_stateid);
6122 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006123 if (!nfs4_valid_open_stateid(state)) {
6124 data->rpc_status = -EBADF;
6125 task->tk_action = NULL;
6126 goto out_release_open_seqid;
6127 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006128 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006129 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006130 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006131 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006132 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006133 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006134out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006135 nfs_release_seqid(data->arg.open_seqid);
6136out_release_lock_seqid:
6137 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006138out_wait:
6139 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006140 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006141}
6142
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006143static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6144{
6145 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006146 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006147
Harvey Harrison3110ff82008-05-02 13:42:44 -07006148 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006149
Trond Myklebust14516c32010-07-31 14:29:06 -04006150 if (!nfs4_sequence_done(task, &data->res.seq_res))
6151 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006152
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006153 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006154 switch (task->tk_status) {
6155 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006156 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006157 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006158 if (data->arg.new_lock) {
6159 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006160 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006161 rpc_restart_call_prepare(task);
6162 break;
6163 }
6164 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006165 if (data->arg.new_lock_owner != 0) {
6166 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6167 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6168 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6169 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6170 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006171 break;
6172 case -NFS4ERR_BAD_STATEID:
6173 case -NFS4ERR_OLD_STATEID:
6174 case -NFS4ERR_STALE_STATEID:
6175 case -NFS4ERR_EXPIRED:
6176 if (data->arg.new_lock_owner != 0) {
6177 if (!nfs4_stateid_match(&data->arg.open_stateid,
6178 &lsp->ls_state->open_stateid))
6179 rpc_restart_call_prepare(task);
6180 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6181 &lsp->ls_stateid))
6182 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006183 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006184 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006185}
6186
6187static void nfs4_lock_release(void *calldata)
6188{
6189 struct nfs4_lockdata *data = calldata;
6190
Harvey Harrison3110ff82008-05-02 13:42:44 -07006191 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006192 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006193 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006194 struct rpc_task *task;
6195 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6196 data->arg.lock_seqid);
6197 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006198 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006199 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006200 } else
6201 nfs_free_seqid(data->arg.lock_seqid);
6202 nfs4_put_lock_state(data->lsp);
6203 put_nfs_open_context(data->ctx);
6204 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006205 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006206}
6207
6208static const struct rpc_call_ops nfs4_lock_ops = {
6209 .rpc_call_prepare = nfs4_lock_prepare,
6210 .rpc_call_done = nfs4_lock_done,
6211 .rpc_release = nfs4_lock_release,
6212};
6213
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006214static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6215{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006216 switch (error) {
6217 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006218 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006219 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006220 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006221 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006222 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006223 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006224 break;
6225 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006226 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006227 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006228 };
6229}
6230
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006231static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006232{
6233 struct nfs4_lockdata *data;
6234 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006235 struct rpc_message msg = {
6236 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6237 .rpc_cred = state->owner->so_cred,
6238 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006239 struct rpc_task_setup task_setup_data = {
6240 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006241 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006242 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006243 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006244 .flags = RPC_TASK_ASYNC,
6245 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006246 int ret;
6247
Harvey Harrison3110ff82008-05-02 13:42:44 -07006248 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006249 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006250 fl->fl_u.nfs4_fl.owner,
6251 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006252 if (data == NULL)
6253 return -ENOMEM;
6254 if (IS_SETLKW(cmd))
6255 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006256 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006257 msg.rpc_argp = &data->arg;
6258 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006259 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006260 if (recovery_type > NFS_LOCK_NEW) {
6261 if (recovery_type == NFS_LOCK_RECLAIM)
6262 data->arg.reclaim = NFS_LOCK_RECLAIM;
6263 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006264 } else
6265 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006266 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006267 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006268 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006269 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006270 if (ret == 0) {
6271 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006272 if (ret)
6273 nfs4_handle_setlk_error(data->server, data->lsp,
6274 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006275 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006276 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006277 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006278 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006279 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006280 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281}
6282
6283static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6284{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006285 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006286 struct nfs4_exception exception = {
6287 .inode = state->inode,
6288 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006289 int err;
6290
6291 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006292 /* Cache the lock if possible... */
6293 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6294 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006295 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006296 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006297 break;
6298 nfs4_handle_exception(server, err, &exception);
6299 } while (exception.retry);
6300 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301}
6302
6303static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6304{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006305 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006306 struct nfs4_exception exception = {
6307 .inode = state->inode,
6308 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006309 int err;
6310
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006311 err = nfs4_set_lock_state(state, request);
6312 if (err != 0)
6313 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006314 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006315 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6316 return 0;
6317 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006318 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006319 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6320 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006321 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006322 switch (err) {
6323 default:
6324 goto out;
6325 case -NFS4ERR_GRACE:
6326 case -NFS4ERR_DELAY:
6327 nfs4_handle_exception(server, err, &exception);
6328 err = 0;
6329 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006330 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006331out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006332 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333}
6334
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006335#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006336static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6337{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006338 struct nfs4_lock_state *lsp;
6339 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006340
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006341 status = nfs4_set_lock_state(state, request);
6342 if (status != 0)
6343 return status;
6344 lsp = request->fl_u.nfs4_fl.owner;
6345 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6346 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6347 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006348 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006349}
6350#endif
6351
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6353{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006354 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006355 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006356 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006357 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358
Trond Myklebust01c3b862006-06-29 16:38:39 -04006359 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006360 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006361 if (status < 0)
6362 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006363 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006364 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006365 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006366 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006367 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006368 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006369 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006370 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006371 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006372 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006373 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006374 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006375 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006376 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006377out:
6378 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 return status;
6380}
6381
6382static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6383{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006384 struct nfs4_exception exception = {
6385 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006386 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006387 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 int err;
6389
6390 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006391 err = _nfs4_proc_setlk(state, cmd, request);
6392 if (err == -NFS4ERR_DENIED)
6393 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006395 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396 } while (exception.retry);
6397 return err;
6398}
6399
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006400#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6401#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6402
6403static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006404nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6405 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006406{
6407 int status = -ERESTARTSYS;
6408 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6409
6410 while(!signalled()) {
6411 status = nfs4_proc_setlk(state, cmd, request);
6412 if ((status != -EAGAIN) || IS_SETLK(cmd))
6413 break;
6414 freezable_schedule_timeout_interruptible(timeout);
6415 timeout *= 2;
6416 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6417 status = -ERESTARTSYS;
6418 }
6419 return status;
6420}
6421
Jeff Laytona1d617d82016-09-17 18:17:39 -04006422#ifdef CONFIG_NFS_V4_1
6423struct nfs4_lock_waiter {
6424 struct task_struct *task;
6425 struct inode *inode;
6426 struct nfs_lowner *owner;
6427 bool notified;
6428};
6429
6430static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006431nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006432{
6433 int ret;
6434 struct cb_notify_lock_args *cbnl = key;
6435 struct nfs4_lock_waiter *waiter = wait->private;
6436 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6437 *wowner = waiter->owner;
6438
6439 /* Only wake if the callback was for the same owner */
6440 if (lowner->clientid != wowner->clientid ||
6441 lowner->id != wowner->id ||
6442 lowner->s_dev != wowner->s_dev)
6443 return 0;
6444
6445 /* Make sure it's for the right inode */
6446 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6447 return 0;
6448
6449 waiter->notified = true;
6450
6451 /* override "private" so we can use default_wake_function */
6452 wait->private = waiter->task;
6453 ret = autoremove_wake_function(wait, mode, flags, key);
6454 wait->private = waiter;
6455 return ret;
6456}
6457
6458static int
6459nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6460{
6461 int status = -ERESTARTSYS;
6462 unsigned long flags;
6463 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6464 struct nfs_server *server = NFS_SERVER(state->inode);
6465 struct nfs_client *clp = server->nfs_client;
6466 wait_queue_head_t *q = &clp->cl_lock_waitq;
6467 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6468 .id = lsp->ls_seqid.owner_id,
6469 .s_dev = server->s_dev };
6470 struct nfs4_lock_waiter waiter = { .task = current,
6471 .inode = state->inode,
6472 .owner = &owner,
6473 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006474 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006475
6476 /* Don't bother with waitqueue if we don't expect a callback */
6477 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6478 return nfs4_retry_setlk_simple(state, cmd, request);
6479
6480 init_wait(&wait);
6481 wait.private = &waiter;
6482 wait.func = nfs4_wake_lock_waiter;
6483 add_wait_queue(q, &wait);
6484
6485 while(!signalled()) {
6486 status = nfs4_proc_setlk(state, cmd, request);
6487 if ((status != -EAGAIN) || IS_SETLK(cmd))
6488 break;
6489
6490 status = -ERESTARTSYS;
6491 spin_lock_irqsave(&q->lock, flags);
6492 if (waiter.notified) {
6493 spin_unlock_irqrestore(&q->lock, flags);
6494 continue;
6495 }
6496 set_current_state(TASK_INTERRUPTIBLE);
6497 spin_unlock_irqrestore(&q->lock, flags);
6498
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006499 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006500 }
6501
6502 finish_wait(q, &wait);
6503 return status;
6504}
6505#else /* !CONFIG_NFS_V4_1 */
6506static inline int
6507nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6508{
6509 return nfs4_retry_setlk_simple(state, cmd, request);
6510}
6511#endif
6512
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513static int
6514nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6515{
6516 struct nfs_open_context *ctx;
6517 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 int status;
6519
6520 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006521 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522 state = ctx->state;
6523
Trond Myklebustd9531262009-07-21 19:22:38 -04006524 if (IS_GETLK(cmd)) {
6525 if (state != NULL)
6526 return nfs4_proc_getlk(state, F_GETLK, request);
6527 return 0;
6528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529
6530 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6531 return -EINVAL;
6532
Trond Myklebustd9531262009-07-21 19:22:38 -04006533 if (request->fl_type == F_UNLCK) {
6534 if (state != NULL)
6535 return nfs4_proc_unlck(state, cmd, request);
6536 return 0;
6537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538
Trond Myklebustd9531262009-07-21 19:22:38 -04006539 if (state == NULL)
6540 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006541
6542 if ((request->fl_flags & FL_POSIX) &&
6543 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6544 return -ENOLCK;
6545
Jeff Layton1ea67db2016-09-17 18:17:37 -04006546 status = nfs4_set_lock_state(state, request);
6547 if (status != 0)
6548 return status;
6549
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006550 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551}
6552
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006553int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006554{
6555 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006556 int err;
6557
6558 err = nfs4_set_lock_state(state, fl);
6559 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006560 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006561 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006562 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006563}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006564
Trond Myklebustcf470c32012-03-07 13:49:12 -05006565struct nfs_release_lockowner_data {
6566 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006567 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006568 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006569 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006570 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006571};
6572
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006573static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6574{
6575 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006576 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006577 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6578 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006579 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006580 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006581}
6582
6583static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6584{
6585 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006586 struct nfs_server *server = data->server;
6587
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006588 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006589
6590 switch (task->tk_status) {
6591 case 0:
6592 renew_lease(server, data->timestamp);
6593 break;
6594 case -NFS4ERR_STALE_CLIENTID:
6595 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006596 nfs4_schedule_lease_recovery(server->nfs_client);
6597 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006598 case -NFS4ERR_LEASE_MOVED:
6599 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006600 if (nfs4_async_handle_error(task, server,
6601 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006602 rpc_restart_call_prepare(task);
6603 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006604}
6605
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006606static void nfs4_release_lockowner_release(void *calldata)
6607{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006608 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006609 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006610 kfree(calldata);
6611}
6612
Trond Myklebust17280172012-03-11 13:11:00 -04006613static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006614 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6615 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006616 .rpc_release = nfs4_release_lockowner_release,
6617};
6618
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006619static void
6620nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006621{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006622 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006623 struct rpc_message msg = {
6624 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6625 };
6626
6627 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006628 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006629
Trond Myklebustcf470c32012-03-07 13:49:12 -05006630 data = kmalloc(sizeof(*data), GFP_NOFS);
6631 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006632 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006633 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006634 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006635 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6636 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6637 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006638
Trond Myklebustcf470c32012-03-07 13:49:12 -05006639 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006640 msg.rpc_resp = &data->res;
6641 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006642 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006643}
6644
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006645#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6646
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006647static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006648 struct dentry *unused, struct inode *inode,
6649 const char *key, const void *buf,
6650 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006651{
Al Viro59301222016-05-27 10:19:30 -04006652 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006653}
6654
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006655static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006656 struct dentry *unused, struct inode *inode,
6657 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006658{
Al Virob2968212016-04-10 20:48:24 -04006659 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006660}
6661
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006662static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006663{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006664 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006665}
6666
David Quigleyc9bccef2013-05-22 12:50:45 -04006667#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006668
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006669static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006670 struct dentry *unused, struct inode *inode,
6671 const char *key, const void *buf,
6672 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006673{
6674 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006675 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006676
6677 return -EOPNOTSUPP;
6678}
6679
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006680static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006681 struct dentry *unused, struct inode *inode,
6682 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006683{
6684 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006685 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006686 return -EOPNOTSUPP;
6687}
6688
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006689static ssize_t
6690nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006691{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006692 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006693
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006694 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6695 len = security_inode_listsecurity(inode, list, list_len);
6696 if (list_len && len > list_len)
6697 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006698 }
6699 return len;
6700}
6701
6702static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6703 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006704 .get = nfs4_xattr_get_nfs4_label,
6705 .set = nfs4_xattr_set_nfs4_label,
6706};
David Quigleyc9bccef2013-05-22 12:50:45 -04006707
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006708#else
6709
6710static ssize_t
6711nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6712{
6713 return 0;
6714}
6715
6716#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006717
Andy Adamson533eb462011-06-13 18:25:56 -04006718/*
6719 * nfs_fhget will use either the mounted_on_fileid or the fileid
6720 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006721static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6722{
Andy Adamson533eb462011-06-13 18:25:56 -04006723 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6724 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6725 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006726 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006727 return;
6728
6729 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006730 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006731 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6732 fattr->nlink = 2;
6733}
6734
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006735static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6736 const struct qstr *name,
6737 struct nfs4_fs_locations *fs_locations,
6738 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006739{
6740 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006741 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006742 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006743 };
6744 struct nfs4_fs_locations_arg args = {
6745 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006746 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006747 .page = page,
6748 .bitmask = bitmask,
6749 };
Benny Halevy22958462009-04-01 09:22:02 -04006750 struct nfs4_fs_locations_res res = {
6751 .fs_locations = fs_locations,
6752 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006753 struct rpc_message msg = {
6754 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6755 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006756 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006757 };
6758 int status;
6759
Harvey Harrison3110ff82008-05-02 13:42:44 -07006760 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006761
6762 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6763 * is not supported */
6764 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6765 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6766 else
6767 bitmask[0] |= FATTR4_WORD0_FILEID;
6768
Trond Myklebustc228fd32007-01-13 02:28:11 -05006769 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006770 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006771 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006772 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006773 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006774 return status;
6775}
6776
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006777int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6778 const struct qstr *name,
6779 struct nfs4_fs_locations *fs_locations,
6780 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006781{
6782 struct nfs4_exception exception = { };
6783 int err;
6784 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006785 err = _nfs4_proc_fs_locations(client, dir, name,
6786 fs_locations, page);
6787 trace_nfs4_get_fs_locations(dir, name, err);
6788 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006789 &exception);
6790 } while (exception.retry);
6791 return err;
6792}
6793
Chuck Leverb03d7352013-10-17 14:12:50 -04006794/*
6795 * This operation also signals the server that this client is
6796 * performing migration recovery. The server can stop returning
6797 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6798 * appended to this compound to identify the client ID which is
6799 * performing recovery.
6800 */
6801static int _nfs40_proc_get_locations(struct inode *inode,
6802 struct nfs4_fs_locations *locations,
6803 struct page *page, struct rpc_cred *cred)
6804{
6805 struct nfs_server *server = NFS_SERVER(inode);
6806 struct rpc_clnt *clnt = server->client;
6807 u32 bitmask[2] = {
6808 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6809 };
6810 struct nfs4_fs_locations_arg args = {
6811 .clientid = server->nfs_client->cl_clientid,
6812 .fh = NFS_FH(inode),
6813 .page = page,
6814 .bitmask = bitmask,
6815 .migration = 1, /* skip LOOKUP */
6816 .renew = 1, /* append RENEW */
6817 };
6818 struct nfs4_fs_locations_res res = {
6819 .fs_locations = locations,
6820 .migration = 1,
6821 .renew = 1,
6822 };
6823 struct rpc_message msg = {
6824 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6825 .rpc_argp = &args,
6826 .rpc_resp = &res,
6827 .rpc_cred = cred,
6828 };
6829 unsigned long now = jiffies;
6830 int status;
6831
6832 nfs_fattr_init(&locations->fattr);
6833 locations->server = server;
6834 locations->nlocations = 0;
6835
6836 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6837 nfs4_set_sequence_privileged(&args.seq_args);
6838 status = nfs4_call_sync_sequence(clnt, server, &msg,
6839 &args.seq_args, &res.seq_res);
6840 if (status)
6841 return status;
6842
6843 renew_lease(server, now);
6844 return 0;
6845}
6846
6847#ifdef CONFIG_NFS_V4_1
6848
6849/*
6850 * This operation also signals the server that this client is
6851 * performing migration recovery. The server can stop asserting
6852 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6853 * performing this operation is identified in the SEQUENCE
6854 * operation in this compound.
6855 *
6856 * When the client supports GETATTR(fs_locations_info), it can
6857 * be plumbed in here.
6858 */
6859static int _nfs41_proc_get_locations(struct inode *inode,
6860 struct nfs4_fs_locations *locations,
6861 struct page *page, struct rpc_cred *cred)
6862{
6863 struct nfs_server *server = NFS_SERVER(inode);
6864 struct rpc_clnt *clnt = server->client;
6865 u32 bitmask[2] = {
6866 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6867 };
6868 struct nfs4_fs_locations_arg args = {
6869 .fh = NFS_FH(inode),
6870 .page = page,
6871 .bitmask = bitmask,
6872 .migration = 1, /* skip LOOKUP */
6873 };
6874 struct nfs4_fs_locations_res res = {
6875 .fs_locations = locations,
6876 .migration = 1,
6877 };
6878 struct rpc_message msg = {
6879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6880 .rpc_argp = &args,
6881 .rpc_resp = &res,
6882 .rpc_cred = cred,
6883 };
6884 int status;
6885
6886 nfs_fattr_init(&locations->fattr);
6887 locations->server = server;
6888 locations->nlocations = 0;
6889
6890 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6891 nfs4_set_sequence_privileged(&args.seq_args);
6892 status = nfs4_call_sync_sequence(clnt, server, &msg,
6893 &args.seq_args, &res.seq_res);
6894 if (status == NFS4_OK &&
6895 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6896 status = -NFS4ERR_LEASE_MOVED;
6897 return status;
6898}
6899
6900#endif /* CONFIG_NFS_V4_1 */
6901
6902/**
6903 * nfs4_proc_get_locations - discover locations for a migrated FSID
6904 * @inode: inode on FSID that is migrating
6905 * @locations: result of query
6906 * @page: buffer
6907 * @cred: credential to use for this operation
6908 *
6909 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6910 * operation failed, or a negative errno if a local error occurred.
6911 *
6912 * On success, "locations" is filled in, but if the server has
6913 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6914 * asserted.
6915 *
6916 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6917 * from this client that require migration recovery.
6918 */
6919int nfs4_proc_get_locations(struct inode *inode,
6920 struct nfs4_fs_locations *locations,
6921 struct page *page, struct rpc_cred *cred)
6922{
6923 struct nfs_server *server = NFS_SERVER(inode);
6924 struct nfs_client *clp = server->nfs_client;
6925 const struct nfs4_mig_recovery_ops *ops =
6926 clp->cl_mvops->mig_recovery_ops;
6927 struct nfs4_exception exception = { };
6928 int status;
6929
6930 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6931 (unsigned long long)server->fsid.major,
6932 (unsigned long long)server->fsid.minor,
6933 clp->cl_hostname);
6934 nfs_display_fhandle(NFS_FH(inode), __func__);
6935
6936 do {
6937 status = ops->get_locations(inode, locations, page, cred);
6938 if (status != -NFS4ERR_DELAY)
6939 break;
6940 nfs4_handle_exception(server, status, &exception);
6941 } while (exception.retry);
6942 return status;
6943}
6944
Chuck Lever44c99932013-10-17 14:13:30 -04006945/*
6946 * This operation also signals the server that this client is
6947 * performing "lease moved" recovery. The server can stop
6948 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6949 * is appended to this compound to identify the client ID which is
6950 * performing recovery.
6951 */
6952static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6953{
6954 struct nfs_server *server = NFS_SERVER(inode);
6955 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6956 struct rpc_clnt *clnt = server->client;
6957 struct nfs4_fsid_present_arg args = {
6958 .fh = NFS_FH(inode),
6959 .clientid = clp->cl_clientid,
6960 .renew = 1, /* append RENEW */
6961 };
6962 struct nfs4_fsid_present_res res = {
6963 .renew = 1,
6964 };
6965 struct rpc_message msg = {
6966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6967 .rpc_argp = &args,
6968 .rpc_resp = &res,
6969 .rpc_cred = cred,
6970 };
6971 unsigned long now = jiffies;
6972 int status;
6973
6974 res.fh = nfs_alloc_fhandle();
6975 if (res.fh == NULL)
6976 return -ENOMEM;
6977
6978 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6979 nfs4_set_sequence_privileged(&args.seq_args);
6980 status = nfs4_call_sync_sequence(clnt, server, &msg,
6981 &args.seq_args, &res.seq_res);
6982 nfs_free_fhandle(res.fh);
6983 if (status)
6984 return status;
6985
6986 do_renew_lease(clp, now);
6987 return 0;
6988}
6989
6990#ifdef CONFIG_NFS_V4_1
6991
6992/*
6993 * This operation also signals the server that this client is
6994 * performing "lease moved" recovery. The server can stop asserting
6995 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6996 * this operation is identified in the SEQUENCE operation in this
6997 * compound.
6998 */
6999static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7000{
7001 struct nfs_server *server = NFS_SERVER(inode);
7002 struct rpc_clnt *clnt = server->client;
7003 struct nfs4_fsid_present_arg args = {
7004 .fh = NFS_FH(inode),
7005 };
7006 struct nfs4_fsid_present_res res = {
7007 };
7008 struct rpc_message msg = {
7009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7010 .rpc_argp = &args,
7011 .rpc_resp = &res,
7012 .rpc_cred = cred,
7013 };
7014 int status;
7015
7016 res.fh = nfs_alloc_fhandle();
7017 if (res.fh == NULL)
7018 return -ENOMEM;
7019
7020 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7021 nfs4_set_sequence_privileged(&args.seq_args);
7022 status = nfs4_call_sync_sequence(clnt, server, &msg,
7023 &args.seq_args, &res.seq_res);
7024 nfs_free_fhandle(res.fh);
7025 if (status == NFS4_OK &&
7026 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7027 status = -NFS4ERR_LEASE_MOVED;
7028 return status;
7029}
7030
7031#endif /* CONFIG_NFS_V4_1 */
7032
7033/**
7034 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7035 * @inode: inode on FSID to check
7036 * @cred: credential to use for this operation
7037 *
7038 * Server indicates whether the FSID is present, moved, or not
7039 * recognized. This operation is necessary to clear a LEASE_MOVED
7040 * condition for this client ID.
7041 *
7042 * Returns NFS4_OK if the FSID is present on this server,
7043 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7044 * NFS4ERR code if some error occurred on the server, or a
7045 * negative errno if a local failure occurred.
7046 */
7047int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7048{
7049 struct nfs_server *server = NFS_SERVER(inode);
7050 struct nfs_client *clp = server->nfs_client;
7051 const struct nfs4_mig_recovery_ops *ops =
7052 clp->cl_mvops->mig_recovery_ops;
7053 struct nfs4_exception exception = { };
7054 int status;
7055
7056 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7057 (unsigned long long)server->fsid.major,
7058 (unsigned long long)server->fsid.minor,
7059 clp->cl_hostname);
7060 nfs_display_fhandle(NFS_FH(inode), __func__);
7061
7062 do {
7063 status = ops->fsid_present(inode, cred);
7064 if (status != -NFS4ERR_DELAY)
7065 break;
7066 nfs4_handle_exception(server, status, &exception);
7067 } while (exception.retry);
7068 return status;
7069}
7070
Andy Adamson5ec16a82013-08-08 10:57:55 -04007071/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007072 * If 'use_integrity' is true and the state managment nfs_client
7073 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7074 * and the machine credential as per RFC3530bis and RFC5661 Security
7075 * Considerations sections. Otherwise, just use the user cred with the
7076 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007077 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007078static 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 +00007079{
7080 int status;
7081 struct nfs4_secinfo_arg args = {
7082 .dir_fh = NFS_FH(dir),
7083 .name = name,
7084 };
7085 struct nfs4_secinfo_res res = {
7086 .flavors = flavors,
7087 };
7088 struct rpc_message msg = {
7089 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7090 .rpc_argp = &args,
7091 .rpc_resp = &res,
7092 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007093 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007094 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007095
7096 if (use_integrity) {
7097 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007098 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7099 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007100 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007101
7102 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007103
7104 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7105 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7106
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007107 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7108 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007109 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007110
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007111 if (cred)
7112 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007113
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007114 return status;
7115}
7116
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007117int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7118 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007119{
7120 struct nfs4_exception exception = { };
7121 int err;
7122 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007123 err = -NFS4ERR_WRONGSEC;
7124
7125 /* try to use integrity protection with machine cred */
7126 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7127 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7128
7129 /*
7130 * if unable to use integrity protection, or SECINFO with
7131 * integrity protection returns NFS4ERR_WRONGSEC (which is
7132 * disallowed by spec, but exists in deployed servers) use
7133 * the current filesystem's rpc_client and the user cred.
7134 */
7135 if (err == -NFS4ERR_WRONGSEC)
7136 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7137
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007138 trace_nfs4_secinfo(dir, name, err);
7139 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007140 &exception);
7141 } while (exception.retry);
7142 return err;
7143}
7144
Andy Adamson557134a2009-04-01 09:21:53 -04007145#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007146/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007147 * Check the exchange flags returned by the server for invalid flags, having
7148 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7149 * DS flags set.
7150 */
7151static int nfs4_check_cl_exchange_flags(u32 flags)
7152{
7153 if (flags & ~EXCHGID4_FLAG_MASK_R)
7154 goto out_inval;
7155 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7156 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7157 goto out_inval;
7158 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7159 goto out_inval;
7160 return NFS_OK;
7161out_inval:
7162 return -NFS4ERR_INVAL;
7163}
7164
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007165static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007166nfs41_same_server_scope(struct nfs41_server_scope *a,
7167 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007168{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007169 if (a->server_scope_sz != b->server_scope_sz)
7170 return false;
7171 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007172}
7173
Andy Adamson02a95de2016-02-05 16:08:37 -05007174static void
7175nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7176{
7177}
7178
7179static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7180 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7181};
7182
Andy Adamson357f54d2010-12-14 10:11:57 -05007183/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007184 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007185 *
7186 * The 4.1 client currently uses the same TCP connection for the
7187 * fore and backchannel.
7188 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007189static
7190int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7191 struct rpc_xprt *xprt,
7192 struct nfs_client *clp,
7193 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007194{
7195 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007196 struct nfs41_bind_conn_to_session_args args = {
7197 .client = clp,
7198 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7199 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007200 struct nfs41_bind_conn_to_session_res res;
7201 struct rpc_message msg = {
7202 .rpc_proc =
7203 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007204 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007205 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007206 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007207 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007208 struct rpc_task_setup task_setup_data = {
7209 .rpc_client = clnt,
7210 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007211 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007212 .rpc_message = &msg,
7213 .flags = RPC_TASK_TIMEOUT,
7214 };
7215 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007216
Trond Myklebust71a097c2015-02-18 09:27:18 -08007217 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7218 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7219 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007220
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007221 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7222 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7223 args.dir = NFS4_CDFC4_FORE;
7224
7225 task = rpc_run_task(&task_setup_data);
7226 if (!IS_ERR(task)) {
7227 status = task->tk_status;
7228 rpc_put_task(task);
7229 } else
7230 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007231 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007232 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007233 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007234 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7235 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007236 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007237 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007238 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007239 dprintk("NFS: %s: Unexpected direction from server\n",
7240 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007241 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007242 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007243 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007244 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7245 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007246 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007247 }
7248 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007249
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007250 return status;
7251}
7252
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007253struct rpc_bind_conn_calldata {
7254 struct nfs_client *clp;
7255 struct rpc_cred *cred;
7256};
7257
7258static int
7259nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7260 struct rpc_xprt *xprt,
7261 void *calldata)
7262{
7263 struct rpc_bind_conn_calldata *p = calldata;
7264
7265 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7266}
7267
7268int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7269{
7270 struct rpc_bind_conn_calldata data = {
7271 .clp = clp,
7272 .cred = cred,
7273 };
7274 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7275 nfs4_proc_bind_conn_to_session_callback, &data);
7276}
7277
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007278/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007279 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7280 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007281 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007282static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7283 .how = SP4_MACH_CRED,
7284 .enforce.u.words = {
7285 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7286 1 << (OP_EXCHANGE_ID - 32) |
7287 1 << (OP_CREATE_SESSION - 32) |
7288 1 << (OP_DESTROY_SESSION - 32) |
7289 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007290 },
7291 .allow.u.words = {
7292 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007293 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007294 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007295 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007296 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007297 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007298 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007299 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007300 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007301 1 << (OP_FREE_STATEID - 32) |
7302 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007303 }
7304};
7305
7306/*
7307 * Select the state protection mode for client `clp' given the server results
7308 * from exchange_id in `sp'.
7309 *
7310 * Returns 0 on success, negative errno otherwise.
7311 */
7312static int nfs4_sp4_select_mode(struct nfs_client *clp,
7313 struct nfs41_state_protection *sp)
7314{
7315 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7316 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7317 1 << (OP_EXCHANGE_ID - 32) |
7318 1 << (OP_CREATE_SESSION - 32) |
7319 1 << (OP_DESTROY_SESSION - 32) |
7320 1 << (OP_DESTROY_CLIENTID - 32)
7321 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007322 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007323 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007324 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007325
7326 if (sp->how == SP4_MACH_CRED) {
7327 /* Print state protect result */
7328 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7329 for (i = 0; i <= LAST_NFS4_OP; i++) {
7330 if (test_bit(i, sp->enforce.u.longs))
7331 dfprintk(MOUNT, " enforce op %d\n", i);
7332 if (test_bit(i, sp->allow.u.longs))
7333 dfprintk(MOUNT, " allow op %d\n", i);
7334 }
7335
7336 /* make sure nothing is on enforce list that isn't supported */
7337 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7338 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7339 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007340 ret = -EINVAL;
7341 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007342 }
7343 }
7344
7345 /*
7346 * Minimal mode - state operations are allowed to use machine
7347 * credential. Note this already happens by default, so the
7348 * client doesn't have to do anything more than the negotiation.
7349 *
7350 * NOTE: we don't care if EXCHANGE_ID is in the list -
7351 * we're already using the machine cred for exchange_id
7352 * and will never use a different cred.
7353 */
7354 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7355 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7356 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7357 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7358 dfprintk(MOUNT, "sp4_mach_cred:\n");
7359 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007360 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007361 } else {
7362 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007363 ret = -EINVAL;
7364 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007365 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007366
7367 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007368 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7369 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007370 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7371 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007372 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007373 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007374
Andrew Elble99ade3c2015-12-02 09:39:51 -05007375 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7376 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007377 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007378 }
7379
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007380 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7381 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7382 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007383 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007384 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007385
7386 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7387 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7388 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007389 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007390 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007391
7392 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7393 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007394 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007395 }
7396
7397 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7398 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007399 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007400 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007401 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007402out:
7403 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007404 return 0;
7405}
7406
Andy Adamson8d89bd72016-09-09 09:22:18 -04007407struct nfs41_exchange_id_data {
7408 struct nfs41_exchange_id_res res;
7409 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007410};
7411
Andy Adamson8d89bd72016-09-09 09:22:18 -04007412static void nfs4_exchange_id_release(void *data)
7413{
7414 struct nfs41_exchange_id_data *cdata =
7415 (struct nfs41_exchange_id_data *)data;
7416
Olga Kornievskaia63513232017-03-13 10:36:19 -04007417 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007418 kfree(cdata->res.impl_id);
7419 kfree(cdata->res.server_scope);
7420 kfree(cdata->res.server_owner);
7421 kfree(cdata);
7422}
7423
7424static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007425 .rpc_release = nfs4_exchange_id_release,
7426};
7427
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007428/*
7429 * _nfs4_proc_exchange_id()
7430 *
7431 * Wrapper for EXCHANGE_ID operation.
7432 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007433static struct rpc_task *
7434nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007435 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007436{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007437 struct rpc_message msg = {
7438 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007439 .rpc_cred = cred,
7440 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007441 struct rpc_task_setup task_setup_data = {
7442 .rpc_client = clp->cl_rpcclient,
7443 .callback_ops = &nfs4_exchange_id_call_ops,
7444 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007445 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007446 };
7447 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007448 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007449
7450 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007451 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007452
Trond Myklebust9c760d12017-07-31 18:38:50 -04007453 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007454 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007455 if (!calldata)
7456 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007457
Trond Myklebustfd405592017-08-01 16:02:47 -04007458 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007459
7460 status = nfs4_init_uniform_client_string(clp);
7461 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007462 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007463
Andy Adamson8d89bd72016-09-09 09:22:18 -04007464 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7465 GFP_NOFS);
7466 status = -ENOMEM;
7467 if (unlikely(calldata->res.server_owner == NULL))
7468 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007469
Andy Adamson8d89bd72016-09-09 09:22:18 -04007470 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007471 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007472 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007473 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007474
Andy Adamson8d89bd72016-09-09 09:22:18 -04007475 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7476 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007477 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007478
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007479 switch (sp4_how) {
7480 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007481 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007482 break;
7483
7484 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007485 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007486 break;
7487
7488 default:
7489 /* unsupported! */
7490 WARN_ON_ONCE(1);
7491 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007492 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007493 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007494 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007495 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007496 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007497 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7498 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007499 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007500 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007501 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7502 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007503#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007504 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007505#endif
7506 msg.rpc_argp = &calldata->args;
7507 msg.rpc_resp = &calldata->res;
7508 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007509
Trond Myklebust9c760d12017-07-31 18:38:50 -04007510 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007511
7512out_impl_id:
7513 kfree(calldata->res.impl_id);
7514out_server_scope:
7515 kfree(calldata->res.server_scope);
7516out_server_owner:
7517 kfree(calldata->res.server_owner);
7518out_calldata:
7519 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007520out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007521 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007522 return ERR_PTR(status);
7523}
7524
7525/*
7526 * _nfs4_proc_exchange_id()
7527 *
7528 * Wrapper for EXCHANGE_ID operation.
7529 */
7530static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7531 u32 sp4_how)
7532{
7533 struct rpc_task *task;
7534 struct nfs41_exchange_id_args *argp;
7535 struct nfs41_exchange_id_res *resp;
7536 int status;
7537
7538 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7539 if (IS_ERR(task))
7540 return PTR_ERR(task);
7541
7542 argp = task->tk_msg.rpc_argp;
7543 resp = task->tk_msg.rpc_resp;
7544 status = task->tk_status;
7545 if (status != 0)
7546 goto out;
7547
7548 status = nfs4_check_cl_exchange_flags(resp->flags);
7549 if (status != 0)
7550 goto out;
7551
7552 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7553 if (status != 0)
7554 goto out;
7555
7556 clp->cl_clientid = resp->clientid;
7557 clp->cl_exchange_flags = resp->flags;
7558 clp->cl_seqid = resp->seqid;
7559 /* Client ID is not confirmed */
7560 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7561 clear_bit(NFS4_SESSION_ESTABLISHED,
7562 &clp->cl_session->session_state);
7563
7564 if (clp->cl_serverscope != NULL &&
7565 !nfs41_same_server_scope(clp->cl_serverscope,
7566 resp->server_scope)) {
7567 dprintk("%s: server_scope mismatch detected\n",
7568 __func__);
7569 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7570 }
7571
7572 swap(clp->cl_serverowner, resp->server_owner);
7573 swap(clp->cl_serverscope, resp->server_scope);
7574 swap(clp->cl_implid, resp->impl_id);
7575
7576 /* Save the EXCHANGE_ID verifier session trunk tests */
7577 memcpy(clp->cl_confirm.data, argp->verifier.data,
7578 sizeof(clp->cl_confirm.data));
7579out:
7580 trace_nfs4_exchange_id(clp, status);
7581 rpc_put_task(task);
7582 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007583}
7584
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007585/*
7586 * nfs4_proc_exchange_id()
7587 *
7588 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7589 *
7590 * Since the clientid has expired, all compounds using sessions
7591 * associated with the stale clientid will be returning
7592 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7593 * be in some phase of session reset.
7594 *
7595 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7596 */
7597int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7598{
7599 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7600 int status;
7601
7602 /* try SP4_MACH_CRED if krb5i/p */
7603 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7604 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007605 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007606 if (!status)
7607 return 0;
7608 }
7609
7610 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007611 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007612}
7613
Andy Adamson04fa2c62016-09-09 09:22:29 -04007614/**
7615 * nfs4_test_session_trunk
7616 *
7617 * This is an add_xprt_test() test function called from
7618 * rpc_clnt_setup_test_and_add_xprt.
7619 *
7620 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7621 * and is dereferrenced in nfs4_exchange_id_release
7622 *
7623 * Upon success, add the new transport to the rpc_clnt
7624 *
7625 * @clnt: struct rpc_clnt to get new transport
7626 * @xprt: the rpc_xprt to test
7627 * @data: call data for _nfs4_proc_exchange_id.
7628 */
7629int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7630 void *data)
7631{
7632 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007633 struct rpc_task *task;
7634 int status;
7635
Andy Adamson04fa2c62016-09-09 09:22:29 -04007636 u32 sp4_how;
7637
7638 dprintk("--> %s try %s\n", __func__,
7639 xprt->address_strings[RPC_DISPLAY_ADDR]);
7640
7641 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7642
7643 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007644 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7645 if (IS_ERR(task))
7646 return PTR_ERR(task);
7647
7648 status = task->tk_status;
7649 if (status == 0)
7650 status = nfs4_detect_session_trunking(adata->clp,
7651 task->tk_msg.rpc_resp, xprt);
7652
7653 rpc_put_task(task);
7654 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007655}
7656EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7657
Trond Myklebust66245532012-05-25 17:18:09 -04007658static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7659 struct rpc_cred *cred)
7660{
7661 struct rpc_message msg = {
7662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7663 .rpc_argp = clp,
7664 .rpc_cred = cred,
7665 };
7666 int status;
7667
7668 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007669 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007670 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007671 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007672 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7673 return status;
7674}
7675
7676static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7677 struct rpc_cred *cred)
7678{
7679 unsigned int loop;
7680 int ret;
7681
7682 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7683 ret = _nfs4_proc_destroy_clientid(clp, cred);
7684 switch (ret) {
7685 case -NFS4ERR_DELAY:
7686 case -NFS4ERR_CLIENTID_BUSY:
7687 ssleep(1);
7688 break;
7689 default:
7690 return ret;
7691 }
7692 }
7693 return 0;
7694}
7695
7696int nfs4_destroy_clientid(struct nfs_client *clp)
7697{
7698 struct rpc_cred *cred;
7699 int ret = 0;
7700
7701 if (clp->cl_mvops->minor_version < 1)
7702 goto out;
7703 if (clp->cl_exchange_flags == 0)
7704 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007705 if (clp->cl_preserve_clid)
7706 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007707 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007708 ret = nfs4_proc_destroy_clientid(clp, cred);
7709 if (cred)
7710 put_rpccred(cred);
7711 switch (ret) {
7712 case 0:
7713 case -NFS4ERR_STALE_CLIENTID:
7714 clp->cl_exchange_flags = 0;
7715 }
7716out:
7717 return ret;
7718}
7719
Andy Adamson2050f0c2009-04-01 09:22:30 -04007720struct nfs4_get_lease_time_data {
7721 struct nfs4_get_lease_time_args *args;
7722 struct nfs4_get_lease_time_res *res;
7723 struct nfs_client *clp;
7724};
7725
7726static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7727 void *calldata)
7728{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007729 struct nfs4_get_lease_time_data *data =
7730 (struct nfs4_get_lease_time_data *)calldata;
7731
7732 dprintk("--> %s\n", __func__);
7733 /* just setup sequence, do not trigger session recovery
7734 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007735 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007736 &data->args->la_seq_args,
7737 &data->res->lr_seq_res,
7738 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007739 dprintk("<-- %s\n", __func__);
7740}
7741
7742/*
7743 * Called from nfs4_state_manager thread for session setup, so don't recover
7744 * from sequence operation or clientid errors.
7745 */
7746static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7747{
7748 struct nfs4_get_lease_time_data *data =
7749 (struct nfs4_get_lease_time_data *)calldata;
7750
7751 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007752 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7753 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007754 switch (task->tk_status) {
7755 case -NFS4ERR_DELAY:
7756 case -NFS4ERR_GRACE:
7757 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7758 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7759 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007760 /* fall through */
7761 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007762 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007763 return;
7764 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007765 dprintk("<-- %s\n", __func__);
7766}
7767
Trond Myklebust17280172012-03-11 13:11:00 -04007768static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007769 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7770 .rpc_call_done = nfs4_get_lease_time_done,
7771};
7772
7773int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7774{
7775 struct rpc_task *task;
7776 struct nfs4_get_lease_time_args args;
7777 struct nfs4_get_lease_time_res res = {
7778 .lr_fsinfo = fsinfo,
7779 };
7780 struct nfs4_get_lease_time_data data = {
7781 .args = &args,
7782 .res = &res,
7783 .clp = clp,
7784 };
7785 struct rpc_message msg = {
7786 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7787 .rpc_argp = &args,
7788 .rpc_resp = &res,
7789 };
7790 struct rpc_task_setup task_setup = {
7791 .rpc_client = clp->cl_rpcclient,
7792 .rpc_message = &msg,
7793 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007794 .callback_data = &data,
7795 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007796 };
7797 int status;
7798
Chuck Levera9c92d62013-08-09 12:48:18 -04007799 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007800 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007801 task = rpc_run_task(&task_setup);
7802
7803 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04007804 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007805
Anna Schumakerf6148712017-04-07 14:15:23 -04007806 status = task->tk_status;
7807 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007808 return status;
7809}
7810
Andy Adamsonfc931582009-04-01 09:22:31 -04007811/*
7812 * Initialize the values to be used by the client in CREATE_SESSION
7813 * If nfs4_init_session set the fore channel request and response sizes,
7814 * use them.
7815 *
7816 * Set the back channel max_resp_sz_cached to zero to force the client to
7817 * always set csa_cachethis to FALSE because the current implementation
7818 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7819 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007820static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7821 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007822{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007823 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007824 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007825
Andy Adamson18aad3d2013-06-26 12:21:49 -04007826 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7827 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7828
Andy Adamsonfc931582009-04-01 09:22:31 -04007829 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007830 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7831 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007832 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007833 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007834
7835 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007836 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007837 __func__,
7838 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007839 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007840
7841 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007842 args->bc_attrs.max_rqst_sz = max_bc_payload;
7843 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007844 args->bc_attrs.max_resp_sz_cached = 0;
7845 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007846 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007847
7848 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7849 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7850 __func__,
7851 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7852 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7853 args->bc_attrs.max_reqs);
7854}
7855
Trond Myklebust79969dd2015-02-18 11:30:18 -08007856static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7857 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007858{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007859 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007860 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007861
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007862 if (rcvd->max_resp_sz > sent->max_resp_sz)
7863 return -EINVAL;
7864 /*
7865 * Our requested max_ops is the minimum we need; we're not
7866 * prepared to break up compounds into smaller pieces than that.
7867 * So, no point even trying to continue if the server won't
7868 * cooperate:
7869 */
7870 if (rcvd->max_ops < sent->max_ops)
7871 return -EINVAL;
7872 if (rcvd->max_reqs == 0)
7873 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007874 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7875 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007876 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007877}
7878
Trond Myklebust79969dd2015-02-18 11:30:18 -08007879static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7880 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007881{
7882 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007883 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007884
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007885 if (!(res->flags & SESSION4_BACK_CHAN))
7886 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007887 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7888 return -EINVAL;
7889 if (rcvd->max_resp_sz < sent->max_resp_sz)
7890 return -EINVAL;
7891 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7892 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007893 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007894 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007895 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007896 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007897out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007898 return 0;
7899}
Andy Adamson8d353012009-04-01 09:22:32 -04007900
Andy Adamson8d353012009-04-01 09:22:32 -04007901static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007902 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007903{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007904 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007905
Trond Myklebust79969dd2015-02-18 11:30:18 -08007906 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007907 if (ret)
7908 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007909 return nfs4_verify_back_channel_attrs(args, res);
7910}
7911
7912static void nfs4_update_session(struct nfs4_session *session,
7913 struct nfs41_create_session_res *res)
7914{
7915 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007916 /* Mark client id and session as being confirmed */
7917 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7918 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007919 session->flags = res->flags;
7920 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007921 if (res->flags & SESSION4_BACK_CHAN)
7922 memcpy(&session->bc_attrs, &res->bc_attrs,
7923 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007924}
7925
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007926static int _nfs4_proc_create_session(struct nfs_client *clp,
7927 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007928{
7929 struct nfs4_session *session = clp->cl_session;
7930 struct nfs41_create_session_args args = {
7931 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007932 .clientid = clp->cl_clientid,
7933 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007934 .cb_program = NFS4_CALLBACK,
7935 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007936 struct nfs41_create_session_res res;
7937
Andy Adamsonfc931582009-04-01 09:22:31 -04007938 struct rpc_message msg = {
7939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7940 .rpc_argp = &args,
7941 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007942 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007943 };
7944 int status;
7945
Chuck Lever6b26cc82016-05-02 14:40:40 -04007946 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007947 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007948
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007949 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007950 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007951
Trond Myklebustb519d402016-09-11 14:50:01 -04007952 switch (status) {
7953 case -NFS4ERR_STALE_CLIENTID:
7954 case -NFS4ERR_DELAY:
7955 case -ETIMEDOUT:
7956 case -EACCES:
7957 case -EAGAIN:
7958 goto out;
7959 };
7960
7961 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007962 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007963 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007964 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007965 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007966 if (status)
7967 goto out;
7968 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007969 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007970out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007971 return status;
7972}
7973
7974/*
7975 * Issues a CREATE_SESSION operation to the server.
7976 * It is the responsibility of the caller to verify the session is
7977 * expired before calling this routine.
7978 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007979int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007980{
7981 int status;
7982 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007983 struct nfs4_session *session = clp->cl_session;
7984
7985 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7986
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007987 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007988 if (status)
7989 goto out;
7990
Andy Adamsonaacd5532011-11-09 13:58:21 -05007991 /* Init or reset the session slot tables */
7992 status = nfs4_setup_session_slot_tables(session);
7993 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007994 if (status)
7995 goto out;
7996
7997 ptr = (unsigned *)&session->sess_id.data[0];
7998 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7999 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008000out:
8001 dprintk("<-- %s\n", __func__);
8002 return status;
8003}
8004
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008005/*
8006 * Issue the over-the-wire RPC DESTROY_SESSION.
8007 * The caller must serialize access to this routine.
8008 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008009int nfs4_proc_destroy_session(struct nfs4_session *session,
8010 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008011{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008012 struct rpc_message msg = {
8013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8014 .rpc_argp = session,
8015 .rpc_cred = cred,
8016 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008017 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008018
8019 dprintk("--> nfs4_proc_destroy_session\n");
8020
8021 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008022 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8023 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008024
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008025 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008026 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008027
8028 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008029 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008030 "Session has been destroyed regardless...\n", status);
8031
8032 dprintk("<-- nfs4_proc_destroy_session\n");
8033 return status;
8034}
8035
Trond Myklebust7b38c362012-05-23 13:23:31 -04008036/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008037 * Renew the cl_session lease.
8038 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008039struct nfs4_sequence_data {
8040 struct nfs_client *clp;
8041 struct nfs4_sequence_args args;
8042 struct nfs4_sequence_res res;
8043};
8044
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008045static void nfs41_sequence_release(void *data)
8046{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008047 struct nfs4_sequence_data *calldata = data;
8048 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008049
Alexandros Batsakis71358402010-02-05 03:45:05 -08008050 if (atomic_read(&clp->cl_count) > 1)
8051 nfs4_schedule_state_renewal(clp);
8052 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008053 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008054}
8055
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008056static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8057{
8058 switch(task->tk_status) {
8059 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008060 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8061 return -EAGAIN;
8062 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008063 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008064 }
8065 return 0;
8066}
8067
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008068static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008069{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008070 struct nfs4_sequence_data *calldata = data;
8071 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008072
Trond Myklebust14516c32010-07-31 14:29:06 -04008073 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8074 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008075
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008076 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008077 if (task->tk_status < 0) {
8078 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008079 if (atomic_read(&clp->cl_count) == 1)
8080 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008081
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008082 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8083 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008084 return;
8085 }
8086 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008087 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008088out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008089 dprintk("<-- %s\n", __func__);
8090}
8091
8092static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8093{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008094 struct nfs4_sequence_data *calldata = data;
8095 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008096 struct nfs4_sequence_args *args;
8097 struct nfs4_sequence_res *res;
8098
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008099 args = task->tk_msg.rpc_argp;
8100 res = task->tk_msg.rpc_resp;
8101
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008102 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008103}
8104
8105static const struct rpc_call_ops nfs41_sequence_ops = {
8106 .rpc_call_done = nfs41_sequence_call_done,
8107 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008108 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008109};
8110
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008111static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8112 struct rpc_cred *cred,
8113 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008114{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008115 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008116 struct rpc_message msg = {
8117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8118 .rpc_cred = cred,
8119 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008120 struct rpc_task_setup task_setup_data = {
8121 .rpc_client = clp->cl_rpcclient,
8122 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008123 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008124 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008125 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008126
Alexandros Batsakis71358402010-02-05 03:45:05 -08008127 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008128 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008129 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008130 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008131 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008132 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008133 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008134 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008135 if (is_privileged)
8136 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008137 msg.rpc_argp = &calldata->args;
8138 msg.rpc_resp = &calldata->res;
8139 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008140 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008141
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008142 return rpc_run_task(&task_setup_data);
8143}
8144
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008145static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008146{
8147 struct rpc_task *task;
8148 int ret = 0;
8149
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008150 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008151 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008152 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008153 if (IS_ERR(task))
8154 ret = PTR_ERR(task);
8155 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008156 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008157 dprintk("<-- %s status=%d\n", __func__, ret);
8158 return ret;
8159}
8160
8161static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8162{
8163 struct rpc_task *task;
8164 int ret;
8165
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008166 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008167 if (IS_ERR(task)) {
8168 ret = PTR_ERR(task);
8169 goto out;
8170 }
8171 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008172 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008173 ret = task->tk_status;
8174 rpc_put_task(task);
8175out:
8176 dprintk("<-- %s status=%d\n", __func__, ret);
8177 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008178}
8179
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008180struct nfs4_reclaim_complete_data {
8181 struct nfs_client *clp;
8182 struct nfs41_reclaim_complete_args arg;
8183 struct nfs41_reclaim_complete_res res;
8184};
8185
8186static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8187{
8188 struct nfs4_reclaim_complete_data *calldata = data;
8189
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008190 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008191 &calldata->arg.seq_args,
8192 &calldata->res.seq_res,
8193 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008194}
8195
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008196static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8197{
8198 switch(task->tk_status) {
8199 case 0:
8200 case -NFS4ERR_COMPLETE_ALREADY:
8201 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8202 break;
8203 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008204 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008205 /* fall through */
8206 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008207 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008208 case -NFS4ERR_BADSESSION:
8209 case -NFS4ERR_DEADSESSION:
8210 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8211 nfs4_schedule_session_recovery(clp->cl_session,
8212 task->tk_status);
8213 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008214 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008215 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008216 }
8217 return 0;
8218}
8219
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008220static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8221{
8222 struct nfs4_reclaim_complete_data *calldata = data;
8223 struct nfs_client *clp = calldata->clp;
8224 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8225
8226 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008227 if (!nfs41_sequence_done(task, res))
8228 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008229
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008230 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008231 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8232 rpc_restart_call_prepare(task);
8233 return;
8234 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008235 dprintk("<-- %s\n", __func__);
8236}
8237
8238static void nfs4_free_reclaim_complete_data(void *data)
8239{
8240 struct nfs4_reclaim_complete_data *calldata = data;
8241
8242 kfree(calldata);
8243}
8244
8245static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8246 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8247 .rpc_call_done = nfs4_reclaim_complete_done,
8248 .rpc_release = nfs4_free_reclaim_complete_data,
8249};
8250
8251/*
8252 * Issue a global reclaim complete.
8253 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008254static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8255 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008256{
8257 struct nfs4_reclaim_complete_data *calldata;
8258 struct rpc_task *task;
8259 struct rpc_message msg = {
8260 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008261 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008262 };
8263 struct rpc_task_setup task_setup_data = {
8264 .rpc_client = clp->cl_rpcclient,
8265 .rpc_message = &msg,
8266 .callback_ops = &nfs4_reclaim_complete_call_ops,
8267 .flags = RPC_TASK_ASYNC,
8268 };
8269 int status = -ENOMEM;
8270
8271 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008272 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008273 if (calldata == NULL)
8274 goto out;
8275 calldata->clp = clp;
8276 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008277
Chuck Levera9c92d62013-08-09 12:48:18 -04008278 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008279 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008280 msg.rpc_argp = &calldata->arg;
8281 msg.rpc_resp = &calldata->res;
8282 task_setup_data.callback_data = calldata;
8283 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008284 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008285 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008286 goto out;
8287 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008288 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008289 if (status == 0)
8290 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008291 rpc_put_task(task);
8292out:
8293 dprintk("<-- %s status=%d\n", __func__, status);
8294 return status;
8295}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008296
8297static void
8298nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8299{
8300 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008301 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008302
8303 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008304 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008305 &lgp->res.seq_res, task);
8306 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008307}
8308
8309static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8310{
8311 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008312
8313 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008314 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008315 dprintk("<-- %s\n", __func__);
8316}
8317
8318static int
8319nfs4_layoutget_handle_exception(struct rpc_task *task,
8320 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8321{
Trond Myklebustee314c22012-10-01 17:25:48 -07008322 struct inode *inode = lgp->args.inode;
8323 struct nfs_server *server = NFS_SERVER(inode);
8324 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008325 int nfs4err = task->tk_status;
8326 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008327 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008328
Boaz Harroshed7e5422014-01-22 20:34:54 +02008329 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008330
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008331 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008332 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008333 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008334
8335 /*
8336 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8337 * on the file. set tk_status to -ENODATA to tell upper layer to
8338 * retry go inband.
8339 */
8340 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008341 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008342 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008343 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008344 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8345 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8346 */
8347 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008348 status = -EOVERFLOW;
8349 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008350 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008351 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008352 * (or clients) writing to the same RAID stripe except when
8353 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008354 *
8355 * Treat it like we would RECALLCONFLICT -- we retry for a little
8356 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008357 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008358 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008359 if (lgp->args.minlength == 0) {
8360 status = -EOVERFLOW;
8361 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008362 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008363 status = -EBUSY;
8364 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008365 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008366 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008367 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008368 case -NFS4ERR_DELEG_REVOKED:
8369 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008370 case -NFS4ERR_EXPIRED:
8371 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008372 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008373 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008374 lo = NFS_I(inode)->layout;
8375 /* If the open stateid was bad, then recover it. */
8376 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8377 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008378 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008379 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008380 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008381 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008382 break;
8383 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008384
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008385 /*
8386 * Mark the bad layout state as invalid, then retry
8387 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008388 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008389 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008390 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008391 pnfs_free_lseg_list(&head);
8392 status = -EAGAIN;
8393 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008394 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008395
Trond Myklebust8ac09252017-01-23 22:44:12 -05008396 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008397 err = nfs4_handle_exception(server, nfs4err, exception);
8398 if (!status) {
8399 if (exception->retry)
8400 status = -EAGAIN;
8401 else
8402 status = err;
8403 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008404out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008405 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008406 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008407}
8408
Idan Kedar85541162012-08-02 11:47:10 +03008409static size_t max_response_pages(struct nfs_server *server)
8410{
8411 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8412 return nfs_page_array_len(0, max_resp_sz);
8413}
8414
8415static void nfs4_free_pages(struct page **pages, size_t size)
8416{
8417 int i;
8418
8419 if (!pages)
8420 return;
8421
8422 for (i = 0; i < size; i++) {
8423 if (!pages[i])
8424 break;
8425 __free_page(pages[i]);
8426 }
8427 kfree(pages);
8428}
8429
8430static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8431{
8432 struct page **pages;
8433 int i;
8434
8435 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8436 if (!pages) {
8437 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8438 return NULL;
8439 }
8440
8441 for (i = 0; i < size; i++) {
8442 pages[i] = alloc_page(gfp_flags);
8443 if (!pages[i]) {
8444 dprintk("%s: failed to allocate page\n", __func__);
8445 nfs4_free_pages(pages, size);
8446 return NULL;
8447 }
8448 }
8449
8450 return pages;
8451}
8452
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008453static void nfs4_layoutget_release(void *calldata)
8454{
8455 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008456 struct inode *inode = lgp->args.inode;
8457 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008458 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008459
8460 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008461 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008462 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008463 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008464 put_nfs_open_context(lgp->args.ctx);
8465 kfree(calldata);
8466 dprintk("<-- %s\n", __func__);
8467}
8468
8469static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8470 .rpc_call_prepare = nfs4_layoutget_prepare,
8471 .rpc_call_done = nfs4_layoutget_done,
8472 .rpc_release = nfs4_layoutget_release,
8473};
8474
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008475struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008476nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008477{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008478 struct inode *inode = lgp->args.inode;
8479 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008480 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008481 struct rpc_task *task;
8482 struct rpc_message msg = {
8483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8484 .rpc_argp = &lgp->args,
8485 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008486 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008487 };
8488 struct rpc_task_setup task_setup_data = {
8489 .rpc_client = server->client,
8490 .rpc_message = &msg,
8491 .callback_ops = &nfs4_layoutget_call_ops,
8492 .callback_data = lgp,
8493 .flags = RPC_TASK_ASYNC,
8494 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008495 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008496 struct nfs4_exception exception = {
8497 .inode = inode,
8498 .timeout = *timeout,
8499 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008500 int status = 0;
8501
8502 dprintk("--> %s\n", __func__);
8503
Peng Tao4bd5a982014-11-17 11:05:17 +08008504 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8505 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8506
Idan Kedar85541162012-08-02 11:47:10 +03008507 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8508 if (!lgp->args.layout.pages) {
8509 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008510 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008511 }
8512 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8513
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008514 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008515 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008516 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008517
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008518 task = rpc_run_task(&task_setup_data);
8519 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008520 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008521 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008522 if (status == 0) {
8523 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8524 *timeout = exception.timeout;
8525 }
8526
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008527 trace_nfs4_layoutget(lgp->args.ctx,
8528 &lgp->args.range,
8529 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008530 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008531 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008532
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008533 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8534 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008535 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008536 rpc_put_task(task);
8537 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008538 if (status)
8539 return ERR_PTR(status);
8540 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008541}
8542
Benny Halevycbe82602011-05-22 19:52:37 +03008543static void
8544nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8545{
8546 struct nfs4_layoutreturn *lrp = calldata;
8547
8548 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008549 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008550 &lrp->args.seq_args,
8551 &lrp->res.seq_res,
8552 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008553}
8554
8555static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8556{
8557 struct nfs4_layoutreturn *lrp = calldata;
8558 struct nfs_server *server;
8559
8560 dprintk("--> %s\n", __func__);
8561
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008562 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008563 return;
8564
8565 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008566 switch (task->tk_status) {
8567 default:
8568 task->tk_status = 0;
8569 case 0:
8570 break;
8571 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008572 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008573 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008574 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008575 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008576 return;
8577 }
Benny Halevycbe82602011-05-22 19:52:37 +03008578 dprintk("<-- %s\n", __func__);
8579}
8580
8581static void nfs4_layoutreturn_release(void *calldata)
8582{
8583 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008584 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008585
8586 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008587 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008588 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008589 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008590 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8591 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008592 pnfs_put_layout_hdr(lrp->args.layout);
8593 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008594 kfree(calldata);
8595 dprintk("<-- %s\n", __func__);
8596}
8597
8598static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8599 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8600 .rpc_call_done = nfs4_layoutreturn_done,
8601 .rpc_release = nfs4_layoutreturn_release,
8602};
8603
Peng Tao6c166052014-11-17 09:30:40 +08008604int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008605{
8606 struct rpc_task *task;
8607 struct rpc_message msg = {
8608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8609 .rpc_argp = &lrp->args,
8610 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008611 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008612 };
8613 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008614 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008615 .rpc_message = &msg,
8616 .callback_ops = &nfs4_layoutreturn_call_ops,
8617 .callback_data = lrp,
8618 };
Peng Tao6c166052014-11-17 09:30:40 +08008619 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008620
Andrew Elble99ade3c2015-12-02 09:39:51 -05008621 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8622 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8623 &task_setup_data.rpc_client, &msg);
8624
Benny Halevycbe82602011-05-22 19:52:37 +03008625 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008626 if (!sync) {
8627 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8628 if (!lrp->inode) {
8629 nfs4_layoutreturn_release(lrp);
8630 return -EAGAIN;
8631 }
8632 task_setup_data.flags |= RPC_TASK_ASYNC;
8633 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008634 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008635 task = rpc_run_task(&task_setup_data);
8636 if (IS_ERR(task))
8637 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008638 if (sync)
8639 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008640 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008641 dprintk("<-- %s status=%d\n", __func__, status);
8642 rpc_put_task(task);
8643 return status;
8644}
8645
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008646static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008647_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8648 struct pnfs_device *pdev,
8649 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008650{
8651 struct nfs4_getdeviceinfo_args args = {
8652 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008653 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8654 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008655 };
8656 struct nfs4_getdeviceinfo_res res = {
8657 .pdev = pdev,
8658 };
8659 struct rpc_message msg = {
8660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8661 .rpc_argp = &args,
8662 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008663 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008664 };
8665 int status;
8666
8667 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008668 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008669 if (res.notification & ~args.notify_types)
8670 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008671 if (res.notification != args.notify_types)
8672 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008673
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008674 dprintk("<-- %s status=%d\n", __func__, status);
8675
8676 return status;
8677}
8678
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008679int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8680 struct pnfs_device *pdev,
8681 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008682{
8683 struct nfs4_exception exception = { };
8684 int err;
8685
8686 do {
8687 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008688 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008689 &exception);
8690 } while (exception.retry);
8691 return err;
8692}
8693EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8694
Andy Adamson863a3c62011-03-23 13:27:54 +00008695static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8696{
8697 struct nfs4_layoutcommit_data *data = calldata;
8698 struct nfs_server *server = NFS_SERVER(data->args.inode);
8699
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008700 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008701 &data->args.seq_args,
8702 &data->res.seq_res,
8703 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008704}
8705
8706static void
8707nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8708{
8709 struct nfs4_layoutcommit_data *data = calldata;
8710 struct nfs_server *server = NFS_SERVER(data->args.inode);
8711
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008712 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008713 return;
8714
8715 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008716 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8717 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8718 case -NFS4ERR_BADLAYOUT: /* no layout */
8719 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008720 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008721 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008722 break;
8723 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008724 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008725 rpc_restart_call_prepare(task);
8726 return;
8727 }
8728 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008729}
8730
8731static void nfs4_layoutcommit_release(void *calldata)
8732{
8733 struct nfs4_layoutcommit_data *data = calldata;
8734
Andy Adamsondb29c082011-07-30 20:52:38 -04008735 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008736 nfs_post_op_update_inode_force_wcc(data->args.inode,
8737 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008738 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008739 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008740 kfree(data);
8741}
8742
8743static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8744 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8745 .rpc_call_done = nfs4_layoutcommit_done,
8746 .rpc_release = nfs4_layoutcommit_release,
8747};
8748
8749int
Andy Adamsonef311532011-03-12 02:58:10 -05008750nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008751{
8752 struct rpc_message msg = {
8753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8754 .rpc_argp = &data->args,
8755 .rpc_resp = &data->res,
8756 .rpc_cred = data->cred,
8757 };
8758 struct rpc_task_setup task_setup_data = {
8759 .task = &data->task,
8760 .rpc_client = NFS_CLIENT(data->args.inode),
8761 .rpc_message = &msg,
8762 .callback_ops = &nfs4_layoutcommit_ops,
8763 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008764 };
8765 struct rpc_task *task;
8766 int status = 0;
8767
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008768 dprintk("NFS: initiating layoutcommit call. sync %d "
8769 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008770 data->args.lastbytewritten,
8771 data->args.inode->i_ino);
8772
Trond Myklebust472e2592015-02-05 16:50:30 -05008773 if (!sync) {
8774 data->inode = nfs_igrab_and_active(data->args.inode);
8775 if (data->inode == NULL) {
8776 nfs4_layoutcommit_release(data);
8777 return -EAGAIN;
8778 }
8779 task_setup_data.flags = RPC_TASK_ASYNC;
8780 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008781 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008782 task = rpc_run_task(&task_setup_data);
8783 if (IS_ERR(task))
8784 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008785 if (sync)
8786 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008787 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008788 dprintk("%s: status %d\n", __func__, status);
8789 rpc_put_task(task);
8790 return status;
8791}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008792
Andy Adamson97431202013-08-08 10:57:56 -04008793/**
8794 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8795 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8796 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008797static int
8798_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008799 struct nfs_fsinfo *info,
8800 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008801{
8802 struct nfs41_secinfo_no_name_args args = {
8803 .style = SECINFO_STYLE_CURRENT_FH,
8804 };
8805 struct nfs4_secinfo_res res = {
8806 .flavors = flavors,
8807 };
8808 struct rpc_message msg = {
8809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8810 .rpc_argp = &args,
8811 .rpc_resp = &res,
8812 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008813 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008814 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008815 int status;
8816
8817 if (use_integrity) {
8818 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008819 cred = nfs4_get_clid_cred(server->nfs_client);
8820 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008821 }
8822
8823 dprintk("--> %s\n", __func__);
8824 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8825 &res.seq_res, 0);
8826 dprintk("<-- %s status=%d\n", __func__, status);
8827
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008828 if (cred)
8829 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008830
8831 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008832}
8833
8834static int
8835nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8836 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8837{
8838 struct nfs4_exception exception = { };
8839 int err;
8840 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008841 /* first try using integrity protection */
8842 err = -NFS4ERR_WRONGSEC;
8843
8844 /* try to use integrity protection with machine cred */
8845 if (_nfs4_is_integrity_protected(server->nfs_client))
8846 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8847 flavors, true);
8848
8849 /*
8850 * if unable to use integrity protection, or SECINFO with
8851 * integrity protection returns NFS4ERR_WRONGSEC (which is
8852 * disallowed by spec, but exists in deployed servers) use
8853 * the current filesystem's rpc_client and the user cred.
8854 */
8855 if (err == -NFS4ERR_WRONGSEC)
8856 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8857 flavors, false);
8858
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008859 switch (err) {
8860 case 0:
8861 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008862 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008863 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008864 default:
8865 err = nfs4_handle_exception(server, err, &exception);
8866 }
8867 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008868out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008869 return err;
8870}
8871
8872static int
8873nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8874 struct nfs_fsinfo *info)
8875{
8876 int err;
8877 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008878 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008879 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008880 struct nfs4_secinfo4 *secinfo;
8881 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008882
8883 page = alloc_page(GFP_KERNEL);
8884 if (!page) {
8885 err = -ENOMEM;
8886 goto out;
8887 }
8888
8889 flavors = page_address(page);
8890 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8891
8892 /*
8893 * Fall back on "guess and check" method if
8894 * the server doesn't support SECINFO_NO_NAME
8895 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008896 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008897 err = nfs4_find_root_sec(server, fhandle, info);
8898 goto out_freepage;
8899 }
8900 if (err)
8901 goto out_freepage;
8902
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008903 for (i = 0; i < flavors->num_flavors; i++) {
8904 secinfo = &flavors->flavors[i];
8905
8906 switch (secinfo->flavor) {
8907 case RPC_AUTH_NULL:
8908 case RPC_AUTH_UNIX:
8909 case RPC_AUTH_GSS:
8910 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8911 &secinfo->flavor_info);
8912 break;
8913 default:
8914 flavor = RPC_AUTH_MAXFLAVOR;
8915 break;
8916 }
8917
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008918 if (!nfs_auth_info_match(&server->auth_info, flavor))
8919 flavor = RPC_AUTH_MAXFLAVOR;
8920
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008921 if (flavor != RPC_AUTH_MAXFLAVOR) {
8922 err = nfs4_lookup_root_sec(server, fhandle,
8923 info, flavor);
8924 if (!err)
8925 break;
8926 }
8927 }
8928
8929 if (flavor == RPC_AUTH_MAXFLAVOR)
8930 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008931
8932out_freepage:
8933 put_page(page);
8934 if (err == -EACCES)
8935 return -EPERM;
8936out:
8937 return err;
8938}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008939
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008940static int _nfs41_test_stateid(struct nfs_server *server,
8941 nfs4_stateid *stateid,
8942 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008943{
8944 int status;
8945 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008946 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008947 };
8948 struct nfs41_test_stateid_res res;
8949 struct rpc_message msg = {
8950 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8951 .rpc_argp = &args,
8952 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008953 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008954 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008955 struct rpc_clnt *rpc_client = server->client;
8956
8957 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8958 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008959
Chuck Lever38527b12012-07-11 16:30:23 -04008960 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008961 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008962 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008963 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008964 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008965 if (status != NFS_OK) {
8966 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008967 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008968 }
8969 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008970 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008971}
8972
Trond Myklebust43912bb2016-09-22 13:38:56 -04008973static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8974 int err, struct nfs4_exception *exception)
8975{
8976 exception->retry = 0;
8977 switch(err) {
8978 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008979 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008980 nfs4_handle_exception(server, err, exception);
8981 break;
8982 case -NFS4ERR_BADSESSION:
8983 case -NFS4ERR_BADSLOT:
8984 case -NFS4ERR_BAD_HIGH_SLOT:
8985 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8986 case -NFS4ERR_DEADSESSION:
8987 nfs4_do_handle_exception(server, err, exception);
8988 }
8989}
8990
Chuck Lever38527b12012-07-11 16:30:23 -04008991/**
8992 * nfs41_test_stateid - perform a TEST_STATEID operation
8993 *
8994 * @server: server / transport on which to perform the operation
8995 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008996 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008997 *
8998 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8999 * Otherwise a negative NFS4ERR value is returned if the operation
9000 * failed or the state ID is not currently valid.
9001 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009002static int nfs41_test_stateid(struct nfs_server *server,
9003 nfs4_stateid *stateid,
9004 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009005{
9006 struct nfs4_exception exception = { };
9007 int err;
9008 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009009 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009010 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009011 } while (exception.retry);
9012 return err;
9013}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009014
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009015struct nfs_free_stateid_data {
9016 struct nfs_server *server;
9017 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009018 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009019};
9020
9021static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9022{
9023 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009024 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009025 &data->args.seq_args,
9026 &data->res.seq_res,
9027 task);
9028}
9029
9030static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9031{
9032 struct nfs_free_stateid_data *data = calldata;
9033
9034 nfs41_sequence_done(task, &data->res.seq_res);
9035
9036 switch (task->tk_status) {
9037 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009038 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009039 rpc_restart_call_prepare(task);
9040 }
9041}
9042
9043static void nfs41_free_stateid_release(void *calldata)
9044{
9045 kfree(calldata);
9046}
9047
Trond Myklebust17f26b12013-08-21 15:48:42 -04009048static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009049 .rpc_call_prepare = nfs41_free_stateid_prepare,
9050 .rpc_call_done = nfs41_free_stateid_done,
9051 .rpc_release = nfs41_free_stateid_release,
9052};
9053
9054static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009055 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009056 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009057 bool privileged)
9058{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009059 struct rpc_message msg = {
9060 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009061 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009062 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009063 struct rpc_task_setup task_setup = {
9064 .rpc_client = server->client,
9065 .rpc_message = &msg,
9066 .callback_ops = &nfs41_free_stateid_ops,
9067 .flags = RPC_TASK_ASYNC,
9068 };
9069 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009070
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009071 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9072 &task_setup.rpc_client, &msg);
9073
Chuck Lever38527b12012-07-11 16:30:23 -04009074 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009075 data = kmalloc(sizeof(*data), GFP_NOFS);
9076 if (!data)
9077 return ERR_PTR(-ENOMEM);
9078 data->server = server;
9079 nfs4_stateid_copy(&data->args.stateid, stateid);
9080
9081 task_setup.callback_data = data;
9082
9083 msg.rpc_argp = &data->args;
9084 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009085 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009086 if (privileged)
9087 nfs4_set_sequence_privileged(&data->args.seq_args);
9088
9089 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009090}
9091
Chuck Lever38527b12012-07-11 16:30:23 -04009092/**
9093 * nfs41_free_stateid - perform a FREE_STATEID operation
9094 *
9095 * @server: server / transport on which to perform the operation
9096 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009097 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009098 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009099 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009100 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009101 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009102static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009103 const nfs4_stateid *stateid,
9104 struct rpc_cred *cred,
9105 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009106{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009107 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009108
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009109 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009110 if (IS_ERR(task))
9111 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009112 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009113 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009114}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009115
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009116static void
9117nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009118{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009119 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009120
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009121 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009122 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009123}
9124
Trond Myklebust36281ca2012-03-04 18:13:56 -05009125static bool nfs41_match_stateid(const nfs4_stateid *s1,
9126 const nfs4_stateid *s2)
9127{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009128 if (s1->type != s2->type)
9129 return false;
9130
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009131 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009132 return false;
9133
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009134 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009135 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009136
Anna Schumaker045c5512017-01-11 16:59:48 -05009137 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009138}
9139
Andy Adamson557134a2009-04-01 09:21:53 -04009140#endif /* CONFIG_NFS_V4_1 */
9141
Trond Myklebust36281ca2012-03-04 18:13:56 -05009142static bool nfs4_match_stateid(const nfs4_stateid *s1,
9143 const nfs4_stateid *s2)
9144{
Trond Myklebustf597c532012-03-04 18:13:56 -05009145 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009146}
9147
9148
Trond Myklebust17280172012-03-11 13:11:00 -04009149static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009150 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009151 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009152 .recover_open = nfs4_open_reclaim,
9153 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009154 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009155 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009156};
9157
Andy Adamson591d71c2009-04-01 09:22:47 -04009158#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009159static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009160 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9161 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9162 .recover_open = nfs4_open_reclaim,
9163 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009164 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009165 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009166 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009167};
9168#endif /* CONFIG_NFS_V4_1 */
9169
Trond Myklebust17280172012-03-11 13:11:00 -04009170static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009171 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009172 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009173 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009174 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009175 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009176};
9177
Andy Adamson591d71c2009-04-01 09:22:47 -04009178#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009179static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009180 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9181 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009182 .recover_open = nfs41_open_expired,
9183 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009184 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009185};
9186#endif /* CONFIG_NFS_V4_1 */
9187
Trond Myklebust17280172012-03-11 13:11:00 -04009188static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009189 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009190 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009191 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009192};
9193
9194#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009195static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009196 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009197 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009198 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009199};
9200#endif
9201
Chuck Leverec011fe2013-10-17 14:12:39 -04009202static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009203 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009204 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009205};
9206
9207#if defined(CONFIG_NFS_V4_1)
9208static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009209 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009210 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009211};
9212#endif /* CONFIG_NFS_V4_1 */
9213
Trond Myklebust97dc1352010-06-16 09:52:26 -04009214static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9215 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009216 .init_caps = NFS_CAP_READDIRPLUS
9217 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009218 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009219 .init_client = nfs40_init_client,
9220 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009221 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009222 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009223 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009224 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009225 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009226 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009227 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9228 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9229 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009230 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009231};
9232
9233#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009234static struct nfs_seqid *
9235nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9236{
9237 return NULL;
9238}
9239
Trond Myklebust97dc1352010-06-16 09:52:26 -04009240static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9241 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009242 .init_caps = NFS_CAP_READDIRPLUS
9243 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009244 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009245 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009246 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009247 .init_client = nfs41_init_client,
9248 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009249 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009250 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009251 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009252 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009253 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009254 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009255 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009256 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9257 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9258 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009259 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009260};
9261#endif
9262
Steve Dickson42c2c422013-05-22 12:50:38 -04009263#if defined(CONFIG_NFS_V4_2)
9264static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9265 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009266 .init_caps = NFS_CAP_READDIRPLUS
9267 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009268 | NFS_CAP_POSIX_LOCK
9269 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009270 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009271 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009272 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009273 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009274 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009275 | NFS_CAP_LAYOUTSTATS
9276 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009277 .init_client = nfs41_init_client,
9278 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009279 .match_stateid = nfs41_match_stateid,
9280 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009281 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009282 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009283 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009284 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009285 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009286 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9287 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9288 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009289 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009290};
9291#endif
9292
Trond Myklebust97dc1352010-06-16 09:52:26 -04009293const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9294 [0] = &nfs_v4_0_minor_ops,
9295#if defined(CONFIG_NFS_V4_1)
9296 [1] = &nfs_v4_1_minor_ops,
9297#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009298#if defined(CONFIG_NFS_V4_2)
9299 [2] = &nfs_v4_2_minor_ops,
9300#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009301};
9302
Trond Myklebust13997822016-07-24 17:10:52 -04009303static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009304{
9305 ssize_t error, error2;
9306
9307 error = generic_listxattr(dentry, list, size);
9308 if (error < 0)
9309 return error;
9310 if (list) {
9311 list += error;
9312 size -= error;
9313 }
9314
9315 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9316 if (error2 < 0)
9317 return error2;
9318 return error + error2;
9319}
9320
Trond Myklebust17f26b12013-08-21 15:48:42 -04009321static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009322 .create = nfs_create,
9323 .lookup = nfs_lookup,
9324 .atomic_open = nfs_atomic_open,
9325 .link = nfs_link,
9326 .unlink = nfs_unlink,
9327 .symlink = nfs_symlink,
9328 .mkdir = nfs_mkdir,
9329 .rmdir = nfs_rmdir,
9330 .mknod = nfs_mknod,
9331 .rename = nfs_rename,
9332 .permission = nfs_permission,
9333 .getattr = nfs_getattr,
9334 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009335 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009336};
9337
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009338static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009339 .permission = nfs_permission,
9340 .getattr = nfs_getattr,
9341 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009342 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009343};
9344
David Howells509de812006-08-22 20:06:11 -04009345const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009346 .version = 4, /* protocol version */
9347 .dentry_ops = &nfs4_dentry_operations,
9348 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009349 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009350 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009351 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009352 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009353 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009354 .getattr = nfs4_proc_getattr,
9355 .setattr = nfs4_proc_setattr,
9356 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009357 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009358 .access = nfs4_proc_access,
9359 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009360 .create = nfs4_proc_create,
9361 .remove = nfs4_proc_remove,
9362 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009363 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009364 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009365 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009366 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009367 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009368 .link = nfs4_proc_link,
9369 .symlink = nfs4_proc_symlink,
9370 .mkdir = nfs4_proc_mkdir,
9371 .rmdir = nfs4_proc_remove,
9372 .readdir = nfs4_proc_readdir,
9373 .mknod = nfs4_proc_mknod,
9374 .statfs = nfs4_proc_statfs,
9375 .fsinfo = nfs4_proc_fsinfo,
9376 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009377 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009378 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009379 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009380 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009381 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009382 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009383 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009384 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009385 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009386 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009387 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009388 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009389 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009390 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009391 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009392 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009393 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009394 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009395 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009396 .create_server = nfs4_create_server,
9397 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009398};
9399
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009400static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009401 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009402 .list = nfs4_xattr_list_nfs4_acl,
9403 .get = nfs4_xattr_get_nfs4_acl,
9404 .set = nfs4_xattr_set_nfs4_acl,
9405};
9406
9407const struct xattr_handler *nfs4_xattr_handlers[] = {
9408 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009409#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9410 &nfs4_xattr_nfs4_label_handler,
9411#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009412 NULL
9413};
9414
Linus Torvalds1da177e2005-04-16 15:20:36 -07009415/*
9416 * Local variables:
9417 * c-basic-offset: 8
9418 * End:
9419 */