blob: f90090e8c959b14346baf76ac7eaefed7ae30f82 [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
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001662static struct inode *
1663nfs4_opendata_get_inode(struct nfs4_opendata *data)
1664{
1665 struct inode *inode;
1666
1667 switch (data->o_arg.claim) {
1668 case NFS4_OPEN_CLAIM_NULL:
1669 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1670 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1671 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1672 return ERR_PTR(-EAGAIN);
1673 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1674 &data->f_attr, data->f_label);
1675 break;
1676 default:
1677 inode = d_inode(data->dentry);
1678 ihold(inode);
1679 nfs_refresh_inode(inode, &data->f_attr);
1680 }
1681 return inode;
1682}
1683
Andy Adamsone23008e2012-10-02 21:07:32 -04001684static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001685nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1686{
1687 struct nfs4_state *state;
1688 struct inode *inode;
1689
1690 inode = nfs4_opendata_get_inode(data);
1691 if (IS_ERR(inode))
1692 return ERR_CAST(inode);
1693 if (data->state != NULL && data->state->inode == inode) {
1694 state = data->state;
1695 atomic_inc(&state->count);
1696 } else
1697 state = nfs4_get_open_state(inode, data->owner);
1698 iput(inode);
1699 if (state == NULL)
1700 state = ERR_PTR(-ENOMEM);
1701 return state;
1702}
1703
Andy Adamsone23008e2012-10-02 21:07:32 -04001704static struct nfs4_state *
1705_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001706{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001707 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001708
Trond Myklebustaac00a82007-07-05 19:02:21 -04001709 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001710 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001711 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001712 goto out;
1713 }
1714
Trond Myklebust75e8c482017-08-08 10:38:07 -04001715 state = nfs4_opendata_find_nfs4_state(data);
1716 if (IS_ERR(state))
1717 goto out;
1718
Andy Adamsone23008e2012-10-02 21:07:32 -04001719 if (data->o_res.delegation_type != 0)
1720 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001721 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001722 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001723out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001724 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001725 return state;
1726}
1727
Andy Adamsone23008e2012-10-02 21:07:32 -04001728static struct nfs4_state *
1729nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1730{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001731 struct nfs4_state *ret;
1732
Andy Adamsone23008e2012-10-02 21:07:32 -04001733 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001734 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1735 else
1736 ret = _nfs4_opendata_to_nfs4_state(data);
1737 nfs4_sequence_free_slot(&data->o_res.seq_res);
1738 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001739}
1740
Trond Myklebust864472e2006-01-03 09:55:15 +01001741static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1742{
1743 struct nfs_inode *nfsi = NFS_I(state->inode);
1744 struct nfs_open_context *ctx;
1745
1746 spin_lock(&state->inode->i_lock);
1747 list_for_each_entry(ctx, &nfsi->open_files, list) {
1748 if (ctx->state != state)
1749 continue;
1750 get_nfs_open_context(ctx);
1751 spin_unlock(&state->inode->i_lock);
1752 return ctx;
1753 }
1754 spin_unlock(&state->inode->i_lock);
1755 return ERR_PTR(-ENOENT);
1756}
1757
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001758static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1759 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001760{
1761 struct nfs4_opendata *opendata;
1762
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001763 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001764 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001765 if (opendata == NULL)
1766 return ERR_PTR(-ENOMEM);
1767 opendata->state = state;
1768 atomic_inc(&state->count);
1769 return opendata;
1770}
1771
Trond Myklebust24311f82015-09-20 10:50:17 -04001772static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1773 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001774{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001775 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001776 int ret;
1777
Trond Myklebust24311f82015-09-20 10:50:17 -04001778 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001779 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001780 opendata->o_arg.open_flags = 0;
1781 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001782 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1783 NFS_SB(opendata->dentry->d_sb),
1784 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001785 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1786 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1787 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001788 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001789 if (ret != 0)
1790 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001791 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001792 if (IS_ERR(newstate))
1793 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001794 if (newstate != opendata->state)
1795 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001796 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001797 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001798}
1799
1800static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1801{
Trond Myklebust864472e2006-01-03 09:55:15 +01001802 int ret;
1803
Trond Myklebust4f14c192014-02-12 19:15:06 -05001804 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1805 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1806 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1807 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001808 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001809 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001810 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001811 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001812 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1813 if (ret != 0)
1814 return ret;
1815 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1816 if (ret != 0)
1817 return ret;
1818 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1819 if (ret != 0)
1820 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001821 /*
1822 * We may have performed cached opens for all three recoveries.
1823 * Check if we need to update the current stateid.
1824 */
1825 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001826 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001827 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001828 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001829 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001830 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001831 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001832 return 0;
1833}
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835/*
1836 * OPEN_RECLAIM:
1837 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001839static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001841 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001842 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001843 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 int status;
1845
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001846 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1847 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001848 if (IS_ERR(opendata))
1849 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001850 rcu_read_lock();
1851 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001852 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001853 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001854 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001855 opendata->o_arg.u.delegation_type = delegation_type;
1856 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001857 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 return status;
1859}
1860
Trond Myklebust539cd032007-06-05 11:46:42 -04001861static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
1863 struct nfs_server *server = NFS_SERVER(state->inode);
1864 struct nfs4_exception exception = { };
1865 int err;
1866 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001867 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001868 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001869 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1870 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001871 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001872 break;
1873 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 } while (exception.retry);
1875 return err;
1876}
1877
Trond Myklebust864472e2006-01-03 09:55:15 +01001878static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1879{
1880 struct nfs_open_context *ctx;
1881 int ret;
1882
1883 ctx = nfs4_state_find_open_context(state);
1884 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001885 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001886 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001887 put_nfs_open_context(ctx);
1888 return ret;
1889}
1890
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001891static 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 -07001892{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001893 switch (err) {
1894 default:
1895 printk(KERN_ERR "NFS: %s: unhandled error "
1896 "%d.\n", __func__, err);
1897 case 0:
1898 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001899 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001900 case -ESTALE:
1901 break;
1902 case -NFS4ERR_BADSESSION:
1903 case -NFS4ERR_BADSLOT:
1904 case -NFS4ERR_BAD_HIGH_SLOT:
1905 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1906 case -NFS4ERR_DEADSESSION:
1907 set_bit(NFS_DELEGATED_STATE, &state->flags);
1908 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1909 return -EAGAIN;
1910 case -NFS4ERR_STALE_CLIENTID:
1911 case -NFS4ERR_STALE_STATEID:
1912 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001913 /* Don't recall a delegation if it was lost */
1914 nfs4_schedule_lease_recovery(server->nfs_client);
1915 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001916 case -NFS4ERR_MOVED:
1917 nfs4_schedule_migration_recovery(server);
1918 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001919 case -NFS4ERR_LEASE_MOVED:
1920 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1921 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001922 case -NFS4ERR_DELEG_REVOKED:
1923 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001924 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001925 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001926 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001927 nfs_inode_find_state_and_recover(state->inode,
1928 stateid);
1929 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001930 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001931 case -NFS4ERR_DELAY:
1932 case -NFS4ERR_GRACE:
1933 set_bit(NFS_DELEGATED_STATE, &state->flags);
1934 ssleep(1);
1935 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001936 case -ENOMEM:
1937 case -NFS4ERR_DENIED:
1938 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1939 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return err;
1942}
1943
Trond Myklebust24311f82015-09-20 10:50:17 -04001944int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1945 struct nfs4_state *state, const nfs4_stateid *stateid,
1946 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001947{
1948 struct nfs_server *server = NFS_SERVER(state->inode);
1949 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001950 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001951
1952 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1953 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1954 if (IS_ERR(opendata))
1955 return PTR_ERR(opendata);
1956 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001957 write_seqlock(&state->seqlock);
1958 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1959 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001960 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1961 switch (type & (FMODE_READ|FMODE_WRITE)) {
1962 case FMODE_READ|FMODE_WRITE:
1963 case FMODE_WRITE:
1964 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1965 if (err)
1966 break;
1967 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1968 if (err)
1969 break;
1970 case FMODE_READ:
1971 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1972 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001973 nfs4_opendata_put(opendata);
1974 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1975}
1976
Chuck Leverbe05c862013-08-09 12:49:47 -04001977static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1978{
1979 struct nfs4_opendata *data = calldata;
1980
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001981 nfs4_setup_sequence(data->o_arg.server->nfs_client,
1982 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001983}
1984
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001985static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1986{
1987 struct nfs4_opendata *data = calldata;
1988
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001989 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001990
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001991 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001992 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001993 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001994 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001995 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001996 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001997 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001998}
1999
2000static void nfs4_open_confirm_release(void *calldata)
2001{
2002 struct nfs4_opendata *data = calldata;
2003 struct nfs4_state *state = NULL;
2004
2005 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002006 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002007 goto out_free;
2008 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002009 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002010 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002011 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002012 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002013 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002014out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002015 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002016}
2017
2018static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002019 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002020 .rpc_call_done = nfs4_open_confirm_done,
2021 .rpc_release = nfs4_open_confirm_release,
2022};
2023
2024/*
2025 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2026 */
2027static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2028{
David Howells2b0143b2015-03-17 22:25:59 +00002029 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002030 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002031 struct rpc_message msg = {
2032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2033 .rpc_argp = &data->c_arg,
2034 .rpc_resp = &data->c_res,
2035 .rpc_cred = data->owner->so_cred,
2036 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002037 struct rpc_task_setup task_setup_data = {
2038 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002039 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002040 .callback_ops = &nfs4_open_confirm_ops,
2041 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002042 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002043 .flags = RPC_TASK_ASYNC,
2044 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 int status;
2046
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002047 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002048 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002049 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002050 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002051 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002052 if (data->is_recover)
2053 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002054 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002055 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002056 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002057 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002058 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002059 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002060 smp_wmb();
2061 } else
2062 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002063 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return status;
2065}
2066
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002067static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002069 struct nfs4_opendata *data = calldata;
2070 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002071 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002072 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002073
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002074 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002075 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002076 /*
2077 * Check if we still need to send an OPEN call, or if we can use
2078 * a delegation instead.
2079 */
2080 if (data->state != NULL) {
2081 struct nfs_delegation *delegation;
2082
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002083 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002084 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002085 rcu_read_lock();
2086 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002087 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002088 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002089 rcu_read_unlock();
2090 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002091 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002092 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002093 switch (claim) {
2094 default:
2095 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002096 case NFS4_OPEN_CLAIM_PREVIOUS:
2097 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2098 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002099 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002100 case NFS4_OPEN_CLAIM_FH:
2101 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002102 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002103 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002104 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002105 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002106 &data->o_res.seq_res,
2107 task) != 0)
2108 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002109
2110 /* Set the create mode (note dependency on the session type) */
2111 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2112 if (data->o_arg.open_flags & O_EXCL) {
2113 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2114 if (nfs4_has_persistent_session(clp))
2115 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2116 else if (clp->cl_mvops->minor_version > 0)
2117 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2118 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002119 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002120unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002121 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002122 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002123out_no_action:
2124 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002125out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002126 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002127}
2128
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002129static void nfs4_open_done(struct rpc_task *task, void *calldata)
2130{
2131 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002133 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002134
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002135 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002136 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002137
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002138 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002139 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2140 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002141 case S_IFREG:
2142 break;
2143 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002144 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002145 break;
2146 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002147 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002148 break;
2149 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002150 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002151 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002152 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002153 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002154 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2155 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002156 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002157 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002158}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002159
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002160static void nfs4_open_release(void *calldata)
2161{
2162 struct nfs4_opendata *data = calldata;
2163 struct nfs4_state *state = NULL;
2164
2165 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002166 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002167 goto out_free;
2168 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002169 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002170 goto out_free;
2171 /* In case we need an open_confirm, no cleanup! */
2172 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2173 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002174 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002175 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002176 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002177out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002178 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002179}
2180
2181static const struct rpc_call_ops nfs4_open_ops = {
2182 .rpc_call_prepare = nfs4_open_prepare,
2183 .rpc_call_done = nfs4_open_done,
2184 .rpc_release = nfs4_open_release,
2185};
2186
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002187static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002188{
David Howells2b0143b2015-03-17 22:25:59 +00002189 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002190 struct nfs_server *server = NFS_SERVER(dir);
2191 struct nfs_openargs *o_arg = &data->o_arg;
2192 struct nfs_openres *o_res = &data->o_res;
2193 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002194 struct rpc_message msg = {
2195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2196 .rpc_argp = o_arg,
2197 .rpc_resp = o_res,
2198 .rpc_cred = data->owner->so_cred,
2199 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002200 struct rpc_task_setup task_setup_data = {
2201 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002202 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002203 .callback_ops = &nfs4_open_ops,
2204 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002205 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002206 .flags = RPC_TASK_ASYNC,
2207 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002208 int status;
2209
Chuck Levera9c92d62013-08-09 12:48:18 -04002210 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002211 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002212 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002213 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002214 data->cancelled = false;
2215 data->is_recover = false;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002216 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002217 nfs4_set_sequence_privileged(&o_arg->seq_args);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002218 data->is_recover = true;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002219 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002220 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002221 if (IS_ERR(task))
2222 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002223 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002224 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002225 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002226 smp_wmb();
2227 } else
2228 status = data->rpc_status;
2229 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002230
2231 return status;
2232}
2233
2234static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2235{
David Howells2b0143b2015-03-17 22:25:59 +00002236 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002237 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002238 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002239
2240 status = nfs4_run_open_task(data, 1);
2241 if (status != 0 || !data->rpc_done)
2242 return status;
2243
Trond Myklebust6926afd12012-01-07 13:22:46 -05002244 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2245
Anna Schumakerd7e98252017-01-11 16:13:29 -05002246 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002247 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002248
2249 return status;
2250}
2251
Trond Myklebustf3792d62014-07-10 08:54:32 -04002252/*
2253 * Additional permission checks in order to distinguish between an
2254 * open for read, and an open for execute. This works around the
2255 * fact that NFSv4 OPEN treats read and execute permissions as being
2256 * the same.
2257 * Note that in the non-execute case, we want to turn off permission
2258 * checking if we just created a new file (POSIX open() semantics).
2259 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002260static int nfs4_opendata_access(struct rpc_cred *cred,
2261 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002262 struct nfs4_state *state, fmode_t fmode,
2263 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002264{
2265 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002266 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002267
2268 /* access call failed or for some reason the server doesn't
2269 * support any access modes -- defer access call until later */
2270 if (opendata->o_res.access_supported == 0)
2271 return 0;
2272
2273 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002274 /*
2275 * Use openflags to check for exec, because fmode won't
2276 * always have FMODE_EXEC set when file open for exec.
2277 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002278 if (openflags & __FMODE_EXEC) {
2279 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002280 if (S_ISDIR(state->inode->i_mode))
2281 mask = NFS4_ACCESS_LOOKUP;
2282 else
2283 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002284 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002285 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002286
2287 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002288 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2289 nfs_access_add_cache(state->inode, &cache);
2290
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002291 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2292 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002293 return 0;
2294
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002295 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002296}
2297
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002298/*
2299 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2300 */
2301static int _nfs4_proc_open(struct nfs4_opendata *data)
2302{
David Howells2b0143b2015-03-17 22:25:59 +00002303 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002304 struct nfs_server *server = NFS_SERVER(dir);
2305 struct nfs_openargs *o_arg = &data->o_arg;
2306 struct nfs_openres *o_res = &data->o_res;
2307 int status;
2308
2309 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002310 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002311 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002312 if (status != 0) {
2313 if (status == -NFS4ERR_BADNAME &&
2314 !(o_arg->open_flags & O_CREAT))
2315 return -ENOENT;
2316 return status;
2317 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002318
Trond Myklebust6926afd12012-01-07 13:22:46 -05002319 nfs_fattr_map_and_free_names(server, &data->f_attr);
2320
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002321 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002322 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002323 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002324 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002325 data->file_created = true;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002326 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002327 update_changeattr(dir, &o_res->cinfo,
2328 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002329 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002330 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2331 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002333 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002335 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002337 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2338 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002339 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002340 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
2343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344/*
2345 * OPEN_EXPIRED:
2346 * reclaim state on the server after a network partition.
2347 * Assumes caller holds the appropriate lock
2348 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002349static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002351 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002352 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002354 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002355 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002356 if (IS_ERR(opendata))
2357 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002358 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002359 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002360 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002361 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002362 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
2364
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002365static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002366{
Trond Myklebust539cd032007-06-05 11:46:42 -04002367 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002368 struct nfs4_exception exception = { };
2369 int err;
2370
2371 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002372 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002373 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002374 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2375 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002376 switch (err) {
2377 default:
2378 goto out;
2379 case -NFS4ERR_GRACE:
2380 case -NFS4ERR_DELAY:
2381 nfs4_handle_exception(server, err, &exception);
2382 err = 0;
2383 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002384 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002385out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002386 return err;
2387}
2388
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2390{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002392 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Trond Myklebust864472e2006-01-03 09:55:15 +01002394 ctx = nfs4_state_find_open_context(state);
2395 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002396 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002397 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002398 put_nfs_open_context(ctx);
2399 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400}
2401
Trond Myklebust41020b62016-09-22 13:38:58 -04002402static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2403 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002404{
Trond Myklebust41020b62016-09-22 13:38:58 -04002405 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002406 write_seqlock(&state->seqlock);
2407 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2408 write_sequnlock(&state->seqlock);
2409 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2410}
2411
2412static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2413{
2414 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002415 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002416}
2417
2418static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2419{
2420 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2421 nfs40_clear_delegation_stateid(state);
2422 return nfs4_open_expired(sp, state);
2423}
2424
Trond Myklebust45870d62016-09-22 13:38:59 -04002425static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2426 nfs4_stateid *stateid,
2427 struct rpc_cred *cred)
2428{
2429 return -NFS4ERR_BAD_STATEID;
2430}
2431
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002432#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002433static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2434 nfs4_stateid *stateid,
2435 struct rpc_cred *cred)
2436{
2437 int status;
2438
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002439 switch (stateid->type) {
2440 default:
2441 break;
2442 case NFS4_INVALID_STATEID_TYPE:
2443 case NFS4_SPECIAL_STATEID_TYPE:
2444 return -NFS4ERR_BAD_STATEID;
2445 case NFS4_REVOKED_STATEID_TYPE:
2446 goto out_free;
2447 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002448
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002449 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002450 switch (status) {
2451 case -NFS4ERR_EXPIRED:
2452 case -NFS4ERR_ADMIN_REVOKED:
2453 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002454 break;
2455 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002456 return status;
2457 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002458out_free:
2459 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002460 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002461 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002462}
2463
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002464static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002465{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002466 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002467 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002468 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002469 struct rpc_cred *cred;
2470 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002471
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002472 /* Get the delegation credential for use by test/free_stateid */
2473 rcu_read_lock();
2474 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002475 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002476 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002477 return;
2478 }
2479
2480 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002481 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2482 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2483 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002484 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002485 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002486 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002487 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002488
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002489 cred = get_rpccred(delegation->cred);
2490 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002491 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002492 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002493 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002494 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002495
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002496 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002497}
2498
2499/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002500 * nfs41_check_expired_locks - possibly free a lock stateid
2501 *
2502 * @state: NFSv4 state for an inode
2503 *
2504 * Returns NFS_OK if recovery for this stateid is now finished.
2505 * Otherwise a negative NFS4ERR value is returned.
2506 */
2507static int nfs41_check_expired_locks(struct nfs4_state *state)
2508{
2509 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002510 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002511 struct nfs_server *server = NFS_SERVER(state->inode);
2512
2513 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2514 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002515
2516 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002517 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2518 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2519 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2520
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002521 atomic_inc(&lsp->ls_count);
2522 spin_unlock(&state->state_lock);
2523
2524 nfs4_put_lock_state(prev);
2525 prev = lsp;
2526
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002527 status = nfs41_test_and_free_expired_stateid(server,
2528 &lsp->ls_stateid,
2529 cred);
2530 trace_nfs4_test_lock_stateid(state, lsp, status);
2531 if (status == -NFS4ERR_EXPIRED ||
2532 status == -NFS4ERR_BAD_STATEID) {
2533 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002534 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002535 if (!recover_lost_locks)
2536 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2537 } else if (status != NFS_OK) {
2538 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002539 nfs4_put_lock_state(prev);
2540 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002541 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002542 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002543 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002544 }
2545 spin_unlock(&state->state_lock);
2546 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002547out:
2548 return ret;
2549}
2550
2551/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002552 * nfs41_check_open_stateid - possibly free an open stateid
2553 *
2554 * @state: NFSv4 state for an inode
2555 *
2556 * Returns NFS_OK if recovery for this stateid is now finished.
2557 * Otherwise a negative NFS4ERR value is returned.
2558 */
2559static int nfs41_check_open_stateid(struct nfs4_state *state)
2560{
2561 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002562 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002563 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002564 int status;
2565
Trond Myklebustb134fc42016-09-22 13:39:16 -04002566 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002567 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2568 if (nfs4_have_delegation(state->inode, state->state))
2569 return NFS_OK;
2570 return -NFS4ERR_OPENMODE;
2571 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002572 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002573 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002574 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002575 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002576 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002577 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2578 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2579 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002580 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002581 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002582 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002583 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002584 if (nfs_open_stateid_recover_openmode(state))
2585 return -NFS4ERR_OPENMODE;
2586 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002587}
2588
2589static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2590{
Chuck Levereb64cf92012-07-11 16:30:05 -04002591 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002592
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002593 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002594 status = nfs41_check_expired_locks(state);
2595 if (status != NFS_OK)
2596 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002597 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002598 if (status != NFS_OK)
2599 status = nfs4_open_expired(sp, state);
2600 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002601}
2602#endif
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002605 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2606 * fields corresponding to attributes that were used to store the verifier.
2607 * Make sure we clobber those fields in the later setattr call
2608 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002609static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2610 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002611{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002612 const u32 *attrset = opendata->o_res.attrset;
2613
2614 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002615 !(sattr->ia_valid & ATTR_ATIME_SET))
2616 sattr->ia_valid |= ATTR_ATIME;
2617
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002618 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002619 !(sattr->ia_valid & ATTR_MTIME_SET))
2620 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002621
2622 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002623 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
Benjamin Coddington501e7a42017-06-02 11:21:34 -04002624 (attrset[1] & FATTR4_WORD1_MODE ||
2625 attrset[2] & FATTR4_WORD2_MODE_UMASK))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002626 sattr->ia_valid &= ~ATTR_MODE;
2627
2628 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2629 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002630}
2631
Trond Myklebustc21443c2013-02-07 14:26:21 -05002632static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2633 fmode_t fmode,
2634 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002635 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002636{
2637 struct nfs4_state_owner *sp = opendata->owner;
2638 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002639 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002640 struct nfs4_state *state;
2641 unsigned int seq;
2642 int ret;
2643
2644 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2645
2646 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002647 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002648 goto out;
2649
2650 state = nfs4_opendata_to_nfs4_state(opendata);
2651 ret = PTR_ERR(state);
2652 if (IS_ERR(state))
2653 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002654 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002655 if (server->caps & NFS_CAP_POSIX_LOCK)
2656 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002657 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2658 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002659
Trond Myklebust275bb302013-05-29 13:11:28 -04002660 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002661 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002662 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002663 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002664 alias = d_exact_alias(dentry, state->inode);
2665 if (!alias)
2666 alias = d_splice_alias(igrab(state->inode), dentry);
2667 /* d_splice_alias() can't fail here - it's a non-directory */
2668 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002669 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002670 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002671 }
2672 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002673 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002674 }
2675
Trond Myklebustc21443c2013-02-07 14:26:21 -05002676 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2677 if (ret != 0)
2678 goto out;
2679
David Howells2b0143b2015-03-17 22:25:59 +00002680 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002681 nfs_inode_attach_open_context(ctx);
2682 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2683 nfs4_schedule_stateid_recovery(server, state);
2684 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002685out:
2686 return ret;
2687}
2688
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002689/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002690 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 */
Andy Adamson82be4172012-05-23 05:02:35 -04002692static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002693 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002694 int flags,
2695 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002696 struct nfs4_label *label,
2697 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698{
2699 struct nfs4_state_owner *sp;
2700 struct nfs4_state *state = NULL;
2701 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002702 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002703 struct dentry *dentry = ctx->dentry;
2704 struct rpc_cred *cred = ctx->cred;
2705 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2706 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002707 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002708 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002709 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
2711 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002713 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2714 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2716 goto out_err;
2717 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002718 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002719 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002720 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002721 if (d_really_is_positive(dentry))
2722 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002723 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002724 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002725 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002726 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002727 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002728 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002729 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
David Quigley14c43f72013-05-22 12:50:43 -04002731 if (label) {
2732 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2733 if (IS_ERR(olabel)) {
2734 status = PTR_ERR(olabel);
2735 goto err_opendata_put;
2736 }
2737 }
2738
Trond Myklebuste911b812014-03-26 13:24:37 -07002739 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2740 if (!opendata->f_attr.mdsthreshold) {
2741 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2742 if (!opendata->f_attr.mdsthreshold)
2743 goto err_free_label;
2744 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002745 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002746 }
David Howells2b0143b2015-03-17 22:25:59 +00002747 if (d_really_is_positive(dentry))
2748 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002749
Trond Myklebust3efb9722013-05-29 13:17:04 -04002750 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002751 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002752 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002753 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002754
NeilBrownefcbc042015-07-30 13:00:56 +10002755 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002756 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002757 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002758 /*
2759 * send create attributes which was not set by open
2760 * with an extra setattr.
2761 */
2762 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2763 nfs_fattr_init(opendata->o_res.f_attr);
2764 status = nfs4_do_setattr(state->inode, cred,
2765 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002766 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002767 if (status == 0) {
2768 nfs_setattr_update_inode(state->inode, sattr,
2769 opendata->o_res.f_attr);
2770 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2771 }
David Quigley1775fd32013-05-22 12:50:42 -04002772 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002773 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002774 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002775 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002776
Trond Myklebuste911b812014-03-26 13:24:37 -07002777 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002778 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002779 opendata->f_attr.mdsthreshold = NULL;
2780 }
Andy Adamson82be4172012-05-23 05:02:35 -04002781
David Quigley14c43f72013-05-22 12:50:43 -04002782 nfs4_label_free(olabel);
2783
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002784 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002787err_free_label:
2788 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002789err_opendata_put:
2790 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002791err_put_state_owner:
2792 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 return status;
2795}
2796
2797
Andy Adamson82be4172012-05-23 05:02:35 -04002798static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002799 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002800 int flags,
2801 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002802 struct nfs4_label *label,
2803 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002805 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 struct nfs4_exception exception = { };
2807 struct nfs4_state *res;
2808 int status;
2809
2810 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002811 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002812 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002813 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 if (status == 0)
2815 break;
2816 /* NOTE: BAD_SEQID means the server and client disagree about the
2817 * book-keeping w.r.t. state-changing operations
2818 * (OPEN/CLOSE/LOCK/LOCKU...)
2819 * It is actually a sign of a bug on the client or on the server.
2820 *
2821 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002822 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 * have unhashed the old state_owner for us, and that we can
2824 * therefore safely retry using a new one. We should still warn
2825 * the user though...
2826 */
2827 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002828 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002829 " returned a bad sequence-id error!\n",
2830 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 exception.retry = 1;
2832 continue;
2833 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002834 /*
2835 * BAD_STATEID on OPEN means that the server cancelled our
2836 * state before it received the OPEN_CONFIRM.
2837 * Recover by retrying the request as per the discussion
2838 * on Page 181 of RFC3530.
2839 */
2840 if (status == -NFS4ERR_BAD_STATEID) {
2841 exception.retry = 1;
2842 continue;
2843 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002844 if (status == -EAGAIN) {
2845 /* We must have found a delegation */
2846 exception.retry = 1;
2847 continue;
2848 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002849 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2850 continue;
2851 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 status, &exception));
2853 } while (exception.retry);
2854 return res;
2855}
2856
Trond Myklebust8487c472016-06-26 08:44:35 -04002857static int _nfs4_do_setattr(struct inode *inode,
2858 struct nfs_setattrargs *arg,
2859 struct nfs_setattrres *res,
2860 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002861 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002862{
2863 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002864 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04002865 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2866 .rpc_argp = arg,
2867 .rpc_resp = res,
2868 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002869 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002870 struct rpc_cred *delegation_cred = NULL;
2871 unsigned long timestamp = jiffies;
2872 fmode_t fmode;
2873 bool truncate;
2874 int status;
2875
2876 nfs_fattr_init(res->fattr);
2877
2878 /* Servers should only apply open mode checks for file size changes */
2879 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2880 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2881
2882 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2883 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002884 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002885 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002886 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002887 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002888 l_ctx = nfs_get_lock_context(ctx);
2889 if (IS_ERR(l_ctx))
2890 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002891 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2892 &arg->stateid, &delegation_cred);
2893 nfs_put_lock_context(l_ctx);
2894 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002895 return -EBADF;
2896 } else
2897 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2898 if (delegation_cred)
2899 msg.rpc_cred = delegation_cred;
2900
2901 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2902
2903 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002904 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002905 renew_lease(server, timestamp);
2906 trace_nfs4_setattr(inode, &arg->stateid, status);
2907 return status;
2908}
2909
2910static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2911 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002912 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002913 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002915 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002916 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002917 struct nfs_setattrargs arg = {
2918 .fh = NFS_FH(inode),
2919 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 .server = server,
2921 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002922 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002923 };
2924 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002926 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002928 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002929 struct nfs4_exception exception = {
2930 .state = state,
2931 .inode = inode,
2932 .stateid = &arg.stateid,
2933 };
2934 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
David Quigleyaa9c2662013-05-22 12:50:44 -04002936 arg.bitmask = nfs4_bitmask(server, ilabel);
2937 if (ilabel)
2938 arg.bitmask = nfs4_bitmask(server, olabel);
2939
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 do {
NeilBrown29b59f92016-10-13 15:26:47 +11002941 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04002942 switch (err) {
2943 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002944 if (!(sattr->ia_valid & ATTR_SIZE)) {
2945 pr_warn_once("NFSv4: server %s is incorrectly "
2946 "applying open mode checks to "
2947 "a SETATTR that is not "
2948 "changing file size.\n",
2949 server->nfs_client->cl_hostname);
2950 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002951 if (state && !(state->state & FMODE_WRITE)) {
2952 err = -EBADF;
2953 if (sattr->ia_valid & ATTR_OPEN)
2954 err = -EACCES;
2955 goto out;
2956 }
2957 }
2958 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002960out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 return err;
2962}
2963
Peng Tao500d7012015-09-22 11:35:22 +08002964static bool
2965nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2966{
2967 if (inode == NULL || !nfs_have_layout(inode))
2968 return false;
2969
2970 return pnfs_wait_on_layoutreturn(inode, task);
2971}
2972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973struct nfs4_closedata {
2974 struct inode *inode;
2975 struct nfs4_state *state;
2976 struct nfs_closeargs arg;
2977 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05002978 struct {
2979 struct nfs4_layoutreturn_args arg;
2980 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04002981 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05002982 u32 roc_barrier;
2983 bool roc;
2984 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002985 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002986 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987};
2988
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002989static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002990{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002991 struct nfs4_closedata *calldata = data;
2992 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002993 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002994
Trond Myklebustcf805162016-11-15 14:56:07 -05002995 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05002996 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
2997 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07002998 nfs4_put_open_state(calldata->state);
2999 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003000 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003001 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003002 kfree(calldata);
3003}
3004
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003005static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003007 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003010 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Chuck Levera3ca5652012-03-01 17:00:40 -05003012 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003013 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3014 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003015 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003016
3017 /* Handle Layoutreturn errors */
3018 if (calldata->arg.lr_args && task->tk_status != 0) {
3019 switch (calldata->res.lr_ret) {
3020 default:
3021 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3022 break;
3023 case 0:
3024 calldata->arg.lr_args = NULL;
3025 calldata->res.lr_res = NULL;
3026 break;
3027 case -NFS4ERR_ADMIN_REVOKED:
3028 case -NFS4ERR_DELEG_REVOKED:
3029 case -NFS4ERR_EXPIRED:
3030 case -NFS4ERR_BAD_STATEID:
3031 case -NFS4ERR_OLD_STATEID:
3032 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3033 case -NFS4ERR_WRONG_CRED:
3034 calldata->arg.lr_args = NULL;
3035 calldata->res.lr_res = NULL;
3036 calldata->res.lr_ret = 0;
3037 rpc_restart_call_prepare(task);
3038 return;
3039 }
3040 }
3041
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003042 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 * the state_owner. we keep this around to process errors
3044 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 switch (task->tk_status) {
3046 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003047 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003048 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003049 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003050 case -NFS4ERR_ACCESS:
3051 if (calldata->arg.bitmask != NULL) {
3052 calldata->arg.bitmask = NULL;
3053 calldata->res.fattr = NULL;
3054 task->tk_status = 0;
3055 rpc_restart_call_prepare(task);
3056 goto out_release;
3057
3058 }
3059 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003060 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003062 case -NFS4ERR_EXPIRED:
3063 nfs4_free_revoked_stateid(server,
3064 &calldata->arg.stateid,
3065 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003066 case -NFS4ERR_OLD_STATEID:
3067 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003068 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003069 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003070 rpc_restart_call_prepare(task);
3071 goto out_release;
3072 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003073 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003074 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003076 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003077 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003078 goto out_release;
3079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003081 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3082 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003083out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003084 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003085 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003086 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087}
3088
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003089static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003091 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003092 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003093 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003094 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003095 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003096
Chuck Levera3ca5652012-03-01 17:00:40 -05003097 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003098 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003099 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003100
Trond Myklebust88069f72009-12-08 08:33:16 -05003101 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003102 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003103 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3104 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3105 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003106 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003107 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003108 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003109 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003110 if (state->n_rdonly == 0)
3111 call_close |= is_rdonly;
3112 else if (is_rdonly)
3113 calldata->arg.fmode |= FMODE_READ;
3114 if (state->n_wronly == 0)
3115 call_close |= is_wronly;
3116 else if (is_wronly)
3117 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003118 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3119 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003120 } else if (is_rdwr)
3121 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3122
Trond Myklebust5cc78612016-11-14 11:19:56 -05003123 if (!nfs4_valid_open_stateid(state) ||
3124 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003125 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003126 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003127
3128 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003129 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003130 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003131 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003132
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003133 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003134 nfs_release_seqid(calldata->arg.seqid);
3135 goto out_wait;
3136 }
3137
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003138 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003139 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003140
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003141 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003142 /* Close-to-open cache consistency revalidation */
3143 if (!nfs4_have_delegation(inode, FMODE_READ))
3144 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3145 else
3146 calldata->arg.bitmask = NULL;
3147 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003148
Trond Myklebust6ae37332015-01-30 14:21:14 -05003149 calldata->arg.share_access =
3150 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3151 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003152
Trond Myklebustd8d84982016-12-19 12:14:44 -05003153 if (calldata->res.fattr == NULL)
3154 calldata->arg.bitmask = NULL;
3155 else if (calldata->arg.bitmask == NULL)
3156 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003157 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003158 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003159 &calldata->arg.seq_args,
3160 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003161 task) != 0)
3162 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003163 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003164 return;
3165out_no_action:
3166 task->tk_action = NULL;
3167out_wait:
3168 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169}
3170
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003171static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003172 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003173 .rpc_call_done = nfs4_close_done,
3174 .rpc_release = nfs4_free_closedata,
3175};
3176
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177/*
3178 * It is possible for data to be read/written from a mem-mapped file
3179 * after the sys_close call (which hits the vfs layer as a flush).
3180 * This means that we can't safely call nfsv4 close on a file until
3181 * the inode is cleared. This in turn means that we are not good
3182 * NFSv4 citizens - we do not indicate to the server to update the file's
3183 * share state even when we are done with one of the three share
3184 * stateid's in the inode.
3185 *
3186 * NOTE: Caller must be holding the sp->so_owner semaphore!
3187 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003188int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003190 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003191 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003193 struct nfs4_state_owner *sp = state->owner;
3194 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003195 struct rpc_message msg = {
3196 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3197 .rpc_cred = state->owner->so_cred,
3198 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003199 struct rpc_task_setup task_setup_data = {
3200 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003201 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003202 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003203 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003204 .flags = RPC_TASK_ASYNC,
3205 };
Trond Myklebust95121352005-10-18 14:20:12 -07003206 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003208 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3209 &task_setup_data.rpc_client, &msg);
3210
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003211 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003213 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003214 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003215 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003217 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003219 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3220 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003221 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003222 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003223 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003224 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003225 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003226 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003227 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003228 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003229 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003230 calldata->lr.roc = pnfs_roc(state->inode,
3231 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3232 if (calldata->lr.roc) {
3233 calldata->arg.lr_args = &calldata->lr.arg;
3234 calldata->res.lr_res = &calldata->lr.res;
3235 }
Al Viro643168c2011-06-22 18:20:23 -04003236 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003237
Trond Myklebust1174dd12010-12-21 10:52:24 -05003238 msg.rpc_argp = &calldata->arg;
3239 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003240 task_setup_data.callback_data = calldata;
3241 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003242 if (IS_ERR(task))
3243 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003244 status = 0;
3245 if (wait)
3246 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003247 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003248 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003249out_free_calldata:
3250 kfree(calldata);
3251out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003252 nfs4_put_open_state(state);
3253 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003254 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255}
3256
Trond Myklebust2b484292010-09-17 10:56:51 -04003257static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003258nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3259 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003262 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3263
3264 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
Trond Myklebust565277f2007-10-15 18:17:53 -04003266 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003267 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003268
3269 nfs4_label_release_security(label);
3270
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003271 if (IS_ERR(state))
3272 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003273 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274}
3275
Trond Myklebust1185a552009-12-03 15:54:02 -05003276static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003277{
3278 if (ctx->state == NULL)
3279 return;
3280 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003281 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003282 else
Al Viro643168c2011-06-22 18:20:23 -04003283 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003284}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Trond Myklebustb944dba2013-11-04 15:20:20 -05003286#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3287#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003288#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003289
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3291{
Kinglong Mee8c612822015-08-26 21:12:58 +08003292 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003293 struct nfs4_server_caps_arg args = {
3294 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003295 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003296 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 struct nfs4_server_caps_res res = {};
3298 struct rpc_message msg = {
3299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003300 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 .rpc_resp = &res,
3302 };
3303 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003304 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Kinglong Mee8c612822015-08-26 21:12:58 +08003306 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3307 FATTR4_WORD0_FH_EXPIRE_TYPE |
3308 FATTR4_WORD0_LINK_SUPPORT |
3309 FATTR4_WORD0_SYMLINK_SUPPORT |
3310 FATTR4_WORD0_ACLSUPPORT;
3311 if (minorversion)
3312 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3313
Bryan Schumaker7c513052011-03-24 17:12:24 +00003314 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003316 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003317 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003318 case 0:
3319 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3320 res.attr_bitmask[2] = 0;
3321 break;
3322 case 1:
3323 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3324 break;
3325 case 2:
3326 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003329 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3330 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3331 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3332 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003333 NFS_CAP_CTIME|NFS_CAP_MTIME|
3334 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003335 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3336 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 server->caps |= NFS_CAP_ACLS;
3338 if (res.has_links != 0)
3339 server->caps |= NFS_CAP_HARDLINKS;
3340 if (res.has_symlinks != 0)
3341 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003342 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3343 server->caps |= NFS_CAP_FILEID;
3344 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3345 server->caps |= NFS_CAP_MODE;
3346 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3347 server->caps |= NFS_CAP_NLINK;
3348 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3349 server->caps |= NFS_CAP_OWNER;
3350 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3351 server->caps |= NFS_CAP_OWNER_GROUP;
3352 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3353 server->caps |= NFS_CAP_ATIME;
3354 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3355 server->caps |= NFS_CAP_CTIME;
3356 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3357 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003358#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3359 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3360 server->caps |= NFS_CAP_SECURITY_LABEL;
3361#endif
3362 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3363 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003364 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003365
Trond Myklebusta65318b2009-03-11 14:10:28 -04003366 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3367 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3368 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003369 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003370
3371 /* Avoid a regression due to buggy server */
3372 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3373 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003374 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3375 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003376
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003378 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003380
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 return status;
3382}
3383
Trond Myklebust55a97592006-06-09 09:34:19 -04003384int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385{
3386 struct nfs4_exception exception = { };
3387 int err;
3388 do {
3389 err = nfs4_handle_exception(server,
3390 _nfs4_server_capabilities(server, fhandle),
3391 &exception);
3392 } while (exception.retry);
3393 return err;
3394}
3395
3396static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3397 struct nfs_fsinfo *info)
3398{
David Quigleyaa9c2662013-05-22 12:50:44 -04003399 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003401 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 };
3403 struct nfs4_lookup_res res = {
3404 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003405 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 .fh = fhandle,
3407 };
3408 struct rpc_message msg = {
3409 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3410 .rpc_argp = &args,
3411 .rpc_resp = &res,
3412 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003413
David Quigleyaa9c2662013-05-22 12:50:44 -04003414 bitmask[0] = nfs4_fattr_bitmap[0];
3415 bitmask[1] = nfs4_fattr_bitmap[1];
3416 /*
3417 * Process the label in the upcoming getfattr
3418 */
3419 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3420
Trond Myklebust0e574af2005-10-27 22:12:38 -04003421 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003422 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423}
3424
3425static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3426 struct nfs_fsinfo *info)
3427{
3428 struct nfs4_exception exception = { };
3429 int err;
3430 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003431 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003432 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003433 switch (err) {
3434 case 0:
3435 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003436 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003437 default:
3438 err = nfs4_handle_exception(server, err, &exception);
3439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003441out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 return err;
3443}
3444
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003445static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3446 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3447{
Trond Myklebustc2190662013-08-26 19:23:04 -04003448 struct rpc_auth_create_args auth_args = {
3449 .pseudoflavor = flavor,
3450 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003451 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003452
Trond Myklebustc2190662013-08-26 19:23:04 -04003453 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003454 if (IS_ERR(auth))
3455 return -EACCES;
3456 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003457}
3458
Chuck Lever9a744ba2013-03-16 15:56:02 -04003459/*
3460 * Retry pseudoroot lookup with various security flavors. We do this when:
3461 *
3462 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3463 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3464 *
3465 * Returns zero on success, or a negative NFS4ERR value, or a
3466 * negative errno value.
3467 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003468static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003469 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003471 /* Per 3530bis 15.33.5 */
3472 static const rpc_authflavor_t flav_array[] = {
3473 RPC_AUTH_GSS_KRB5P,
3474 RPC_AUTH_GSS_KRB5I,
3475 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003476 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003477 RPC_AUTH_NULL,
3478 };
3479 int status = -EPERM;
3480 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003482 if (server->auth_info.flavor_len > 0) {
3483 /* try each flavor specified by user */
3484 for (i = 0; i < server->auth_info.flavor_len; i++) {
3485 status = nfs4_lookup_root_sec(server, fhandle, info,
3486 server->auth_info.flavors[i]);
3487 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3488 continue;
3489 break;
3490 }
3491 } else {
3492 /* no flavors specified by user, try default list */
3493 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3494 status = nfs4_lookup_root_sec(server, fhandle, info,
3495 flav_array[i]);
3496 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3497 continue;
3498 break;
3499 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003500 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003501
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003502 /*
3503 * -EACCESS could mean that the user doesn't have correct permissions
3504 * to access the mount. It could also mean that we tried to mount
3505 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3506 * existing mount programs don't handle -EACCES very well so it should
3507 * be mapped to -EPERM instead.
3508 */
3509 if (status == -EACCES)
3510 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003511 return status;
3512}
3513
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003514/**
3515 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3516 * @server: initialized nfs_server handle
3517 * @fhandle: we fill in the pseudo-fs root file handle
3518 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003519 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003520 *
3521 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003522 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003523int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003524 struct nfs_fsinfo *info,
3525 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003526{
Andre Przywarac7757072015-04-23 17:17:40 +01003527 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003528
Andre Przywarac7757072015-04-23 17:17:40 +01003529 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003530 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003531
3532 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003533 status = server->nfs_client->cl_mvops->find_root_sec(server,
3534 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003535
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 if (status == 0)
3537 status = nfs4_server_capabilities(server, fhandle);
3538 if (status == 0)
3539 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003540
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003541 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542}
3543
Bryan Schumakerbae36242012-05-10 15:07:31 -04003544static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3545 struct nfs_fsinfo *info)
3546{
3547 int error;
3548 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003549 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003550
3551 error = nfs4_server_capabilities(server, mntfh);
3552 if (error < 0) {
3553 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3554 return error;
3555 }
3556
David Quigley14c43f72013-05-22 12:50:43 -04003557 label = nfs4_label_alloc(server, GFP_KERNEL);
3558 if (IS_ERR(label))
3559 return PTR_ERR(label);
3560
David Quigley1775fd32013-05-22 12:50:42 -04003561 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003562 if (error < 0) {
3563 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003564 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003565 }
3566
3567 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3568 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3569 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3570
David Quigley14c43f72013-05-22 12:50:43 -04003571err_free_label:
3572 nfs4_label_free(label);
3573
Bryan Schumakerbae36242012-05-10 15:07:31 -04003574 return error;
3575}
3576
Manoj Naik6b97fd32006-06-09 09:34:29 -04003577/*
3578 * Get locations and (maybe) other attributes of a referral.
3579 * Note that we'll actually follow the referral later when
3580 * we detect fsid mismatch in inode revalidation
3581 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003582static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3583 const struct qstr *name, struct nfs_fattr *fattr,
3584 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003585{
3586 int status = -ENOMEM;
3587 struct page *page = NULL;
3588 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003589
3590 page = alloc_page(GFP_KERNEL);
3591 if (page == NULL)
3592 goto out;
3593 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3594 if (locations == NULL)
3595 goto out;
3596
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003597 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003598 if (status != 0)
3599 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003600
3601 /*
3602 * If the fsid didn't change, this is a migration event, not a
3603 * referral. Cause us to drop into the exception handler, which
3604 * will kick off migration recovery.
3605 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003606 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003607 dprintk("%s: server did not return a different fsid for"
3608 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003609 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003610 goto out;
3611 }
Andy Adamson533eb462011-06-13 18:25:56 -04003612 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3613 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003614
Andy Adamson533eb462011-06-13 18:25:56 -04003615 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003616 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003617 memset(fhandle, 0, sizeof(struct nfs_fh));
3618out:
3619 if (page)
3620 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003621 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003622 return status;
3623}
3624
David Quigley1775fd32013-05-22 12:50:42 -04003625static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3626 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
3628 struct nfs4_getattr_arg args = {
3629 .fh = fhandle,
3630 .bitmask = server->attr_bitmask,
3631 };
3632 struct nfs4_getattr_res res = {
3633 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003634 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 .server = server,
3636 };
3637 struct rpc_message msg = {
3638 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3639 .rpc_argp = &args,
3640 .rpc_resp = &res,
3641 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003642
3643 args.bitmask = nfs4_bitmask(server, label);
3644
Trond Myklebust0e574af2005-10-27 22:12:38 -04003645 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003646 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647}
3648
David Quigley1775fd32013-05-22 12:50:42 -04003649static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3650 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651{
3652 struct nfs4_exception exception = { };
3653 int err;
3654 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003655 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3656 trace_nfs4_getattr(server, fhandle, fattr, err);
3657 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 &exception);
3659 } while (exception.retry);
3660 return err;
3661}
3662
3663/*
3664 * The file is not closed if it is opened due to the a request to change
3665 * the size of the file. The open call will not be needed once the
3666 * VFS layer lookup-intents are implemented.
3667 *
3668 * Close is called when the inode is destroyed.
3669 * If we haven't opened the file for O_WRONLY, we
3670 * need to in the size_change case to obtain a stateid.
3671 *
3672 * Got race?
3673 * Because OPEN is always done by name in nfsv4, it is
3674 * possible that we opened a different file by the same
3675 * name. We can recognize this race condition, but we
3676 * can't do anything about it besides returning an error.
3677 *
3678 * This will be fixed with VFS changes (lookup-intent).
3679 */
3680static int
3681nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3682 struct iattr *sattr)
3683{
David Howells2b0143b2015-03-17 22:25:59 +00003684 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003685 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003686 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003687 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 int status;
3689
Peng Tao88ac8152014-09-12 11:04:10 +08003690 if (pnfs_ld_layoutret_on_setattr(inode) &&
3691 sattr->ia_valid & ATTR_SIZE &&
3692 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003693 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003694
Trond Myklebust0e574af2005-10-27 22:12:38 -04003695 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696
Andy Adamson26699402012-05-30 16:12:24 -04003697 /* Deal with open(O_TRUNC) */
3698 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003699 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003700
3701 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003702 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003703 return 0;
3704
Trond Myklebustd5308382005-11-04 15:33:38 -05003705 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003706 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003707
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003708 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003709 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003710 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003711 }
3712
David Quigley14c43f72013-05-22 12:50:43 -04003713 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3714 if (IS_ERR(label))
3715 return PTR_ERR(label);
3716
NeilBrown29b59f92016-10-13 15:26:47 +11003717 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003718 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003719 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003720 nfs_setsecurity(inode, fattr, label);
3721 }
David Quigley14c43f72013-05-22 12:50:43 -04003722 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 return status;
3724}
3725
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003726static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3727 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003728 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003729{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003730 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003731 int status;
3732 struct nfs4_lookup_arg args = {
3733 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003734 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003735 .name = name,
3736 };
3737 struct nfs4_lookup_res res = {
3738 .server = server,
3739 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003740 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003741 .fh = fhandle,
3742 };
3743 struct rpc_message msg = {
3744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3745 .rpc_argp = &args,
3746 .rpc_resp = &res,
3747 };
3748
David Quigleyaa9c2662013-05-22 12:50:44 -04003749 args.bitmask = nfs4_bitmask(server, label);
3750
David Howells2b3de442006-08-22 20:06:09 -04003751 nfs_fattr_init(fattr);
3752
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003754 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 dprintk("NFS reply lookup: %d\n", status);
3756 return status;
3757}
3758
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003759static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003760{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003761 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003762 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003763 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3764 fattr->nlink = 2;
3765}
3766
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003767static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003768 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003769 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003770{
3771 struct nfs4_exception exception = { };
3772 struct rpc_clnt *client = *clnt;
3773 int err;
3774 do {
David Quigley1775fd32013-05-22 12:50:42 -04003775 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003776 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003777 switch (err) {
3778 case -NFS4ERR_BADNAME:
3779 err = -ENOENT;
3780 goto out;
3781 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003782 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003783 if (err == -NFS4ERR_MOVED)
3784 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003785 goto out;
3786 case -NFS4ERR_WRONGSEC:
3787 err = -EPERM;
3788 if (client != *clnt)
3789 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003790 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003791 if (IS_ERR(client))
3792 return PTR_ERR(client);
3793
3794 exception.retry = 1;
3795 break;
3796 default:
3797 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3798 }
3799 } while (exception.retry);
3800
3801out:
3802 if (err == 0)
3803 *clnt = client;
3804 else if (client != *clnt)
3805 rpc_shutdown_client(client);
3806
3807 return err;
3808}
3809
Al Virobeffb8f2016-07-20 16:34:42 -04003810static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003811 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3812 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003814 int status;
3815 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003816
David Quigley1775fd32013-05-22 12:50:42 -04003817 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003818 if (client != NFS_CLIENT(dir)) {
3819 rpc_shutdown_client(client);
3820 nfs_fixup_secinfo_attributes(fattr);
3821 }
3822 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823}
3824
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003825struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003826nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003827 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3828{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003829 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003830 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003831
David Quigley1775fd32013-05-22 12:50:42 -04003832 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003833 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003834 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003835 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003836}
3837
Jeff Layton5b5faaf2017-06-29 06:34:52 -07003838static int _nfs4_proc_lookupp(struct inode *inode,
3839 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3840 struct nfs4_label *label)
3841{
3842 struct rpc_clnt *clnt = NFS_CLIENT(inode);
3843 struct nfs_server *server = NFS_SERVER(inode);
3844 int status;
3845 struct nfs4_lookupp_arg args = {
3846 .bitmask = server->attr_bitmask,
3847 .fh = NFS_FH(inode),
3848 };
3849 struct nfs4_lookupp_res res = {
3850 .server = server,
3851 .fattr = fattr,
3852 .label = label,
3853 .fh = fhandle,
3854 };
3855 struct rpc_message msg = {
3856 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
3857 .rpc_argp = &args,
3858 .rpc_resp = &res,
3859 };
3860
3861 args.bitmask = nfs4_bitmask(server, label);
3862
3863 nfs_fattr_init(fattr);
3864
3865 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
3866 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
3867 &res.seq_res, 0);
3868 dprintk("NFS reply lookupp: %d\n", status);
3869 return status;
3870}
3871
3872static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
3873 struct nfs_fattr *fattr, struct nfs4_label *label)
3874{
3875 struct nfs4_exception exception = { };
3876 int err;
3877 do {
3878 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
3879 trace_nfs4_lookupp(inode, err);
3880 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3881 &exception);
3882 } while (exception.retry);
3883 return err;
3884}
3885
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3887{
Trond Myklebust76b32992007-08-10 17:45:11 -04003888 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 struct nfs4_accessargs args = {
3890 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003891 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003893 struct nfs4_accessres res = {
3894 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003895 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 struct rpc_message msg = {
3897 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3898 .rpc_argp = &args,
3899 .rpc_resp = &res,
3900 .rpc_cred = entry->cred,
3901 };
3902 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003903 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
3905 /*
3906 * Determine which access bits we want to ask for...
3907 */
3908 if (mode & MAY_READ)
3909 args.access |= NFS4_ACCESS_READ;
3910 if (S_ISDIR(inode->i_mode)) {
3911 if (mode & MAY_WRITE)
3912 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3913 if (mode & MAY_EXEC)
3914 args.access |= NFS4_ACCESS_LOOKUP;
3915 } else {
3916 if (mode & MAY_WRITE)
3917 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3918 if (mode & MAY_EXEC)
3919 args.access |= NFS4_ACCESS_EXECUTE;
3920 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003921
3922 res.fattr = nfs_alloc_fattr();
3923 if (res.fattr == NULL)
3924 return -ENOMEM;
3925
Bryan Schumaker7c513052011-03-24 17:12:24 +00003926 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003928 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003929 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003931 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 return status;
3933}
3934
3935static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3936{
3937 struct nfs4_exception exception = { };
3938 int err;
3939 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003940 err = _nfs4_proc_access(inode, entry);
3941 trace_nfs4_access(inode, err);
3942 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 &exception);
3944 } while (exception.retry);
3945 return err;
3946}
3947
3948/*
3949 * TODO: For the time being, we don't try to get any attributes
3950 * along with any of the zero-copy operations READ, READDIR,
3951 * READLINK, WRITE.
3952 *
3953 * In the case of the first three, we want to put the GETATTR
3954 * after the read-type operation -- this is because it is hard
3955 * to predict the length of a GETATTR response in v4, and thus
3956 * align the READ data correctly. This means that the GETATTR
3957 * may end up partially falling into the page cache, and we should
3958 * shift it into the 'tail' of the xdr_buf before processing.
3959 * To do this efficiently, we need to know the total length
3960 * of data received, which doesn't seem to be available outside
3961 * of the RPC layer.
3962 *
3963 * In the case of WRITE, we also want to put the GETATTR after
3964 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003965 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 *
3967 * Both of these changes to the XDR layer would in fact be quite
3968 * minor, but I decided to leave them for a subsequent patch.
3969 */
3970static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3971 unsigned int pgbase, unsigned int pglen)
3972{
3973 struct nfs4_readlink args = {
3974 .fh = NFS_FH(inode),
3975 .pgbase = pgbase,
3976 .pglen = pglen,
3977 .pages = &page,
3978 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003979 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 struct rpc_message msg = {
3981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3982 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003983 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 };
3985
Bryan Schumaker7c513052011-03-24 17:12:24 +00003986 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 -07003987}
3988
3989static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3990 unsigned int pgbase, unsigned int pglen)
3991{
3992 struct nfs4_exception exception = { };
3993 int err;
3994 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003995 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3996 trace_nfs4_readlink(inode, err);
3997 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 &exception);
3999 } while (exception.retry);
4000 return err;
4001}
4002
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004004 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006static int
4007nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004008 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004010 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004011 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004012 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 int status = 0;
4015
NeilBrown532d4de2016-10-13 15:26:47 +11004016 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004017 if (IS_ERR(ctx))
4018 return PTR_ERR(ctx);
4019
David Quigleyaa9c2662013-05-22 12:50:44 -04004020 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4021
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004022 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4023 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004024 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 if (IS_ERR(state)) {
4026 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004027 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004030 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004031 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 return status;
4033}
4034
Al Virobeffb8f2016-07-20 16:34:42 -04004035static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036{
Trond Myklebust16e42952005-10-27 22:12:44 -04004037 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004038 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004040 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004042 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004043 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004044 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004046 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4047 .rpc_argp = &args,
4048 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004050 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004051 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004052
Bryan Schumaker7c513052011-03-24 17:12:24 +00004053 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004054 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004055 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 return status;
4057}
4058
Al Virobeffb8f2016-07-20 16:34:42 -04004059static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060{
4061 struct nfs4_exception exception = { };
4062 int err;
4063 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004064 err = _nfs4_proc_remove(dir, name);
4065 trace_nfs4_remove(dir, name, err);
4066 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 &exception);
4068 } while (exception.retry);
4069 return err;
4070}
4071
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004072static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004074 struct nfs_server *server = NFS_SERVER(dir);
4075 struct nfs_removeargs *args = msg->rpc_argp;
4076 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004078 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004080 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004081
4082 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083}
4084
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004085static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4086{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004087 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004088 &data->args.seq_args,
4089 &data->res.seq_res,
4090 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091}
4092
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004093static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004095 struct nfs_unlinkdata *data = task->tk_calldata;
4096 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004097
Trond Myklebust14516c32010-07-31 14:29:06 -04004098 if (!nfs4_sequence_done(task, &res->seq_res))
4099 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004100 if (nfs4_async_handle_error(task, res->server, NULL,
4101 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004102 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004103 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004104 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004105 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106}
4107
Jeff Laytond3d41522010-09-17 17:31:57 -04004108static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4109{
4110 struct nfs_server *server = NFS_SERVER(dir);
4111 struct nfs_renameargs *arg = msg->rpc_argp;
4112 struct nfs_renameres *res = msg->rpc_resp;
4113
4114 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004115 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004116 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004117}
4118
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004119static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4120{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004121 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004122 &data->args.seq_args,
4123 &data->res.seq_res,
4124 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004125}
4126
4127static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4128 struct inode *new_dir)
4129{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004130 struct nfs_renamedata *data = task->tk_calldata;
4131 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004132
4133 if (!nfs4_sequence_done(task, &res->seq_res))
4134 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004135 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004136 return 0;
4137
Trond Myklebustc733c492017-01-11 12:32:26 -05004138 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004139 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004140 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004141 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004142 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004143 return 1;
4144}
4145
Al Virobeffb8f2016-07-20 16:34:42 -04004146static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004148 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 struct nfs4_link_arg arg = {
4150 .fh = NFS_FH(inode),
4151 .dir_fh = NFS_FH(dir),
4152 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004153 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004155 struct nfs4_link_res res = {
4156 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004157 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004158 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 struct rpc_message msg = {
4160 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4161 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004162 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004164 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
Trond Myklebust136f2622010-04-16 16:22:49 -04004166 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004167 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004168 goto out;
4169
David Quigley14c43f72013-05-22 12:50:43 -04004170 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4171 if (IS_ERR(res.label)) {
4172 status = PTR_ERR(res.label);
4173 goto out;
4174 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004175 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004176
Bryan Schumaker7c513052011-03-24 17:12:24 +00004177 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004178 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004179 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004180 status = nfs_post_op_update_inode(inode, res.fattr);
4181 if (!status)
4182 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004183 }
David Quigley14c43f72013-05-22 12:50:43 -04004184
4185
4186 nfs4_label_free(res.label);
4187
Trond Myklebust136f2622010-04-16 16:22:49 -04004188out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004189 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 return status;
4191}
4192
Al Virobeffb8f2016-07-20 16:34:42 -04004193static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194{
4195 struct nfs4_exception exception = { };
4196 int err;
4197 do {
4198 err = nfs4_handle_exception(NFS_SERVER(inode),
4199 _nfs4_proc_link(inode, dir, name),
4200 &exception);
4201 } while (exception.retry);
4202 return err;
4203}
4204
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004205struct nfs4_createdata {
4206 struct rpc_message msg;
4207 struct nfs4_create_arg arg;
4208 struct nfs4_create_res res;
4209 struct nfs_fh fh;
4210 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004211 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004212};
4213
4214static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004215 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004216{
4217 struct nfs4_createdata *data;
4218
4219 data = kzalloc(sizeof(*data), GFP_KERNEL);
4220 if (data != NULL) {
4221 struct nfs_server *server = NFS_SERVER(dir);
4222
David Quigley14c43f72013-05-22 12:50:43 -04004223 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4224 if (IS_ERR(data->label))
4225 goto out_free;
4226
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004227 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4228 data->msg.rpc_argp = &data->arg;
4229 data->msg.rpc_resp = &data->res;
4230 data->arg.dir_fh = NFS_FH(dir);
4231 data->arg.server = server;
4232 data->arg.name = name;
4233 data->arg.attrs = sattr;
4234 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004235 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004236 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004237 data->res.server = server;
4238 data->res.fh = &data->fh;
4239 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004240 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004241 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004242 }
4243 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004244out_free:
4245 kfree(data);
4246 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004247}
4248
4249static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4250{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004251 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004252 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004253 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004254 update_changeattr(dir, &data->res.dir_cinfo,
4255 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004256 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004257 }
4258 return status;
4259}
4260
4261static void nfs4_free_createdata(struct nfs4_createdata *data)
4262{
David Quigley14c43f72013-05-22 12:50:43 -04004263 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004264 kfree(data);
4265}
4266
Chuck Lever4f390c12006-08-22 20:06:22 -04004267static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004268 struct page *page, unsigned int len, struct iattr *sattr,
4269 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004271 struct nfs4_createdata *data;
4272 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
Chuck Lever94a6d752006-08-22 20:06:23 -04004274 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004275 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004276
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004277 status = -ENOMEM;
4278 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4279 if (data == NULL)
4280 goto out;
4281
4282 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4283 data->arg.u.symlink.pages = &page;
4284 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004285 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004287 status = nfs4_do_create(dir, dentry, data);
4288
4289 nfs4_free_createdata(data);
4290out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 return status;
4292}
4293
Chuck Lever4f390c12006-08-22 20:06:22 -04004294static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004295 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296{
4297 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004298 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004300
4301 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4302
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004304 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4305 trace_nfs4_symlink(dir, &dentry->d_name, err);
4306 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 &exception);
4308 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004309
4310 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 return err;
4312}
4313
4314static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004315 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004317 struct nfs4_createdata *data;
4318 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004320 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4321 if (data == NULL)
4322 goto out;
4323
David Quigley1775fd32013-05-22 12:50:42 -04004324 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004325 status = nfs4_do_create(dir, dentry, data);
4326
4327 nfs4_free_createdata(data);
4328out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 return status;
4330}
4331
4332static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4333 struct iattr *sattr)
4334{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004335 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004337 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004339
David Quigleyaa9c2662013-05-22 12:50:44 -04004340 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4341
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004342 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4343 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004345 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4346 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4347 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 &exception);
4349 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004350 nfs4_label_release_security(label);
4351
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 return err;
4353}
4354
4355static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004356 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357{
David Howells2b0143b2015-03-17 22:25:59 +00004358 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 struct nfs4_readdir_arg args = {
4360 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004361 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 .pgbase = 0,
4363 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004364 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004365 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 };
4367 struct nfs4_readdir_res res;
4368 struct rpc_message msg = {
4369 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4370 .rpc_argp = &args,
4371 .rpc_resp = &res,
4372 .rpc_cred = cred,
4373 };
4374 int status;
4375
Al Viro6de14722013-09-16 10:53:17 -04004376 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4377 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004378 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004379 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004381 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 -05004382 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004383 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004384 status += args.pgbase;
4385 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004386
4387 nfs_invalidate_atime(dir);
4388
Harvey Harrison3110ff82008-05-02 13:42:44 -07004389 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 return status;
4391}
4392
4393static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004394 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395{
4396 struct nfs4_exception exception = { };
4397 int err;
4398 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004399 err = _nfs4_proc_readdir(dentry, cred, cookie,
4400 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004401 trace_nfs4_readdir(d_inode(dentry), err);
4402 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 &exception);
4404 } while (exception.retry);
4405 return err;
4406}
4407
4408static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004409 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004411 struct nfs4_createdata *data;
4412 int mode = sattr->ia_mode;
4413 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004415 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4416 if (data == NULL)
4417 goto out;
4418
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004420 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004422 data->arg.ftype = NF4BLK;
4423 data->arg.u.device.specdata1 = MAJOR(rdev);
4424 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 }
4426 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004427 data->arg.ftype = NF4CHR;
4428 data->arg.u.device.specdata1 = MAJOR(rdev);
4429 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004430 } else if (!S_ISSOCK(mode)) {
4431 status = -EINVAL;
4432 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 }
David Quigley1775fd32013-05-22 12:50:42 -04004434
David Quigleyaa9c2662013-05-22 12:50:44 -04004435 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004436 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004437out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004438 nfs4_free_createdata(data);
4439out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 return status;
4441}
4442
4443static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4444 struct iattr *sattr, dev_t rdev)
4445{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004446 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004448 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004450
David Quigleyaa9c2662013-05-22 12:50:44 -04004451 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4452
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004453 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4454 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004456 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4457 trace_nfs4_mknod(dir, &dentry->d_name, err);
4458 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 &exception);
4460 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004461
4462 nfs4_label_release_security(label);
4463
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 return err;
4465}
4466
4467static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4468 struct nfs_fsstat *fsstat)
4469{
4470 struct nfs4_statfs_arg args = {
4471 .fh = fhandle,
4472 .bitmask = server->attr_bitmask,
4473 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004474 struct nfs4_statfs_res res = {
4475 .fsstat = fsstat,
4476 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 struct rpc_message msg = {
4478 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4479 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004480 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 };
4482
Trond Myklebust0e574af2005-10-27 22:12:38 -04004483 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004484 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485}
4486
4487static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4488{
4489 struct nfs4_exception exception = { };
4490 int err;
4491 do {
4492 err = nfs4_handle_exception(server,
4493 _nfs4_proc_statfs(server, fhandle, fsstat),
4494 &exception);
4495 } while (exception.retry);
4496 return err;
4497}
4498
4499static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4500 struct nfs_fsinfo *fsinfo)
4501{
4502 struct nfs4_fsinfo_arg args = {
4503 .fh = fhandle,
4504 .bitmask = server->attr_bitmask,
4505 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004506 struct nfs4_fsinfo_res res = {
4507 .fsinfo = fsinfo,
4508 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 struct rpc_message msg = {
4510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4511 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004512 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 };
4514
Bryan Schumaker7c513052011-03-24 17:12:24 +00004515 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516}
4517
4518static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4519{
4520 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004521 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 int err;
4523
4524 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004525 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004526 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004527 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004528 nfs4_set_lease_period(server->nfs_client,
4529 fsinfo->lease_time * HZ,
4530 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004531 break;
4532 }
4533 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 } while (exception.retry);
4535 return err;
4536}
4537
4538static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4539{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004540 int error;
4541
Trond Myklebust0e574af2005-10-27 22:12:38 -04004542 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004543 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004544 if (error == 0) {
4545 /* block layout checks this! */
4546 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004547 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004548 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004549
4550 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551}
4552
4553static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4554 struct nfs_pathconf *pathconf)
4555{
4556 struct nfs4_pathconf_arg args = {
4557 .fh = fhandle,
4558 .bitmask = server->attr_bitmask,
4559 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004560 struct nfs4_pathconf_res res = {
4561 .pathconf = pathconf,
4562 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 struct rpc_message msg = {
4564 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4565 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004566 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 };
4568
4569 /* None of the pathconf attributes are mandatory to implement */
4570 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4571 memset(pathconf, 0, sizeof(*pathconf));
4572 return 0;
4573 }
4574
Trond Myklebust0e574af2005-10-27 22:12:38 -04004575 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004576 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577}
4578
4579static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4580 struct nfs_pathconf *pathconf)
4581{
4582 struct nfs4_exception exception = { };
4583 int err;
4584
4585 do {
4586 err = nfs4_handle_exception(server,
4587 _nfs4_proc_pathconf(server, fhandle, pathconf),
4588 &exception);
4589 } while (exception.retry);
4590 return err;
4591}
4592
Trond Myklebust5521abf2013-03-16 20:54:34 -04004593int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004594 const struct nfs_open_context *ctx,
4595 const struct nfs_lock_context *l_ctx,
4596 fmode_t fmode)
4597{
NeilBrown17393472016-10-13 15:26:47 +11004598 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004599}
4600EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4601
Trond Myklebust5521abf2013-03-16 20:54:34 -04004602static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4603 const struct nfs_open_context *ctx,
4604 const struct nfs_lock_context *l_ctx,
4605 fmode_t fmode)
4606{
4607 nfs4_stateid current_stateid;
4608
Trond Myklebuste1253be2014-03-05 08:44:23 -05004609 /* If the current stateid represents a lost lock, then exit */
4610 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4611 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004612 return nfs4_stateid_match(stateid, &current_stateid);
4613}
4614
4615static bool nfs4_error_stateid_expired(int err)
4616{
4617 switch (err) {
4618 case -NFS4ERR_DELEG_REVOKED:
4619 case -NFS4ERR_ADMIN_REVOKED:
4620 case -NFS4ERR_BAD_STATEID:
4621 case -NFS4ERR_STALE_STATEID:
4622 case -NFS4ERR_OLD_STATEID:
4623 case -NFS4ERR_OPENMODE:
4624 case -NFS4ERR_EXPIRED:
4625 return true;
4626 }
4627 return false;
4628}
4629
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004630static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004632 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004634 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004635 if (task->tk_status < 0) {
4636 struct nfs4_exception exception = {
4637 .inode = hdr->inode,
4638 .state = hdr->args.context->state,
4639 .stateid = &hdr->args.stateid,
4640 };
4641 task->tk_status = nfs4_async_handle_exception(task,
4642 server, task->tk_status, &exception);
4643 if (exception.retry) {
4644 rpc_restart_call_prepare(task);
4645 return -EAGAIN;
4646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004648
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004650 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004651 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652}
4653
Trond Myklebust5521abf2013-03-16 20:54:34 -04004654static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004655 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004656{
4657
4658 if (!nfs4_error_stateid_expired(task->tk_status) ||
4659 nfs4_stateid_is_current(&args->stateid,
4660 args->context,
4661 args->lock_context,
4662 FMODE_READ))
4663 return false;
4664 rpc_restart_call_prepare(task);
4665 return true;
4666}
4667
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004668static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004669{
4670
4671 dprintk("--> %s\n", __func__);
4672
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004673 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004674 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004675 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004676 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004677 if (task->tk_status > 0)
4678 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004679 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4680 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004681}
4682
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004683static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4684 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004686 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004687 if (!hdr->pgio_done_cb)
4688 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004689 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004690 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691}
4692
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004693static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4694 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004695{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004696 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004697 &hdr->args.seq_args,
4698 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004699 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004700 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004701 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4702 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004703 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004704 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004705 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004706 return -EIO;
4707 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708}
4709
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004710static int nfs4_write_done_cb(struct rpc_task *task,
4711 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004713 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004714
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004715 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004716 if (task->tk_status < 0) {
4717 struct nfs4_exception exception = {
4718 .inode = hdr->inode,
4719 .state = hdr->args.context->state,
4720 .stateid = &hdr->args.stateid,
4721 };
4722 task->tk_status = nfs4_async_handle_exception(task,
4723 NFS_SERVER(inode), task->tk_status,
4724 &exception);
4725 if (exception.retry) {
4726 rpc_restart_call_prepare(task);
4727 return -EAGAIN;
4728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004730 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004731 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004732 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004733 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004734 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735}
4736
Trond Myklebust5521abf2013-03-16 20:54:34 -04004737static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004738 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004739{
4740
4741 if (!nfs4_error_stateid_expired(task->tk_status) ||
4742 nfs4_stateid_is_current(&args->stateid,
4743 args->context,
4744 args->lock_context,
4745 FMODE_WRITE))
4746 return false;
4747 rpc_restart_call_prepare(task);
4748 return true;
4749}
4750
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004751static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004752{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004753 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004754 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004755 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004756 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004757 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4758 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004759}
4760
Trond Myklebust5a37f852012-04-28 14:55:16 -04004761static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004762bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004763{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004764 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004765 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004766 return false;
4767 /* Otherwise, request attributes if and only if we don't hold
4768 * a delegation
4769 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004770 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004771}
Fred Isamana69aef12011-03-03 15:13:47 +00004772
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004773static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4774 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004776 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004777
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004778 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4779 hdr->args.bitmask = NULL;
4780 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004781 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004782 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004783
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004784 if (!hdr->pgio_done_cb)
4785 hdr->pgio_done_cb = nfs4_write_done_cb;
4786 hdr->res.server = server;
4787 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004789 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004790 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791}
4792
Fred Isaman0b7c0152012-04-20 14:47:39 -04004793static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4794{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004795 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004796 &data->args.seq_args,
4797 &data->res.seq_res,
4798 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799}
4800
Fred Isaman0b7c0152012-04-20 14:47:39 -04004801static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004804
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004805 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004806 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4807 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004808 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004809 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004811 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812}
4813
Fred Isaman0b7c0152012-04-20 14:47:39 -04004814static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004815{
4816 if (!nfs4_sequence_done(task, &data->res.seq_res))
4817 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004818 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004819}
4820
Fred Isaman0b7c0152012-04-20 14:47:39 -04004821static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004823 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004824
Fred Isaman0b7c0152012-04-20 14:47:39 -04004825 if (data->commit_done_cb == NULL)
4826 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004827 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004828 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004829 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830}
4831
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004832struct nfs4_renewdata {
4833 struct nfs_client *client;
4834 unsigned long timestamp;
4835};
4836
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837/*
4838 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4839 * standalone procedure for queueing an asynchronous RENEW.
4840 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004841static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004842{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004843 struct nfs4_renewdata *data = calldata;
4844 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004845
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004846 if (atomic_read(&clp->cl_count) > 1)
4847 nfs4_schedule_state_renewal(clp);
4848 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004849 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004850}
4851
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004852static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004854 struct nfs4_renewdata *data = calldata;
4855 struct nfs_client *clp = data->client;
4856 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004858 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004859 switch (task->tk_status) {
4860 case 0:
4861 break;
4862 case -NFS4ERR_LEASE_MOVED:
4863 nfs4_schedule_lease_moved_recovery(clp);
4864 break;
4865 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004866 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004867 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4868 return;
4869 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004870 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004871 return;
4872 }
4873 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004875 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876}
4877
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004878static const struct rpc_call_ops nfs4_renew_ops = {
4879 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004880 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004881};
4882
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004883static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884{
4885 struct rpc_message msg = {
4886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4887 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004888 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004890 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004892 if (renew_flags == 0)
4893 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004894 if (!atomic_inc_not_zero(&clp->cl_count))
4895 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004896 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04004897 if (data == NULL) {
4898 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004899 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04004900 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004901 data->client = clp;
4902 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004903 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004904 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905}
4906
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004907static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908{
4909 struct rpc_message msg = {
4910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4911 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004912 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 };
4914 unsigned long now = jiffies;
4915 int status;
4916
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004917 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 if (status < 0)
4919 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004920 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 return 0;
4922}
4923
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004924static inline int nfs4_server_supports_acls(struct nfs_server *server)
4925{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004926 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004927}
4928
Trond Myklebust21f498c2012-08-24 10:59:25 -04004929/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4930 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004931 * the stack.
4932 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004933#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004934
Neil Hormane9e3d722011-03-04 19:26:03 -05004935static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004936 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004937{
4938 struct page *newpage, **spages;
4939 int rc = 0;
4940 size_t len;
4941 spages = pages;
4942
4943 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004944 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004945 newpage = alloc_page(GFP_KERNEL);
4946
4947 if (newpage == NULL)
4948 goto unwind;
4949 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05004950 buf += len;
4951 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05004952 *pages++ = newpage;
4953 rc++;
4954 } while (buflen != 0);
4955
4956 return rc;
4957
4958unwind:
4959 for(; rc > 0; rc--)
4960 __free_page(spages[rc-1]);
4961 return -ENOMEM;
4962}
4963
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004964struct nfs4_cached_acl {
4965 int cached;
4966 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004967 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004968};
4969
4970static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004971{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004972 struct nfs_inode *nfsi = NFS_I(inode);
4973
4974 spin_lock(&inode->i_lock);
4975 kfree(nfsi->nfs4_acl);
4976 nfsi->nfs4_acl = acl;
4977 spin_unlock(&inode->i_lock);
4978}
4979
4980static void nfs4_zap_acl_attr(struct inode *inode)
4981{
4982 nfs4_set_cached_acl(inode, NULL);
4983}
4984
4985static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4986{
4987 struct nfs_inode *nfsi = NFS_I(inode);
4988 struct nfs4_cached_acl *acl;
4989 int ret = -ENOENT;
4990
4991 spin_lock(&inode->i_lock);
4992 acl = nfsi->nfs4_acl;
4993 if (acl == NULL)
4994 goto out;
4995 if (buf == NULL) /* user is just asking for length */
4996 goto out_len;
4997 if (acl->cached == 0)
4998 goto out;
4999 ret = -ERANGE; /* see getxattr(2) man page */
5000 if (acl->len > buflen)
5001 goto out;
5002 memcpy(buf, acl->data, acl->len);
5003out_len:
5004 ret = acl->len;
5005out:
5006 spin_unlock(&inode->i_lock);
5007 return ret;
5008}
5009
Sachin Prabhu5794d212012-04-17 14:36:40 +01005010static 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 +00005011{
5012 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005013 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005014
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005015 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005016 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005017 if (acl == NULL)
5018 goto out;
5019 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005020 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005021 } else {
5022 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5023 if (acl == NULL)
5024 goto out;
5025 acl->cached = 0;
5026 }
5027 acl->len = acl_len;
5028out:
5029 nfs4_set_cached_acl(inode, acl);
5030}
5031
Andy Adamsonbf118a32011-12-07 11:55:27 -05005032/*
5033 * The getxattr API returns the required buffer length when called with a
5034 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5035 * the required buf. On a NULL buf, we send a page of data to the server
5036 * guessing that the ACL request can be serviced by a page. If so, we cache
5037 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5038 * the cache. If not so, we throw away the page, and cache the required
5039 * length. The next getxattr call will then produce another round trip to
5040 * the server, this time with the input buf of the required size.
5041 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005042static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005043{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005044 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005045 struct nfs_getaclargs args = {
5046 .fh = NFS_FH(inode),
5047 .acl_pages = pages,
5048 .acl_len = buflen,
5049 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005050 struct nfs_getaclres res = {
5051 .acl_len = buflen,
5052 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005053 struct rpc_message msg = {
5054 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5055 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005056 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005057 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005058 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005059 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005060
Trond Myklebust21f498c2012-08-24 10:59:25 -04005061 if (npages > ARRAY_SIZE(pages))
5062 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005063
Andy Adamsonbf118a32011-12-07 11:55:27 -05005064 for (i = 0; i < npages; i++) {
5065 pages[i] = alloc_page(GFP_KERNEL);
5066 if (!pages[i])
5067 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005068 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005069
5070 /* for decoding across pages */
5071 res.acl_scratch = alloc_page(GFP_KERNEL);
5072 if (!res.acl_scratch)
5073 goto out_free;
5074
Andy Adamsonbf118a32011-12-07 11:55:27 -05005075 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005076
Peng Taode040be2012-01-10 22:42:47 +08005077 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005078 __func__, buf, buflen, npages, args.acl_len);
5079 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5080 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005081 if (ret)
5082 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005083
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005084 /* Handle the case where the passed-in buffer is too short */
5085 if (res.acl_flags & NFS4_ACL_TRUNC) {
5086 /* Did the user only issue a request for the acl length? */
5087 if (buf == NULL)
5088 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005089 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005090 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005091 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005092 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005093 if (buf) {
5094 if (res.acl_len > buflen) {
5095 ret = -ERANGE;
5096 goto out_free;
5097 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005098 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005099 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005100out_ok:
5101 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005102out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005103 for (i = 0; i < npages; i++)
5104 if (pages[i])
5105 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005106 if (res.acl_scratch)
5107 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005108 return ret;
5109}
5110
Trond Myklebust16b42892006-08-24 12:27:15 -04005111static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5112{
5113 struct nfs4_exception exception = { };
5114 ssize_t ret;
5115 do {
5116 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005117 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005118 if (ret >= 0)
5119 break;
5120 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5121 } while (exception.retry);
5122 return ret;
5123}
5124
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005125static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5126{
5127 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005128 int ret;
5129
5130 if (!nfs4_server_supports_acls(server))
5131 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005132 ret = nfs_revalidate_inode(server, inode);
5133 if (ret < 0)
5134 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005135 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5136 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005137 ret = nfs4_read_cached_acl(inode, buf, buflen);
5138 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005139 /* -ENOENT is returned if there is no ACL or if there is an ACL
5140 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005141 return ret;
5142 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005143}
5144
Trond Myklebust16b42892006-08-24 12:27:15 -04005145static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005146{
5147 struct nfs_server *server = NFS_SERVER(inode);
5148 struct page *pages[NFS4ACL_MAXPAGES];
5149 struct nfs_setaclargs arg = {
5150 .fh = NFS_FH(inode),
5151 .acl_pages = pages,
5152 .acl_len = buflen,
5153 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005154 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005155 struct rpc_message msg = {
5156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5157 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005158 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005159 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005160 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005161 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005162
5163 if (!nfs4_server_supports_acls(server))
5164 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005165 if (npages > ARRAY_SIZE(pages))
5166 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005167 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005168 if (i < 0)
5169 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005170 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005171 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005172
5173 /*
5174 * Free each page after tx, so the only ref left is
5175 * held by the network stack
5176 */
5177 for (; i > 0; i--)
5178 put_page(pages[i-1]);
5179
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005180 /*
5181 * Acl update can result in inode attribute update.
5182 * so mark the attribute cache invalid.
5183 */
5184 spin_lock(&inode->i_lock);
5185 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5186 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005187 nfs_access_zap_cache(inode);
5188 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005189 return ret;
5190}
5191
Trond Myklebust16b42892006-08-24 12:27:15 -04005192static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5193{
5194 struct nfs4_exception exception = { };
5195 int err;
5196 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005197 err = __nfs4_proc_set_acl(inode, buf, buflen);
5198 trace_nfs4_set_acl(inode, err);
5199 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005200 &exception);
5201 } while (exception.retry);
5202 return err;
5203}
5204
David Quigleyaa9c2662013-05-22 12:50:44 -04005205#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5206static int _nfs4_get_security_label(struct inode *inode, void *buf,
5207 size_t buflen)
5208{
5209 struct nfs_server *server = NFS_SERVER(inode);
5210 struct nfs_fattr fattr;
5211 struct nfs4_label label = {0, 0, buflen, buf};
5212
5213 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005214 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005215 .fh = NFS_FH(inode),
5216 .bitmask = bitmask,
5217 };
5218 struct nfs4_getattr_res res = {
5219 .fattr = &fattr,
5220 .label = &label,
5221 .server = server,
5222 };
5223 struct rpc_message msg = {
5224 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005225 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005226 .rpc_resp = &res,
5227 };
5228 int ret;
5229
5230 nfs_fattr_init(&fattr);
5231
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005232 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005233 if (ret)
5234 return ret;
5235 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5236 return -ENOENT;
5237 if (buflen < label.len)
5238 return -ERANGE;
5239 return 0;
5240}
5241
5242static int nfs4_get_security_label(struct inode *inode, void *buf,
5243 size_t buflen)
5244{
5245 struct nfs4_exception exception = { };
5246 int err;
5247
5248 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5249 return -EOPNOTSUPP;
5250
5251 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005252 err = _nfs4_get_security_label(inode, buf, buflen);
5253 trace_nfs4_get_security_label(inode, err);
5254 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005255 &exception);
5256 } while (exception.retry);
5257 return err;
5258}
5259
5260static int _nfs4_do_set_security_label(struct inode *inode,
5261 struct nfs4_label *ilabel,
5262 struct nfs_fattr *fattr,
5263 struct nfs4_label *olabel)
5264{
5265
5266 struct iattr sattr = {0};
5267 struct nfs_server *server = NFS_SERVER(inode);
5268 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005269 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005270 .fh = NFS_FH(inode),
5271 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005272 .server = server,
5273 .bitmask = bitmask,
5274 .label = ilabel,
5275 };
5276 struct nfs_setattrres res = {
5277 .fattr = fattr,
5278 .label = olabel,
5279 .server = server,
5280 };
5281 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5283 .rpc_argp = &arg,
5284 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005285 };
5286 int status;
5287
Jeff Layton12207f62013-11-01 10:49:32 -04005288 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005289
Jeff Layton12207f62013-11-01 10:49:32 -04005290 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005291 if (status)
5292 dprintk("%s failed: %d\n", __func__, status);
5293
5294 return status;
5295}
5296
5297static int nfs4_do_set_security_label(struct inode *inode,
5298 struct nfs4_label *ilabel,
5299 struct nfs_fattr *fattr,
5300 struct nfs4_label *olabel)
5301{
5302 struct nfs4_exception exception = { };
5303 int err;
5304
5305 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005306 err = _nfs4_do_set_security_label(inode, ilabel,
5307 fattr, olabel);
5308 trace_nfs4_set_security_label(inode, err);
5309 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005310 &exception);
5311 } while (exception.retry);
5312 return err;
5313}
5314
5315static int
Al Viro59301222016-05-27 10:19:30 -04005316nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005317{
5318 struct nfs4_label ilabel, *olabel = NULL;
5319 struct nfs_fattr fattr;
5320 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005321 int status;
5322
5323 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5324 return -EOPNOTSUPP;
5325
5326 nfs_fattr_init(&fattr);
5327
5328 ilabel.pi = 0;
5329 ilabel.lfs = 0;
5330 ilabel.label = (char *)buf;
5331 ilabel.len = buflen;
5332
5333 cred = rpc_lookup_cred();
5334 if (IS_ERR(cred))
5335 return PTR_ERR(cred);
5336
5337 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5338 if (IS_ERR(olabel)) {
5339 status = -PTR_ERR(olabel);
5340 goto out;
5341 }
5342
5343 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5344 if (status == 0)
5345 nfs_setsecurity(inode, &fattr, olabel);
5346
5347 nfs4_label_free(olabel);
5348out:
5349 put_rpccred(cred);
5350 return status;
5351}
5352#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5353
5354
Chuck Leverf0920752012-05-21 22:45:41 -04005355static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5356 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005357{
5358 __be32 verf[2];
5359
Chuck Lever2c820d92012-05-21 22:45:33 -04005360 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5361 /* An impossible timestamp guarantees this value
5362 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005363 verf[0] = cpu_to_be32(U32_MAX);
5364 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005365 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005366 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005367 u64 ns = ktime_to_ns(nn->boot_time);
5368
5369 verf[0] = cpu_to_be32(ns >> 32);
5370 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005371 }
Chuck Levercd937102012-03-02 17:14:31 -05005372 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5373}
5374
Jeff Laytona3192682015-06-09 19:43:59 -04005375static int
5376nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005377{
Jeff Laytona3192682015-06-09 19:43:59 -04005378 size_t len;
5379 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005380
Trond Myklebustceb3a162015-01-03 15:16:04 -05005381 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005382 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005383
Jeff Laytona3192682015-06-09 19:43:59 -04005384 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005385 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005386 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5387 1 +
5388 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5389 1;
5390 rcu_read_unlock();
5391
5392 if (len > NFS4_OPAQUE_LIMIT + 1)
5393 return -EINVAL;
5394
5395 /*
5396 * Since this string is allocated at mount time, and held until the
5397 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5398 * about a memory-reclaim deadlock.
5399 */
5400 str = kmalloc(len, GFP_KERNEL);
5401 if (!str)
5402 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005403
Chuck Levere984a552012-09-14 17:24:21 -04005404 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005405 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005406 clp->cl_ipaddr,
5407 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5408 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005409 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005410
Jeff Laytona3192682015-06-09 19:43:59 -04005411 clp->cl_owner_id = str;
5412 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005413}
5414
Jeff Layton873e3852015-06-09 19:44:00 -04005415static int
5416nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005417{
Jeff Layton873e3852015-06-09 19:44:00 -04005418 size_t len;
5419 char *str;
5420
5421 len = 10 + 10 + 1 + 10 + 1 +
5422 strlen(nfs4_client_id_uniquifier) + 1 +
5423 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5424
5425 if (len > NFS4_OPAQUE_LIMIT + 1)
5426 return -EINVAL;
5427
5428 /*
5429 * Since this string is allocated at mount time, and held until the
5430 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5431 * about a memory-reclaim deadlock.
5432 */
5433 str = kmalloc(len, GFP_KERNEL);
5434 if (!str)
5435 return -ENOMEM;
5436
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005437 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005438 clp->rpc_ops->version, clp->cl_minorversion,
5439 nfs4_client_id_uniquifier,
5440 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005441 clp->cl_owner_id = str;
5442 return 0;
5443}
5444
5445static int
5446nfs4_init_uniform_client_string(struct nfs_client *clp)
5447{
Jeff Layton873e3852015-06-09 19:44:00 -04005448 size_t len;
5449 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005450
5451 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005452 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005453
5454 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005455 return nfs4_init_uniquifier_client_string(clp);
5456
5457 len = 10 + 10 + 1 + 10 + 1 +
5458 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5459
5460 if (len > NFS4_OPAQUE_LIMIT + 1)
5461 return -EINVAL;
5462
5463 /*
5464 * Since this string is allocated at mount time, and held until the
5465 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5466 * about a memory-reclaim deadlock.
5467 */
5468 str = kmalloc(len, GFP_KERNEL);
5469 if (!str)
5470 return -ENOMEM;
5471
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005472 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005473 clp->rpc_ops->version, clp->cl_minorversion,
5474 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005475 clp->cl_owner_id = str;
5476 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005477}
5478
Chuck Lever706cb8d2014-03-12 12:51:47 -04005479/*
5480 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5481 * services. Advertise one based on the address family of the
5482 * clientaddr.
5483 */
5484static unsigned int
5485nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5486{
5487 if (strchr(clp->cl_ipaddr, ':') != NULL)
5488 return scnprintf(buf, len, "tcp6");
5489 else
5490 return scnprintf(buf, len, "tcp");
5491}
5492
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005493static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5494{
5495 struct nfs4_setclientid *sc = calldata;
5496
5497 if (task->tk_status == 0)
5498 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5499}
5500
5501static const struct rpc_call_ops nfs4_setclientid_ops = {
5502 .rpc_call_done = nfs4_setclientid_done,
5503};
5504
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005505/**
5506 * nfs4_proc_setclientid - Negotiate client ID
5507 * @clp: state data structure
5508 * @program: RPC program for NFSv4 callback service
5509 * @port: IP port number for NFS4 callback service
5510 * @cred: RPC credential to use for this call
5511 * @res: where to place the result
5512 *
5513 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5514 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005515int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5516 unsigned short port, struct rpc_cred *cred,
5517 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518{
5519 nfs4_verifier sc_verifier;
5520 struct nfs4_setclientid setclientid = {
5521 .sc_verifier = &sc_verifier,
5522 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005523 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 };
5525 struct rpc_message msg = {
5526 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5527 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005528 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005529 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005531 struct rpc_task *task;
5532 struct rpc_task_setup task_setup_data = {
5533 .rpc_client = clp->cl_rpcclient,
5534 .rpc_message = &msg,
5535 .callback_ops = &nfs4_setclientid_ops,
5536 .callback_data = &setclientid,
5537 .flags = RPC_TASK_TIMEOUT,
5538 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005539 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540
Chuck Leverde734832012-07-11 16:30:50 -04005541 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005542 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005543
5544 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5545 status = nfs4_init_uniform_client_string(clp);
5546 else
Jeff Laytona3192682015-06-09 19:43:59 -04005547 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005548
5549 if (status)
5550 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005551
Chuck Leverde734832012-07-11 16:30:50 -04005552 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005553 setclientid.sc_netid_len =
5554 nfs4_init_callback_netid(clp,
5555 setclientid.sc_netid,
5556 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005557 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005558 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 clp->cl_ipaddr, port >> 8, port & 255);
5560
Jeff Layton3a6bb732015-06-09 19:43:57 -04005561 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005562 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005563 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005564 task = rpc_run_task(&task_setup_data);
5565 if (IS_ERR(task)) {
5566 status = PTR_ERR(task);
5567 goto out;
5568 }
5569 status = task->tk_status;
5570 if (setclientid.sc_cred) {
5571 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5572 put_rpccred(setclientid.sc_cred);
5573 }
5574 rpc_put_task(task);
5575out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005576 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005577 dprintk("NFS reply setclientid: %d\n", status);
5578 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579}
5580
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005581/**
5582 * nfs4_proc_setclientid_confirm - Confirm client ID
5583 * @clp: state data structure
5584 * @res: result of a previous SETCLIENTID
5585 * @cred: RPC credential to use for this call
5586 *
5587 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5588 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005589int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005590 struct nfs4_setclientid_res *arg,
5591 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 struct rpc_message msg = {
5594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005595 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005596 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 int status;
5599
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005600 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5601 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5602 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005603 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005604 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005605 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 return status;
5607}
5608
Trond Myklebustfe650402006-01-03 09:55:18 +01005609struct nfs4_delegreturndata {
5610 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005611 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005612 struct nfs_fh fh;
5613 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005614 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005615 struct {
5616 struct nfs4_layoutreturn_args arg;
5617 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005618 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005619 u32 roc_barrier;
5620 bool roc;
5621 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005622 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005623 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005624 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005625};
5626
Trond Myklebustfe650402006-01-03 09:55:18 +01005627static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5628{
5629 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005630
Trond Myklebust14516c32010-07-31 14:29:06 -04005631 if (!nfs4_sequence_done(task, &data->res.seq_res))
5632 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005633
Trond Myklebustca8acf82013-08-13 10:36:56 -04005634 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005635
5636 /* Handle Layoutreturn errors */
5637 if (data->args.lr_args && task->tk_status != 0) {
5638 switch(data->res.lr_ret) {
5639 default:
5640 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5641 break;
5642 case 0:
5643 data->args.lr_args = NULL;
5644 data->res.lr_res = NULL;
5645 break;
5646 case -NFS4ERR_ADMIN_REVOKED:
5647 case -NFS4ERR_DELEG_REVOKED:
5648 case -NFS4ERR_EXPIRED:
5649 case -NFS4ERR_BAD_STATEID:
5650 case -NFS4ERR_OLD_STATEID:
5651 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5652 case -NFS4ERR_WRONG_CRED:
5653 data->args.lr_args = NULL;
5654 data->res.lr_res = NULL;
5655 data->res.lr_ret = 0;
5656 rpc_restart_call_prepare(task);
5657 return;
5658 }
5659 }
5660
Ricardo Labiaga79708862009-12-07 09:23:21 -05005661 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005662 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005663 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005664 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005665 case -NFS4ERR_ADMIN_REVOKED:
5666 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005667 case -NFS4ERR_EXPIRED:
5668 nfs4_free_revoked_stateid(data->res.server,
5669 data->args.stateid,
5670 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005671 case -NFS4ERR_BAD_STATEID:
5672 case -NFS4ERR_OLD_STATEID:
5673 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005674 task->tk_status = 0;
5675 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005676 case -NFS4ERR_ACCESS:
5677 if (data->args.bitmask) {
5678 data->args.bitmask = NULL;
5679 data->res.fattr = NULL;
5680 task->tk_status = 0;
5681 rpc_restart_call_prepare(task);
5682 return;
5683 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005684 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005685 if (nfs4_async_handle_error(task, data->res.server,
5686 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005687 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005688 return;
5689 }
5690 }
5691 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005692}
5693
5694static void nfs4_delegreturn_release(void *calldata)
5695{
Peng Tao039b7562014-07-03 13:05:02 +08005696 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005697 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005698
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005699 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005700 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005701 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5702 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005703 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005704 nfs_iput_and_deactive(inode);
5705 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005706 kfree(calldata);
5707}
5708
Andy Adamson938e1012009-04-01 09:22:28 -04005709static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5710{
5711 struct nfs4_delegreturndata *d_data;
5712
5713 d_data = (struct nfs4_delegreturndata *)data;
5714
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005715 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005716 return;
5717
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005718 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005719 &d_data->args.seq_args,
5720 &d_data->res.seq_res,
5721 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005722}
Andy Adamson938e1012009-04-01 09:22:28 -04005723
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005724static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005725 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005726 .rpc_call_done = nfs4_delegreturn_done,
5727 .rpc_release = nfs4_delegreturn_release,
5728};
5729
Trond Myklebuste6f81072008-01-24 18:14:34 -05005730static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005731{
5732 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005733 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005734 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005735 struct rpc_message msg = {
5736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5737 .rpc_cred = cred,
5738 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005739 struct rpc_task_setup task_setup_data = {
5740 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005741 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005742 .callback_ops = &nfs4_delegreturn_ops,
5743 .flags = RPC_TASK_ASYNC,
5744 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005745 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005746
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005747 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005748 if (data == NULL)
5749 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005750 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005751
5752 nfs4_state_protect(server->nfs_client,
5753 NFS_SP4_MACH_CRED_CLEANUP,
5754 &task_setup_data.rpc_client, &msg);
5755
Trond Myklebustfe650402006-01-03 09:55:18 +01005756 data->args.fhandle = &data->fh;
5757 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005758 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005759 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005760 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005761 data->res.fattr = &data->fattr;
5762 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005763 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005764 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005765 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005766 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005767 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005768 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005769 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005770 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005771 if (data->lr.roc) {
5772 data->args.lr_args = &data->lr.arg;
5773 data->res.lr_res = &data->lr.res;
5774 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005775 } else if (data->lr.roc) {
5776 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5777 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005778 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005779
Trond Myklebustc970aa82007-07-14 15:39:59 -04005780 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005781 msg.rpc_argp = &data->args;
5782 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005783 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005784 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005785 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005786 if (!issync)
5787 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005788 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005789 if (status != 0)
5790 goto out;
5791 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005792out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005793 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005794 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795}
5796
Trond Myklebuste6f81072008-01-24 18:14:34 -05005797int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798{
5799 struct nfs_server *server = NFS_SERVER(inode);
5800 struct nfs4_exception exception = { };
5801 int err;
5802 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005803 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005804 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 switch (err) {
5806 case -NFS4ERR_STALE_STATEID:
5807 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 case 0:
5809 return 0;
5810 }
5811 err = nfs4_handle_exception(server, err, &exception);
5812 } while (exception.retry);
5813 return err;
5814}
5815
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5817{
5818 struct inode *inode = state->inode;
5819 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005820 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005821 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005823 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005825 struct nfs_lockt_res res = {
5826 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 };
5828 struct rpc_message msg = {
5829 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005830 .rpc_argp = &arg,
5831 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 .rpc_cred = state->owner->so_cred,
5833 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 struct nfs4_lock_state *lsp;
5835 int status;
5836
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005837 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005838 status = nfs4_set_lock_state(state, request);
5839 if (status != 0)
5840 goto out;
5841 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005842 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005843 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005844 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005845 switch (status) {
5846 case 0:
5847 request->fl_type = F_UNLCK;
5848 break;
5849 case -NFS4ERR_DENIED:
5850 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005852 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005853 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005854out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 return status;
5856}
5857
5858static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5859{
5860 struct nfs4_exception exception = { };
5861 int err;
5862
5863 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005864 err = _nfs4_proc_getlk(state, cmd, request);
5865 trace_nfs4_get_lock(request, state, cmd, err);
5866 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 &exception);
5868 } while (exception.retry);
5869 return err;
5870}
5871
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005872struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005873 struct nfs_locku_args arg;
5874 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005875 struct nfs4_lock_state *lsp;
5876 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005877 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005878 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005879 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005880 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005881};
5882
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005883static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5884 struct nfs_open_context *ctx,
5885 struct nfs4_lock_state *lsp,
5886 struct nfs_seqid *seqid)
5887{
5888 struct nfs4_unlockdata *p;
5889 struct inode *inode = lsp->ls_state->inode;
5890
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005891 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005892 if (p == NULL)
5893 return NULL;
5894 p->arg.fh = NFS_FH(inode);
5895 p->arg.fl = &p->fl;
5896 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005897 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005898 p->lsp = lsp;
5899 atomic_inc(&lsp->ls_count);
5900 /* Ensure we don't close file until we're done freeing locks! */
5901 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005902 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005903 memcpy(&p->fl, fl, sizeof(p->fl));
5904 p->server = NFS_SERVER(inode);
5905 return p;
5906}
5907
Trond Myklebust06f814a2006-01-03 09:55:07 +01005908static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005909{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005910 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005911 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005912 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005913 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005914 put_nfs_open_context(calldata->ctx);
5915 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005916}
5917
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005918static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005919{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005920 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005921
Trond Myklebust14516c32010-07-31 14:29:06 -04005922 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5923 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005924 switch (task->tk_status) {
5925 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005926 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005927 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005928 if (nfs4_update_lock_stateid(calldata->lsp,
5929 &calldata->res.stateid))
5930 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005931 case -NFS4ERR_ADMIN_REVOKED:
5932 case -NFS4ERR_EXPIRED:
5933 nfs4_free_revoked_stateid(calldata->server,
5934 &calldata->arg.stateid,
5935 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005936 case -NFS4ERR_BAD_STATEID:
5937 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005938 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005939 if (!nfs4_stateid_match(&calldata->arg.stateid,
5940 &calldata->lsp->ls_stateid))
5941 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005942 break;
5943 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005944 if (nfs4_async_handle_error(task, calldata->server,
5945 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005946 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005947 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005948 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005949}
5950
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005951static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005952{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005953 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005955 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
5956 nfs_async_iocounter_wait(task, calldata->l_ctx))
5957 return;
5958
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005959 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005960 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005961 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005962 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005963 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005964 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005965 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005966 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005967 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04005968 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005969 &calldata->res.seq_res,
5970 task) != 0)
5971 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005972 return;
5973out_no_action:
5974 task->tk_action = NULL;
5975out_wait:
5976 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977}
5978
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005979static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005980 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005981 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005982 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005983};
5984
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005985static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5986 struct nfs_open_context *ctx,
5987 struct nfs4_lock_state *lsp,
5988 struct nfs_seqid *seqid)
5989{
5990 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005991 struct rpc_message msg = {
5992 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5993 .rpc_cred = ctx->cred,
5994 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005995 struct rpc_task_setup task_setup_data = {
5996 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005997 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005998 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005999 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006000 .flags = RPC_TASK_ASYNC,
6001 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006002
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006003 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6004 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6005
Frank Filz137d6ac2007-07-09 15:32:29 -07006006 /* Ensure this is an unlock - when canceling a lock, the
6007 * canceled lock is passed in, and it won't be an unlock.
6008 */
6009 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006010 if (fl->fl_flags & FL_CLOSE)
6011 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006012
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006013 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6014 if (data == NULL) {
6015 nfs_free_seqid(seqid);
6016 return ERR_PTR(-ENOMEM);
6017 }
6018
Chuck Levera9c92d62013-08-09 12:48:18 -04006019 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006020 msg.rpc_argp = &data->arg;
6021 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006022 task_setup_data.callback_data = data;
6023 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006024}
6025
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6027{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006028 struct inode *inode = state->inode;
6029 struct nfs4_state_owner *sp = state->owner;
6030 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006031 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006032 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006033 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006034 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006035 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006036 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037
Trond Myklebust9b073572006-06-29 16:38:34 -04006038 status = nfs4_set_lock_state(state, request);
6039 /* Unlock _before_ we do the RPC call */
6040 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006041 /* Exclude nfs_delegation_claim_locks() */
6042 mutex_lock(&sp->so_delegreturn_mutex);
6043 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006044 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006045 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006046 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006047 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006048 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006049 }
6050 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006051 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006052 if (status != 0)
6053 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006054 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006055 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006056 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6057 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006058 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6059 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006060 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006061 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006062 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006063 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006064 status = PTR_ERR(task);
6065 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006066 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006067 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006068 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006069out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006070 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006071 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006072 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073}
6074
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006075struct nfs4_lockdata {
6076 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006077 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006078 struct nfs4_lock_state *lsp;
6079 struct nfs_open_context *ctx;
6080 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006081 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006082 int rpc_status;
6083 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006084 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006085};
6086
6087static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006088 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6089 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006090{
6091 struct nfs4_lockdata *p;
6092 struct inode *inode = lsp->ls_state->inode;
6093 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006094 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006095
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006096 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006097 if (p == NULL)
6098 return NULL;
6099
6100 p->arg.fh = NFS_FH(inode);
6101 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006102 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006103 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006104 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006105 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6106 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006107 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006108 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006109 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006110 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006111 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006112 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006113 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006114 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006115 atomic_inc(&lsp->ls_count);
6116 p->ctx = get_nfs_open_context(ctx);
6117 memcpy(&p->fl, fl, sizeof(p->fl));
6118 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006119out_free_seqid:
6120 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006121out_free:
6122 kfree(p);
6123 return NULL;
6124}
6125
6126static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6127{
6128 struct nfs4_lockdata *data = calldata;
6129 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130
Harvey Harrison3110ff82008-05-02 13:42:44 -07006131 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006132 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006133 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006134 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006135 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006136 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006137 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006138 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006139 nfs4_stateid_copy(&data->arg.open_stateid,
6140 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006141 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006142 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006143 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006144 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006145 nfs4_stateid_copy(&data->arg.lock_stateid,
6146 &data->lsp->ls_stateid);
6147 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006148 if (!nfs4_valid_open_stateid(state)) {
6149 data->rpc_status = -EBADF;
6150 task->tk_action = NULL;
6151 goto out_release_open_seqid;
6152 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006153 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006154 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006155 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006156 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006157 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006158 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006159out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006160 nfs_release_seqid(data->arg.open_seqid);
6161out_release_lock_seqid:
6162 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006163out_wait:
6164 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006165 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006166}
6167
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006168static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6169{
6170 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006171 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006172
Harvey Harrison3110ff82008-05-02 13:42:44 -07006173 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006174
Trond Myklebust14516c32010-07-31 14:29:06 -04006175 if (!nfs4_sequence_done(task, &data->res.seq_res))
6176 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006177
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006178 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006179 switch (task->tk_status) {
6180 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006181 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006182 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006183 if (data->arg.new_lock) {
6184 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006185 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006186 rpc_restart_call_prepare(task);
6187 break;
6188 }
6189 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006190 if (data->arg.new_lock_owner != 0) {
6191 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6192 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6193 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6194 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6195 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006196 break;
6197 case -NFS4ERR_BAD_STATEID:
6198 case -NFS4ERR_OLD_STATEID:
6199 case -NFS4ERR_STALE_STATEID:
6200 case -NFS4ERR_EXPIRED:
6201 if (data->arg.new_lock_owner != 0) {
6202 if (!nfs4_stateid_match(&data->arg.open_stateid,
6203 &lsp->ls_state->open_stateid))
6204 rpc_restart_call_prepare(task);
6205 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6206 &lsp->ls_stateid))
6207 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006208 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006209 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006210}
6211
6212static void nfs4_lock_release(void *calldata)
6213{
6214 struct nfs4_lockdata *data = calldata;
6215
Harvey Harrison3110ff82008-05-02 13:42:44 -07006216 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006217 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006218 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006219 struct rpc_task *task;
6220 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6221 data->arg.lock_seqid);
6222 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006223 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006224 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006225 } else
6226 nfs_free_seqid(data->arg.lock_seqid);
6227 nfs4_put_lock_state(data->lsp);
6228 put_nfs_open_context(data->ctx);
6229 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006230 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006231}
6232
6233static const struct rpc_call_ops nfs4_lock_ops = {
6234 .rpc_call_prepare = nfs4_lock_prepare,
6235 .rpc_call_done = nfs4_lock_done,
6236 .rpc_release = nfs4_lock_release,
6237};
6238
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006239static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6240{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006241 switch (error) {
6242 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006243 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006244 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006245 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006246 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006247 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006248 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006249 break;
6250 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006251 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006252 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006253 };
6254}
6255
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006256static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006257{
6258 struct nfs4_lockdata *data;
6259 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006260 struct rpc_message msg = {
6261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6262 .rpc_cred = state->owner->so_cred,
6263 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006264 struct rpc_task_setup task_setup_data = {
6265 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006266 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006267 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006268 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006269 .flags = RPC_TASK_ASYNC,
6270 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006271 int ret;
6272
Harvey Harrison3110ff82008-05-02 13:42:44 -07006273 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006274 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006275 fl->fl_u.nfs4_fl.owner,
6276 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006277 if (data == NULL)
6278 return -ENOMEM;
6279 if (IS_SETLKW(cmd))
6280 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006281 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006282 msg.rpc_argp = &data->arg;
6283 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006284 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006285 if (recovery_type > NFS_LOCK_NEW) {
6286 if (recovery_type == NFS_LOCK_RECLAIM)
6287 data->arg.reclaim = NFS_LOCK_RECLAIM;
6288 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006289 } else
6290 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006291 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006292 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006293 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006294 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006295 if (ret == 0) {
6296 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006297 if (ret)
6298 nfs4_handle_setlk_error(data->server, data->lsp,
6299 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006300 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006301 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006302 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006303 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006304 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006305 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306}
6307
6308static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6309{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006310 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006311 struct nfs4_exception exception = {
6312 .inode = state->inode,
6313 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006314 int err;
6315
6316 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006317 /* Cache the lock if possible... */
6318 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6319 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006320 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006321 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006322 break;
6323 nfs4_handle_exception(server, err, &exception);
6324 } while (exception.retry);
6325 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326}
6327
6328static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6329{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006330 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006331 struct nfs4_exception exception = {
6332 .inode = state->inode,
6333 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006334 int err;
6335
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006336 err = nfs4_set_lock_state(state, request);
6337 if (err != 0)
6338 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006339 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006340 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6341 return 0;
6342 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006343 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006344 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6345 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006346 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006347 switch (err) {
6348 default:
6349 goto out;
6350 case -NFS4ERR_GRACE:
6351 case -NFS4ERR_DELAY:
6352 nfs4_handle_exception(server, err, &exception);
6353 err = 0;
6354 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006355 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006356out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006357 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358}
6359
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006360#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006361static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6362{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006363 struct nfs4_lock_state *lsp;
6364 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006365
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006366 status = nfs4_set_lock_state(state, request);
6367 if (status != 0)
6368 return status;
6369 lsp = request->fl_u.nfs4_fl.owner;
6370 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6371 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6372 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006373 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006374}
6375#endif
6376
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6378{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006379 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006380 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006381 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006382 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383
Trond Myklebust01c3b862006-06-29 16:38:39 -04006384 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006385 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006386 if (status < 0)
6387 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006388 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006389 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006390 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006391 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006392 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006393 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006394 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006395 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006396 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006397 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006398 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006399 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006400 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006401 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006402out:
6403 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 return status;
6405}
6406
6407static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6408{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006409 struct nfs4_exception exception = {
6410 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006411 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006412 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413 int err;
6414
6415 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006416 err = _nfs4_proc_setlk(state, cmd, request);
6417 if (err == -NFS4ERR_DENIED)
6418 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006420 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 } while (exception.retry);
6422 return err;
6423}
6424
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006425#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6426#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6427
6428static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006429nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6430 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006431{
6432 int status = -ERESTARTSYS;
6433 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6434
6435 while(!signalled()) {
6436 status = nfs4_proc_setlk(state, cmd, request);
6437 if ((status != -EAGAIN) || IS_SETLK(cmd))
6438 break;
6439 freezable_schedule_timeout_interruptible(timeout);
6440 timeout *= 2;
6441 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6442 status = -ERESTARTSYS;
6443 }
6444 return status;
6445}
6446
Jeff Laytona1d617d82016-09-17 18:17:39 -04006447#ifdef CONFIG_NFS_V4_1
6448struct nfs4_lock_waiter {
6449 struct task_struct *task;
6450 struct inode *inode;
6451 struct nfs_lowner *owner;
6452 bool notified;
6453};
6454
6455static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006456nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006457{
6458 int ret;
6459 struct cb_notify_lock_args *cbnl = key;
6460 struct nfs4_lock_waiter *waiter = wait->private;
6461 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6462 *wowner = waiter->owner;
6463
6464 /* Only wake if the callback was for the same owner */
6465 if (lowner->clientid != wowner->clientid ||
6466 lowner->id != wowner->id ||
6467 lowner->s_dev != wowner->s_dev)
6468 return 0;
6469
6470 /* Make sure it's for the right inode */
6471 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6472 return 0;
6473
6474 waiter->notified = true;
6475
6476 /* override "private" so we can use default_wake_function */
6477 wait->private = waiter->task;
6478 ret = autoremove_wake_function(wait, mode, flags, key);
6479 wait->private = waiter;
6480 return ret;
6481}
6482
6483static int
6484nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6485{
6486 int status = -ERESTARTSYS;
6487 unsigned long flags;
6488 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6489 struct nfs_server *server = NFS_SERVER(state->inode);
6490 struct nfs_client *clp = server->nfs_client;
6491 wait_queue_head_t *q = &clp->cl_lock_waitq;
6492 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6493 .id = lsp->ls_seqid.owner_id,
6494 .s_dev = server->s_dev };
6495 struct nfs4_lock_waiter waiter = { .task = current,
6496 .inode = state->inode,
6497 .owner = &owner,
6498 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006499 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006500
6501 /* Don't bother with waitqueue if we don't expect a callback */
6502 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6503 return nfs4_retry_setlk_simple(state, cmd, request);
6504
6505 init_wait(&wait);
6506 wait.private = &waiter;
6507 wait.func = nfs4_wake_lock_waiter;
6508 add_wait_queue(q, &wait);
6509
6510 while(!signalled()) {
6511 status = nfs4_proc_setlk(state, cmd, request);
6512 if ((status != -EAGAIN) || IS_SETLK(cmd))
6513 break;
6514
6515 status = -ERESTARTSYS;
6516 spin_lock_irqsave(&q->lock, flags);
6517 if (waiter.notified) {
6518 spin_unlock_irqrestore(&q->lock, flags);
6519 continue;
6520 }
6521 set_current_state(TASK_INTERRUPTIBLE);
6522 spin_unlock_irqrestore(&q->lock, flags);
6523
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006524 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006525 }
6526
6527 finish_wait(q, &wait);
6528 return status;
6529}
6530#else /* !CONFIG_NFS_V4_1 */
6531static inline int
6532nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6533{
6534 return nfs4_retry_setlk_simple(state, cmd, request);
6535}
6536#endif
6537
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538static int
6539nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6540{
6541 struct nfs_open_context *ctx;
6542 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543 int status;
6544
6545 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006546 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547 state = ctx->state;
6548
Trond Myklebustd9531262009-07-21 19:22:38 -04006549 if (IS_GETLK(cmd)) {
6550 if (state != NULL)
6551 return nfs4_proc_getlk(state, F_GETLK, request);
6552 return 0;
6553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554
6555 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6556 return -EINVAL;
6557
Trond Myklebustd9531262009-07-21 19:22:38 -04006558 if (request->fl_type == F_UNLCK) {
6559 if (state != NULL)
6560 return nfs4_proc_unlck(state, cmd, request);
6561 return 0;
6562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563
Trond Myklebustd9531262009-07-21 19:22:38 -04006564 if (state == NULL)
6565 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006566
6567 if ((request->fl_flags & FL_POSIX) &&
6568 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6569 return -ENOLCK;
6570
Jeff Layton1ea67db2016-09-17 18:17:37 -04006571 status = nfs4_set_lock_state(state, request);
6572 if (status != 0)
6573 return status;
6574
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006575 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576}
6577
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006578int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006579{
6580 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006581 int err;
6582
6583 err = nfs4_set_lock_state(state, fl);
6584 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006585 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006586 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006587 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006588}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006589
Trond Myklebustcf470c32012-03-07 13:49:12 -05006590struct nfs_release_lockowner_data {
6591 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006592 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006593 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006594 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006595 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006596};
6597
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006598static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6599{
6600 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006601 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006602 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6603 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006604 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006605 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006606}
6607
6608static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6609{
6610 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006611 struct nfs_server *server = data->server;
6612
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006613 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006614
6615 switch (task->tk_status) {
6616 case 0:
6617 renew_lease(server, data->timestamp);
6618 break;
6619 case -NFS4ERR_STALE_CLIENTID:
6620 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006621 nfs4_schedule_lease_recovery(server->nfs_client);
6622 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006623 case -NFS4ERR_LEASE_MOVED:
6624 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006625 if (nfs4_async_handle_error(task, server,
6626 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006627 rpc_restart_call_prepare(task);
6628 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006629}
6630
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006631static void nfs4_release_lockowner_release(void *calldata)
6632{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006633 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006634 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006635 kfree(calldata);
6636}
6637
Trond Myklebust17280172012-03-11 13:11:00 -04006638static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006639 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6640 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006641 .rpc_release = nfs4_release_lockowner_release,
6642};
6643
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006644static void
6645nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006646{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006647 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006648 struct rpc_message msg = {
6649 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6650 };
6651
6652 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006653 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006654
Trond Myklebustcf470c32012-03-07 13:49:12 -05006655 data = kmalloc(sizeof(*data), GFP_NOFS);
6656 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006657 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006658 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006659 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006660 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6661 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6662 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006663
Trond Myklebustcf470c32012-03-07 13:49:12 -05006664 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006665 msg.rpc_resp = &data->res;
6666 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006667 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006668}
6669
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006670#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6671
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006672static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006673 struct dentry *unused, struct inode *inode,
6674 const char *key, const void *buf,
6675 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006676{
Al Viro59301222016-05-27 10:19:30 -04006677 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006678}
6679
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006680static int nfs4_xattr_get_nfs4_acl(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)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006683{
Al Virob2968212016-04-10 20:48:24 -04006684 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006685}
6686
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006687static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006688{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006689 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006690}
6691
David Quigleyc9bccef2013-05-22 12:50:45 -04006692#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006693
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006694static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006695 struct dentry *unused, struct inode *inode,
6696 const char *key, const void *buf,
6697 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006698{
6699 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006700 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006701
6702 return -EOPNOTSUPP;
6703}
6704
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006705static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006706 struct dentry *unused, struct inode *inode,
6707 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006708{
6709 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006710 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006711 return -EOPNOTSUPP;
6712}
6713
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006714static ssize_t
6715nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006716{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006717 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006718
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006719 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6720 len = security_inode_listsecurity(inode, list, list_len);
6721 if (list_len && len > list_len)
6722 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006723 }
6724 return len;
6725}
6726
6727static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6728 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006729 .get = nfs4_xattr_get_nfs4_label,
6730 .set = nfs4_xattr_set_nfs4_label,
6731};
David Quigleyc9bccef2013-05-22 12:50:45 -04006732
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006733#else
6734
6735static ssize_t
6736nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6737{
6738 return 0;
6739}
6740
6741#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006742
Andy Adamson533eb462011-06-13 18:25:56 -04006743/*
6744 * nfs_fhget will use either the mounted_on_fileid or the fileid
6745 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006746static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6747{
Andy Adamson533eb462011-06-13 18:25:56 -04006748 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6749 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6750 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006751 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006752 return;
6753
6754 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006755 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006756 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6757 fattr->nlink = 2;
6758}
6759
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006760static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6761 const struct qstr *name,
6762 struct nfs4_fs_locations *fs_locations,
6763 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006764{
6765 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006766 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006767 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006768 };
6769 struct nfs4_fs_locations_arg args = {
6770 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006771 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006772 .page = page,
6773 .bitmask = bitmask,
6774 };
Benny Halevy22958462009-04-01 09:22:02 -04006775 struct nfs4_fs_locations_res res = {
6776 .fs_locations = fs_locations,
6777 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006778 struct rpc_message msg = {
6779 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6780 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006781 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006782 };
6783 int status;
6784
Harvey Harrison3110ff82008-05-02 13:42:44 -07006785 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006786
6787 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6788 * is not supported */
6789 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6790 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6791 else
6792 bitmask[0] |= FATTR4_WORD0_FILEID;
6793
Trond Myklebustc228fd32007-01-13 02:28:11 -05006794 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006795 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006796 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006797 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006798 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006799 return status;
6800}
6801
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006802int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6803 const struct qstr *name,
6804 struct nfs4_fs_locations *fs_locations,
6805 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006806{
6807 struct nfs4_exception exception = { };
6808 int err;
6809 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006810 err = _nfs4_proc_fs_locations(client, dir, name,
6811 fs_locations, page);
6812 trace_nfs4_get_fs_locations(dir, name, err);
6813 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006814 &exception);
6815 } while (exception.retry);
6816 return err;
6817}
6818
Chuck Leverb03d7352013-10-17 14:12:50 -04006819/*
6820 * This operation also signals the server that this client is
6821 * performing migration recovery. The server can stop returning
6822 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6823 * appended to this compound to identify the client ID which is
6824 * performing recovery.
6825 */
6826static int _nfs40_proc_get_locations(struct inode *inode,
6827 struct nfs4_fs_locations *locations,
6828 struct page *page, struct rpc_cred *cred)
6829{
6830 struct nfs_server *server = NFS_SERVER(inode);
6831 struct rpc_clnt *clnt = server->client;
6832 u32 bitmask[2] = {
6833 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6834 };
6835 struct nfs4_fs_locations_arg args = {
6836 .clientid = server->nfs_client->cl_clientid,
6837 .fh = NFS_FH(inode),
6838 .page = page,
6839 .bitmask = bitmask,
6840 .migration = 1, /* skip LOOKUP */
6841 .renew = 1, /* append RENEW */
6842 };
6843 struct nfs4_fs_locations_res res = {
6844 .fs_locations = locations,
6845 .migration = 1,
6846 .renew = 1,
6847 };
6848 struct rpc_message msg = {
6849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6850 .rpc_argp = &args,
6851 .rpc_resp = &res,
6852 .rpc_cred = cred,
6853 };
6854 unsigned long now = jiffies;
6855 int status;
6856
6857 nfs_fattr_init(&locations->fattr);
6858 locations->server = server;
6859 locations->nlocations = 0;
6860
6861 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6862 nfs4_set_sequence_privileged(&args.seq_args);
6863 status = nfs4_call_sync_sequence(clnt, server, &msg,
6864 &args.seq_args, &res.seq_res);
6865 if (status)
6866 return status;
6867
6868 renew_lease(server, now);
6869 return 0;
6870}
6871
6872#ifdef CONFIG_NFS_V4_1
6873
6874/*
6875 * This operation also signals the server that this client is
6876 * performing migration recovery. The server can stop asserting
6877 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6878 * performing this operation is identified in the SEQUENCE
6879 * operation in this compound.
6880 *
6881 * When the client supports GETATTR(fs_locations_info), it can
6882 * be plumbed in here.
6883 */
6884static int _nfs41_proc_get_locations(struct inode *inode,
6885 struct nfs4_fs_locations *locations,
6886 struct page *page, struct rpc_cred *cred)
6887{
6888 struct nfs_server *server = NFS_SERVER(inode);
6889 struct rpc_clnt *clnt = server->client;
6890 u32 bitmask[2] = {
6891 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6892 };
6893 struct nfs4_fs_locations_arg args = {
6894 .fh = NFS_FH(inode),
6895 .page = page,
6896 .bitmask = bitmask,
6897 .migration = 1, /* skip LOOKUP */
6898 };
6899 struct nfs4_fs_locations_res res = {
6900 .fs_locations = locations,
6901 .migration = 1,
6902 };
6903 struct rpc_message msg = {
6904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6905 .rpc_argp = &args,
6906 .rpc_resp = &res,
6907 .rpc_cred = cred,
6908 };
6909 int status;
6910
6911 nfs_fattr_init(&locations->fattr);
6912 locations->server = server;
6913 locations->nlocations = 0;
6914
6915 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6916 nfs4_set_sequence_privileged(&args.seq_args);
6917 status = nfs4_call_sync_sequence(clnt, server, &msg,
6918 &args.seq_args, &res.seq_res);
6919 if (status == NFS4_OK &&
6920 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6921 status = -NFS4ERR_LEASE_MOVED;
6922 return status;
6923}
6924
6925#endif /* CONFIG_NFS_V4_1 */
6926
6927/**
6928 * nfs4_proc_get_locations - discover locations for a migrated FSID
6929 * @inode: inode on FSID that is migrating
6930 * @locations: result of query
6931 * @page: buffer
6932 * @cred: credential to use for this operation
6933 *
6934 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6935 * operation failed, or a negative errno if a local error occurred.
6936 *
6937 * On success, "locations" is filled in, but if the server has
6938 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6939 * asserted.
6940 *
6941 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6942 * from this client that require migration recovery.
6943 */
6944int nfs4_proc_get_locations(struct inode *inode,
6945 struct nfs4_fs_locations *locations,
6946 struct page *page, struct rpc_cred *cred)
6947{
6948 struct nfs_server *server = NFS_SERVER(inode);
6949 struct nfs_client *clp = server->nfs_client;
6950 const struct nfs4_mig_recovery_ops *ops =
6951 clp->cl_mvops->mig_recovery_ops;
6952 struct nfs4_exception exception = { };
6953 int status;
6954
6955 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6956 (unsigned long long)server->fsid.major,
6957 (unsigned long long)server->fsid.minor,
6958 clp->cl_hostname);
6959 nfs_display_fhandle(NFS_FH(inode), __func__);
6960
6961 do {
6962 status = ops->get_locations(inode, locations, page, cred);
6963 if (status != -NFS4ERR_DELAY)
6964 break;
6965 nfs4_handle_exception(server, status, &exception);
6966 } while (exception.retry);
6967 return status;
6968}
6969
Chuck Lever44c99932013-10-17 14:13:30 -04006970/*
6971 * This operation also signals the server that this client is
6972 * performing "lease moved" recovery. The server can stop
6973 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6974 * is appended to this compound to identify the client ID which is
6975 * performing recovery.
6976 */
6977static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6978{
6979 struct nfs_server *server = NFS_SERVER(inode);
6980 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6981 struct rpc_clnt *clnt = server->client;
6982 struct nfs4_fsid_present_arg args = {
6983 .fh = NFS_FH(inode),
6984 .clientid = clp->cl_clientid,
6985 .renew = 1, /* append RENEW */
6986 };
6987 struct nfs4_fsid_present_res res = {
6988 .renew = 1,
6989 };
6990 struct rpc_message msg = {
6991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6992 .rpc_argp = &args,
6993 .rpc_resp = &res,
6994 .rpc_cred = cred,
6995 };
6996 unsigned long now = jiffies;
6997 int status;
6998
6999 res.fh = nfs_alloc_fhandle();
7000 if (res.fh == NULL)
7001 return -ENOMEM;
7002
7003 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7004 nfs4_set_sequence_privileged(&args.seq_args);
7005 status = nfs4_call_sync_sequence(clnt, server, &msg,
7006 &args.seq_args, &res.seq_res);
7007 nfs_free_fhandle(res.fh);
7008 if (status)
7009 return status;
7010
7011 do_renew_lease(clp, now);
7012 return 0;
7013}
7014
7015#ifdef CONFIG_NFS_V4_1
7016
7017/*
7018 * This operation also signals the server that this client is
7019 * performing "lease moved" recovery. The server can stop asserting
7020 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7021 * this operation is identified in the SEQUENCE operation in this
7022 * compound.
7023 */
7024static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7025{
7026 struct nfs_server *server = NFS_SERVER(inode);
7027 struct rpc_clnt *clnt = server->client;
7028 struct nfs4_fsid_present_arg args = {
7029 .fh = NFS_FH(inode),
7030 };
7031 struct nfs4_fsid_present_res res = {
7032 };
7033 struct rpc_message msg = {
7034 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7035 .rpc_argp = &args,
7036 .rpc_resp = &res,
7037 .rpc_cred = cred,
7038 };
7039 int status;
7040
7041 res.fh = nfs_alloc_fhandle();
7042 if (res.fh == NULL)
7043 return -ENOMEM;
7044
7045 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7046 nfs4_set_sequence_privileged(&args.seq_args);
7047 status = nfs4_call_sync_sequence(clnt, server, &msg,
7048 &args.seq_args, &res.seq_res);
7049 nfs_free_fhandle(res.fh);
7050 if (status == NFS4_OK &&
7051 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7052 status = -NFS4ERR_LEASE_MOVED;
7053 return status;
7054}
7055
7056#endif /* CONFIG_NFS_V4_1 */
7057
7058/**
7059 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7060 * @inode: inode on FSID to check
7061 * @cred: credential to use for this operation
7062 *
7063 * Server indicates whether the FSID is present, moved, or not
7064 * recognized. This operation is necessary to clear a LEASE_MOVED
7065 * condition for this client ID.
7066 *
7067 * Returns NFS4_OK if the FSID is present on this server,
7068 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7069 * NFS4ERR code if some error occurred on the server, or a
7070 * negative errno if a local failure occurred.
7071 */
7072int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7073{
7074 struct nfs_server *server = NFS_SERVER(inode);
7075 struct nfs_client *clp = server->nfs_client;
7076 const struct nfs4_mig_recovery_ops *ops =
7077 clp->cl_mvops->mig_recovery_ops;
7078 struct nfs4_exception exception = { };
7079 int status;
7080
7081 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7082 (unsigned long long)server->fsid.major,
7083 (unsigned long long)server->fsid.minor,
7084 clp->cl_hostname);
7085 nfs_display_fhandle(NFS_FH(inode), __func__);
7086
7087 do {
7088 status = ops->fsid_present(inode, cred);
7089 if (status != -NFS4ERR_DELAY)
7090 break;
7091 nfs4_handle_exception(server, status, &exception);
7092 } while (exception.retry);
7093 return status;
7094}
7095
Andy Adamson5ec16a82013-08-08 10:57:55 -04007096/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007097 * If 'use_integrity' is true and the state managment nfs_client
7098 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7099 * and the machine credential as per RFC3530bis and RFC5661 Security
7100 * Considerations sections. Otherwise, just use the user cred with the
7101 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007102 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007103static 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 +00007104{
7105 int status;
7106 struct nfs4_secinfo_arg args = {
7107 .dir_fh = NFS_FH(dir),
7108 .name = name,
7109 };
7110 struct nfs4_secinfo_res res = {
7111 .flavors = flavors,
7112 };
7113 struct rpc_message msg = {
7114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7115 .rpc_argp = &args,
7116 .rpc_resp = &res,
7117 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007118 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007119 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007120
7121 if (use_integrity) {
7122 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007123 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7124 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007125 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007126
7127 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007128
7129 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7130 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7131
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007132 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7133 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007134 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007135
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007136 if (cred)
7137 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007138
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007139 return status;
7140}
7141
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007142int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7143 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007144{
7145 struct nfs4_exception exception = { };
7146 int err;
7147 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007148 err = -NFS4ERR_WRONGSEC;
7149
7150 /* try to use integrity protection with machine cred */
7151 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7152 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7153
7154 /*
7155 * if unable to use integrity protection, or SECINFO with
7156 * integrity protection returns NFS4ERR_WRONGSEC (which is
7157 * disallowed by spec, but exists in deployed servers) use
7158 * the current filesystem's rpc_client and the user cred.
7159 */
7160 if (err == -NFS4ERR_WRONGSEC)
7161 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7162
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007163 trace_nfs4_secinfo(dir, name, err);
7164 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007165 &exception);
7166 } while (exception.retry);
7167 return err;
7168}
7169
Andy Adamson557134a2009-04-01 09:21:53 -04007170#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007171/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007172 * Check the exchange flags returned by the server for invalid flags, having
7173 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7174 * DS flags set.
7175 */
7176static int nfs4_check_cl_exchange_flags(u32 flags)
7177{
7178 if (flags & ~EXCHGID4_FLAG_MASK_R)
7179 goto out_inval;
7180 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7181 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7182 goto out_inval;
7183 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7184 goto out_inval;
7185 return NFS_OK;
7186out_inval:
7187 return -NFS4ERR_INVAL;
7188}
7189
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007190static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007191nfs41_same_server_scope(struct nfs41_server_scope *a,
7192 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007193{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007194 if (a->server_scope_sz != b->server_scope_sz)
7195 return false;
7196 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007197}
7198
Andy Adamson02a95de2016-02-05 16:08:37 -05007199static void
7200nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7201{
7202}
7203
7204static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7205 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7206};
7207
Andy Adamson357f54d2010-12-14 10:11:57 -05007208/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007209 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007210 *
7211 * The 4.1 client currently uses the same TCP connection for the
7212 * fore and backchannel.
7213 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007214static
7215int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7216 struct rpc_xprt *xprt,
7217 struct nfs_client *clp,
7218 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007219{
7220 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007221 struct nfs41_bind_conn_to_session_args args = {
7222 .client = clp,
7223 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7224 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007225 struct nfs41_bind_conn_to_session_res res;
7226 struct rpc_message msg = {
7227 .rpc_proc =
7228 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007229 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007230 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007231 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007232 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007233 struct rpc_task_setup task_setup_data = {
7234 .rpc_client = clnt,
7235 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007236 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007237 .rpc_message = &msg,
7238 .flags = RPC_TASK_TIMEOUT,
7239 };
7240 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007241
Trond Myklebust71a097c2015-02-18 09:27:18 -08007242 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7243 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7244 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007245
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007246 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7247 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7248 args.dir = NFS4_CDFC4_FORE;
7249
7250 task = rpc_run_task(&task_setup_data);
7251 if (!IS_ERR(task)) {
7252 status = task->tk_status;
7253 rpc_put_task(task);
7254 } else
7255 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007256 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007257 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007258 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007259 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7260 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007261 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007262 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007263 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007264 dprintk("NFS: %s: Unexpected direction from server\n",
7265 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007266 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007267 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007268 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007269 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7270 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007271 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007272 }
7273 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007274
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007275 return status;
7276}
7277
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007278struct rpc_bind_conn_calldata {
7279 struct nfs_client *clp;
7280 struct rpc_cred *cred;
7281};
7282
7283static int
7284nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7285 struct rpc_xprt *xprt,
7286 void *calldata)
7287{
7288 struct rpc_bind_conn_calldata *p = calldata;
7289
7290 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7291}
7292
7293int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7294{
7295 struct rpc_bind_conn_calldata data = {
7296 .clp = clp,
7297 .cred = cred,
7298 };
7299 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7300 nfs4_proc_bind_conn_to_session_callback, &data);
7301}
7302
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007303/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007304 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7305 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007306 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007307static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7308 .how = SP4_MACH_CRED,
7309 .enforce.u.words = {
7310 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7311 1 << (OP_EXCHANGE_ID - 32) |
7312 1 << (OP_CREATE_SESSION - 32) |
7313 1 << (OP_DESTROY_SESSION - 32) |
7314 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007315 },
7316 .allow.u.words = {
7317 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007318 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007319 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007320 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007321 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007322 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007323 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007324 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007325 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007326 1 << (OP_FREE_STATEID - 32) |
7327 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007328 }
7329};
7330
7331/*
7332 * Select the state protection mode for client `clp' given the server results
7333 * from exchange_id in `sp'.
7334 *
7335 * Returns 0 on success, negative errno otherwise.
7336 */
7337static int nfs4_sp4_select_mode(struct nfs_client *clp,
7338 struct nfs41_state_protection *sp)
7339{
7340 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7341 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7342 1 << (OP_EXCHANGE_ID - 32) |
7343 1 << (OP_CREATE_SESSION - 32) |
7344 1 << (OP_DESTROY_SESSION - 32) |
7345 1 << (OP_DESTROY_CLIENTID - 32)
7346 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007347 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007348 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007349 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007350
7351 if (sp->how == SP4_MACH_CRED) {
7352 /* Print state protect result */
7353 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7354 for (i = 0; i <= LAST_NFS4_OP; i++) {
7355 if (test_bit(i, sp->enforce.u.longs))
7356 dfprintk(MOUNT, " enforce op %d\n", i);
7357 if (test_bit(i, sp->allow.u.longs))
7358 dfprintk(MOUNT, " allow op %d\n", i);
7359 }
7360
7361 /* make sure nothing is on enforce list that isn't supported */
7362 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7363 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7364 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007365 ret = -EINVAL;
7366 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007367 }
7368 }
7369
7370 /*
7371 * Minimal mode - state operations are allowed to use machine
7372 * credential. Note this already happens by default, so the
7373 * client doesn't have to do anything more than the negotiation.
7374 *
7375 * NOTE: we don't care if EXCHANGE_ID is in the list -
7376 * we're already using the machine cred for exchange_id
7377 * and will never use a different cred.
7378 */
7379 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7380 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7381 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7382 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7383 dfprintk(MOUNT, "sp4_mach_cred:\n");
7384 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007385 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007386 } else {
7387 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007388 ret = -EINVAL;
7389 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007390 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007391
7392 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007393 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7394 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007395 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7396 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007397 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007398 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007399
Andrew Elble99ade3c2015-12-02 09:39:51 -05007400 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7401 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007402 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007403 }
7404
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007405 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7406 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7407 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007408 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007409 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007410
7411 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7412 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7413 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007414 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007415 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007416
7417 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7418 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007419 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007420 }
7421
7422 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7423 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007424 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007425 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007426 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007427out:
7428 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007429 return 0;
7430}
7431
Andy Adamson8d89bd72016-09-09 09:22:18 -04007432struct nfs41_exchange_id_data {
7433 struct nfs41_exchange_id_res res;
7434 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007435};
7436
Andy Adamson8d89bd72016-09-09 09:22:18 -04007437static void nfs4_exchange_id_release(void *data)
7438{
7439 struct nfs41_exchange_id_data *cdata =
7440 (struct nfs41_exchange_id_data *)data;
7441
Olga Kornievskaia63513232017-03-13 10:36:19 -04007442 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007443 kfree(cdata->res.impl_id);
7444 kfree(cdata->res.server_scope);
7445 kfree(cdata->res.server_owner);
7446 kfree(cdata);
7447}
7448
7449static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007450 .rpc_release = nfs4_exchange_id_release,
7451};
7452
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007453/*
7454 * _nfs4_proc_exchange_id()
7455 *
7456 * Wrapper for EXCHANGE_ID operation.
7457 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007458static struct rpc_task *
7459nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007460 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007461{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007462 struct rpc_message msg = {
7463 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007464 .rpc_cred = cred,
7465 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007466 struct rpc_task_setup task_setup_data = {
7467 .rpc_client = clp->cl_rpcclient,
7468 .callback_ops = &nfs4_exchange_id_call_ops,
7469 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007470 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007471 };
7472 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007473 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007474
7475 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007476 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007477
Trond Myklebust9c760d12017-07-31 18:38:50 -04007478 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007479 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007480 if (!calldata)
7481 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007482
Trond Myklebustfd405592017-08-01 16:02:47 -04007483 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007484
7485 status = nfs4_init_uniform_client_string(clp);
7486 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007487 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007488
Andy Adamson8d89bd72016-09-09 09:22:18 -04007489 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7490 GFP_NOFS);
7491 status = -ENOMEM;
7492 if (unlikely(calldata->res.server_owner == NULL))
7493 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007494
Andy Adamson8d89bd72016-09-09 09:22:18 -04007495 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007496 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007497 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007498 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007499
Andy Adamson8d89bd72016-09-09 09:22:18 -04007500 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7501 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007502 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007503
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007504 switch (sp4_how) {
7505 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007506 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007507 break;
7508
7509 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007510 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007511 break;
7512
7513 default:
7514 /* unsupported! */
7515 WARN_ON_ONCE(1);
7516 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007517 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007518 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007519 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007520 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007521 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007522 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7523 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007524 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007525 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007526 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7527 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007528#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007529 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007530#endif
7531 msg.rpc_argp = &calldata->args;
7532 msg.rpc_resp = &calldata->res;
7533 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007534
Trond Myklebust9c760d12017-07-31 18:38:50 -04007535 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007536
7537out_impl_id:
7538 kfree(calldata->res.impl_id);
7539out_server_scope:
7540 kfree(calldata->res.server_scope);
7541out_server_owner:
7542 kfree(calldata->res.server_owner);
7543out_calldata:
7544 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007545out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007546 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007547 return ERR_PTR(status);
7548}
7549
7550/*
7551 * _nfs4_proc_exchange_id()
7552 *
7553 * Wrapper for EXCHANGE_ID operation.
7554 */
7555static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7556 u32 sp4_how)
7557{
7558 struct rpc_task *task;
7559 struct nfs41_exchange_id_args *argp;
7560 struct nfs41_exchange_id_res *resp;
7561 int status;
7562
7563 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7564 if (IS_ERR(task))
7565 return PTR_ERR(task);
7566
7567 argp = task->tk_msg.rpc_argp;
7568 resp = task->tk_msg.rpc_resp;
7569 status = task->tk_status;
7570 if (status != 0)
7571 goto out;
7572
7573 status = nfs4_check_cl_exchange_flags(resp->flags);
7574 if (status != 0)
7575 goto out;
7576
7577 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7578 if (status != 0)
7579 goto out;
7580
7581 clp->cl_clientid = resp->clientid;
7582 clp->cl_exchange_flags = resp->flags;
7583 clp->cl_seqid = resp->seqid;
7584 /* Client ID is not confirmed */
7585 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7586 clear_bit(NFS4_SESSION_ESTABLISHED,
7587 &clp->cl_session->session_state);
7588
7589 if (clp->cl_serverscope != NULL &&
7590 !nfs41_same_server_scope(clp->cl_serverscope,
7591 resp->server_scope)) {
7592 dprintk("%s: server_scope mismatch detected\n",
7593 __func__);
7594 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7595 }
7596
7597 swap(clp->cl_serverowner, resp->server_owner);
7598 swap(clp->cl_serverscope, resp->server_scope);
7599 swap(clp->cl_implid, resp->impl_id);
7600
7601 /* Save the EXCHANGE_ID verifier session trunk tests */
7602 memcpy(clp->cl_confirm.data, argp->verifier.data,
7603 sizeof(clp->cl_confirm.data));
7604out:
7605 trace_nfs4_exchange_id(clp, status);
7606 rpc_put_task(task);
7607 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007608}
7609
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007610/*
7611 * nfs4_proc_exchange_id()
7612 *
7613 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7614 *
7615 * Since the clientid has expired, all compounds using sessions
7616 * associated with the stale clientid will be returning
7617 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7618 * be in some phase of session reset.
7619 *
7620 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7621 */
7622int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7623{
7624 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7625 int status;
7626
7627 /* try SP4_MACH_CRED if krb5i/p */
7628 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7629 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007630 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007631 if (!status)
7632 return 0;
7633 }
7634
7635 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007636 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007637}
7638
Andy Adamson04fa2c62016-09-09 09:22:29 -04007639/**
7640 * nfs4_test_session_trunk
7641 *
7642 * This is an add_xprt_test() test function called from
7643 * rpc_clnt_setup_test_and_add_xprt.
7644 *
7645 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7646 * and is dereferrenced in nfs4_exchange_id_release
7647 *
7648 * Upon success, add the new transport to the rpc_clnt
7649 *
7650 * @clnt: struct rpc_clnt to get new transport
7651 * @xprt: the rpc_xprt to test
7652 * @data: call data for _nfs4_proc_exchange_id.
7653 */
7654int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7655 void *data)
7656{
7657 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007658 struct rpc_task *task;
7659 int status;
7660
Andy Adamson04fa2c62016-09-09 09:22:29 -04007661 u32 sp4_how;
7662
7663 dprintk("--> %s try %s\n", __func__,
7664 xprt->address_strings[RPC_DISPLAY_ADDR]);
7665
7666 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7667
7668 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007669 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7670 if (IS_ERR(task))
7671 return PTR_ERR(task);
7672
7673 status = task->tk_status;
7674 if (status == 0)
7675 status = nfs4_detect_session_trunking(adata->clp,
7676 task->tk_msg.rpc_resp, xprt);
7677
7678 rpc_put_task(task);
7679 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007680}
7681EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7682
Trond Myklebust66245532012-05-25 17:18:09 -04007683static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7684 struct rpc_cred *cred)
7685{
7686 struct rpc_message msg = {
7687 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7688 .rpc_argp = clp,
7689 .rpc_cred = cred,
7690 };
7691 int status;
7692
7693 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007694 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007695 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007696 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007697 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7698 return status;
7699}
7700
7701static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7702 struct rpc_cred *cred)
7703{
7704 unsigned int loop;
7705 int ret;
7706
7707 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7708 ret = _nfs4_proc_destroy_clientid(clp, cred);
7709 switch (ret) {
7710 case -NFS4ERR_DELAY:
7711 case -NFS4ERR_CLIENTID_BUSY:
7712 ssleep(1);
7713 break;
7714 default:
7715 return ret;
7716 }
7717 }
7718 return 0;
7719}
7720
7721int nfs4_destroy_clientid(struct nfs_client *clp)
7722{
7723 struct rpc_cred *cred;
7724 int ret = 0;
7725
7726 if (clp->cl_mvops->minor_version < 1)
7727 goto out;
7728 if (clp->cl_exchange_flags == 0)
7729 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007730 if (clp->cl_preserve_clid)
7731 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007732 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007733 ret = nfs4_proc_destroy_clientid(clp, cred);
7734 if (cred)
7735 put_rpccred(cred);
7736 switch (ret) {
7737 case 0:
7738 case -NFS4ERR_STALE_CLIENTID:
7739 clp->cl_exchange_flags = 0;
7740 }
7741out:
7742 return ret;
7743}
7744
Andy Adamson2050f0c2009-04-01 09:22:30 -04007745struct nfs4_get_lease_time_data {
7746 struct nfs4_get_lease_time_args *args;
7747 struct nfs4_get_lease_time_res *res;
7748 struct nfs_client *clp;
7749};
7750
7751static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7752 void *calldata)
7753{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007754 struct nfs4_get_lease_time_data *data =
7755 (struct nfs4_get_lease_time_data *)calldata;
7756
7757 dprintk("--> %s\n", __func__);
7758 /* just setup sequence, do not trigger session recovery
7759 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007760 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007761 &data->args->la_seq_args,
7762 &data->res->lr_seq_res,
7763 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007764 dprintk("<-- %s\n", __func__);
7765}
7766
7767/*
7768 * Called from nfs4_state_manager thread for session setup, so don't recover
7769 * from sequence operation or clientid errors.
7770 */
7771static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7772{
7773 struct nfs4_get_lease_time_data *data =
7774 (struct nfs4_get_lease_time_data *)calldata;
7775
7776 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007777 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7778 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007779 switch (task->tk_status) {
7780 case -NFS4ERR_DELAY:
7781 case -NFS4ERR_GRACE:
7782 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7783 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7784 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007785 /* fall through */
7786 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007787 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007788 return;
7789 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007790 dprintk("<-- %s\n", __func__);
7791}
7792
Trond Myklebust17280172012-03-11 13:11:00 -04007793static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007794 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7795 .rpc_call_done = nfs4_get_lease_time_done,
7796};
7797
7798int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7799{
7800 struct rpc_task *task;
7801 struct nfs4_get_lease_time_args args;
7802 struct nfs4_get_lease_time_res res = {
7803 .lr_fsinfo = fsinfo,
7804 };
7805 struct nfs4_get_lease_time_data data = {
7806 .args = &args,
7807 .res = &res,
7808 .clp = clp,
7809 };
7810 struct rpc_message msg = {
7811 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7812 .rpc_argp = &args,
7813 .rpc_resp = &res,
7814 };
7815 struct rpc_task_setup task_setup = {
7816 .rpc_client = clp->cl_rpcclient,
7817 .rpc_message = &msg,
7818 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007819 .callback_data = &data,
7820 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007821 };
7822 int status;
7823
Chuck Levera9c92d62013-08-09 12:48:18 -04007824 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007825 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007826 task = rpc_run_task(&task_setup);
7827
7828 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04007829 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007830
Anna Schumakerf6148712017-04-07 14:15:23 -04007831 status = task->tk_status;
7832 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007833 return status;
7834}
7835
Andy Adamsonfc931582009-04-01 09:22:31 -04007836/*
7837 * Initialize the values to be used by the client in CREATE_SESSION
7838 * If nfs4_init_session set the fore channel request and response sizes,
7839 * use them.
7840 *
7841 * Set the back channel max_resp_sz_cached to zero to force the client to
7842 * always set csa_cachethis to FALSE because the current implementation
7843 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7844 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007845static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7846 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007847{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007848 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007849 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007850
Andy Adamson18aad3d2013-06-26 12:21:49 -04007851 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7852 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7853
Andy Adamsonfc931582009-04-01 09:22:31 -04007854 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007855 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7856 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007857 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007858 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007859
7860 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007861 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007862 __func__,
7863 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007864 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007865
7866 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007867 args->bc_attrs.max_rqst_sz = max_bc_payload;
7868 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007869 args->bc_attrs.max_resp_sz_cached = 0;
7870 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007871 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007872
7873 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7874 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7875 __func__,
7876 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7877 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7878 args->bc_attrs.max_reqs);
7879}
7880
Trond Myklebust79969dd2015-02-18 11:30:18 -08007881static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7882 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007883{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007884 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007885 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007886
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007887 if (rcvd->max_resp_sz > sent->max_resp_sz)
7888 return -EINVAL;
7889 /*
7890 * Our requested max_ops is the minimum we need; we're not
7891 * prepared to break up compounds into smaller pieces than that.
7892 * So, no point even trying to continue if the server won't
7893 * cooperate:
7894 */
7895 if (rcvd->max_ops < sent->max_ops)
7896 return -EINVAL;
7897 if (rcvd->max_reqs == 0)
7898 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007899 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7900 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007901 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007902}
7903
Trond Myklebust79969dd2015-02-18 11:30:18 -08007904static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7905 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007906{
7907 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007908 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007909
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007910 if (!(res->flags & SESSION4_BACK_CHAN))
7911 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007912 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7913 return -EINVAL;
7914 if (rcvd->max_resp_sz < sent->max_resp_sz)
7915 return -EINVAL;
7916 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7917 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007918 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007919 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007920 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007921 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007922out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007923 return 0;
7924}
Andy Adamson8d353012009-04-01 09:22:32 -04007925
Andy Adamson8d353012009-04-01 09:22:32 -04007926static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007927 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007928{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007929 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007930
Trond Myklebust79969dd2015-02-18 11:30:18 -08007931 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007932 if (ret)
7933 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007934 return nfs4_verify_back_channel_attrs(args, res);
7935}
7936
7937static void nfs4_update_session(struct nfs4_session *session,
7938 struct nfs41_create_session_res *res)
7939{
7940 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007941 /* Mark client id and session as being confirmed */
7942 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7943 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007944 session->flags = res->flags;
7945 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007946 if (res->flags & SESSION4_BACK_CHAN)
7947 memcpy(&session->bc_attrs, &res->bc_attrs,
7948 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007949}
7950
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007951static int _nfs4_proc_create_session(struct nfs_client *clp,
7952 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007953{
7954 struct nfs4_session *session = clp->cl_session;
7955 struct nfs41_create_session_args args = {
7956 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007957 .clientid = clp->cl_clientid,
7958 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007959 .cb_program = NFS4_CALLBACK,
7960 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007961 struct nfs41_create_session_res res;
7962
Andy Adamsonfc931582009-04-01 09:22:31 -04007963 struct rpc_message msg = {
7964 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7965 .rpc_argp = &args,
7966 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007967 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007968 };
7969 int status;
7970
Chuck Lever6b26cc82016-05-02 14:40:40 -04007971 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007972 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007973
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007974 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007975 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007976
Trond Myklebustb519d402016-09-11 14:50:01 -04007977 switch (status) {
7978 case -NFS4ERR_STALE_CLIENTID:
7979 case -NFS4ERR_DELAY:
7980 case -ETIMEDOUT:
7981 case -EACCES:
7982 case -EAGAIN:
7983 goto out;
7984 };
7985
7986 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007987 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007988 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007989 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007990 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007991 if (status)
7992 goto out;
7993 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007994 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007995out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007996 return status;
7997}
7998
7999/*
8000 * Issues a CREATE_SESSION operation to the server.
8001 * It is the responsibility of the caller to verify the session is
8002 * expired before calling this routine.
8003 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008004int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008005{
8006 int status;
8007 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008008 struct nfs4_session *session = clp->cl_session;
8009
8010 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8011
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008012 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008013 if (status)
8014 goto out;
8015
Andy Adamsonaacd5532011-11-09 13:58:21 -05008016 /* Init or reset the session slot tables */
8017 status = nfs4_setup_session_slot_tables(session);
8018 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008019 if (status)
8020 goto out;
8021
8022 ptr = (unsigned *)&session->sess_id.data[0];
8023 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8024 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008025out:
8026 dprintk("<-- %s\n", __func__);
8027 return status;
8028}
8029
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008030/*
8031 * Issue the over-the-wire RPC DESTROY_SESSION.
8032 * The caller must serialize access to this routine.
8033 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008034int nfs4_proc_destroy_session(struct nfs4_session *session,
8035 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008036{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008037 struct rpc_message msg = {
8038 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8039 .rpc_argp = session,
8040 .rpc_cred = cred,
8041 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008042 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008043
8044 dprintk("--> nfs4_proc_destroy_session\n");
8045
8046 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008047 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8048 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008049
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008050 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008051 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008052
8053 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008054 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008055 "Session has been destroyed regardless...\n", status);
8056
8057 dprintk("<-- nfs4_proc_destroy_session\n");
8058 return status;
8059}
8060
Trond Myklebust7b38c362012-05-23 13:23:31 -04008061/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008062 * Renew the cl_session lease.
8063 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008064struct nfs4_sequence_data {
8065 struct nfs_client *clp;
8066 struct nfs4_sequence_args args;
8067 struct nfs4_sequence_res res;
8068};
8069
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008070static void nfs41_sequence_release(void *data)
8071{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008072 struct nfs4_sequence_data *calldata = data;
8073 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008074
Alexandros Batsakis71358402010-02-05 03:45:05 -08008075 if (atomic_read(&clp->cl_count) > 1)
8076 nfs4_schedule_state_renewal(clp);
8077 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008078 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008079}
8080
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008081static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8082{
8083 switch(task->tk_status) {
8084 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008085 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8086 return -EAGAIN;
8087 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008088 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008089 }
8090 return 0;
8091}
8092
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008093static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008094{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008095 struct nfs4_sequence_data *calldata = data;
8096 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008097
Trond Myklebust14516c32010-07-31 14:29:06 -04008098 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8099 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008100
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008101 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008102 if (task->tk_status < 0) {
8103 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008104 if (atomic_read(&clp->cl_count) == 1)
8105 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008106
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008107 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8108 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008109 return;
8110 }
8111 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008112 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008113out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008114 dprintk("<-- %s\n", __func__);
8115}
8116
8117static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8118{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008119 struct nfs4_sequence_data *calldata = data;
8120 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008121 struct nfs4_sequence_args *args;
8122 struct nfs4_sequence_res *res;
8123
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008124 args = task->tk_msg.rpc_argp;
8125 res = task->tk_msg.rpc_resp;
8126
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008127 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008128}
8129
8130static const struct rpc_call_ops nfs41_sequence_ops = {
8131 .rpc_call_done = nfs41_sequence_call_done,
8132 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008133 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008134};
8135
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008136static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8137 struct rpc_cred *cred,
8138 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008139{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008140 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008141 struct rpc_message msg = {
8142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8143 .rpc_cred = cred,
8144 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008145 struct rpc_task_setup task_setup_data = {
8146 .rpc_client = clp->cl_rpcclient,
8147 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008148 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008149 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008150 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008151
Alexandros Batsakis71358402010-02-05 03:45:05 -08008152 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008153 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008154 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008155 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008156 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008157 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008158 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008159 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008160 if (is_privileged)
8161 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008162 msg.rpc_argp = &calldata->args;
8163 msg.rpc_resp = &calldata->res;
8164 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008165 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008166
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008167 return rpc_run_task(&task_setup_data);
8168}
8169
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008170static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008171{
8172 struct rpc_task *task;
8173 int ret = 0;
8174
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008175 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008176 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008177 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008178 if (IS_ERR(task))
8179 ret = PTR_ERR(task);
8180 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008181 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008182 dprintk("<-- %s status=%d\n", __func__, ret);
8183 return ret;
8184}
8185
8186static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8187{
8188 struct rpc_task *task;
8189 int ret;
8190
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008191 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008192 if (IS_ERR(task)) {
8193 ret = PTR_ERR(task);
8194 goto out;
8195 }
8196 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008197 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008198 ret = task->tk_status;
8199 rpc_put_task(task);
8200out:
8201 dprintk("<-- %s status=%d\n", __func__, ret);
8202 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008203}
8204
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008205struct nfs4_reclaim_complete_data {
8206 struct nfs_client *clp;
8207 struct nfs41_reclaim_complete_args arg;
8208 struct nfs41_reclaim_complete_res res;
8209};
8210
8211static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8212{
8213 struct nfs4_reclaim_complete_data *calldata = data;
8214
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008215 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008216 &calldata->arg.seq_args,
8217 &calldata->res.seq_res,
8218 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008219}
8220
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008221static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8222{
8223 switch(task->tk_status) {
8224 case 0:
8225 case -NFS4ERR_COMPLETE_ALREADY:
8226 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8227 break;
8228 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008229 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008230 /* fall through */
8231 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008232 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008233 case -NFS4ERR_BADSESSION:
8234 case -NFS4ERR_DEADSESSION:
8235 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8236 nfs4_schedule_session_recovery(clp->cl_session,
8237 task->tk_status);
8238 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008239 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008240 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008241 }
8242 return 0;
8243}
8244
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008245static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8246{
8247 struct nfs4_reclaim_complete_data *calldata = data;
8248 struct nfs_client *clp = calldata->clp;
8249 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8250
8251 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008252 if (!nfs41_sequence_done(task, res))
8253 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008254
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008255 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008256 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8257 rpc_restart_call_prepare(task);
8258 return;
8259 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008260 dprintk("<-- %s\n", __func__);
8261}
8262
8263static void nfs4_free_reclaim_complete_data(void *data)
8264{
8265 struct nfs4_reclaim_complete_data *calldata = data;
8266
8267 kfree(calldata);
8268}
8269
8270static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8271 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8272 .rpc_call_done = nfs4_reclaim_complete_done,
8273 .rpc_release = nfs4_free_reclaim_complete_data,
8274};
8275
8276/*
8277 * Issue a global reclaim complete.
8278 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008279static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8280 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008281{
8282 struct nfs4_reclaim_complete_data *calldata;
8283 struct rpc_task *task;
8284 struct rpc_message msg = {
8285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008286 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008287 };
8288 struct rpc_task_setup task_setup_data = {
8289 .rpc_client = clp->cl_rpcclient,
8290 .rpc_message = &msg,
8291 .callback_ops = &nfs4_reclaim_complete_call_ops,
8292 .flags = RPC_TASK_ASYNC,
8293 };
8294 int status = -ENOMEM;
8295
8296 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008297 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008298 if (calldata == NULL)
8299 goto out;
8300 calldata->clp = clp;
8301 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008302
Chuck Levera9c92d62013-08-09 12:48:18 -04008303 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008304 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008305 msg.rpc_argp = &calldata->arg;
8306 msg.rpc_resp = &calldata->res;
8307 task_setup_data.callback_data = calldata;
8308 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008309 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008310 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008311 goto out;
8312 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008313 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008314 if (status == 0)
8315 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008316 rpc_put_task(task);
8317out:
8318 dprintk("<-- %s status=%d\n", __func__, status);
8319 return status;
8320}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008321
8322static void
8323nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8324{
8325 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008326 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008327
8328 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008329 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008330 &lgp->res.seq_res, task);
8331 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008332}
8333
8334static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8335{
8336 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008337
8338 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008339 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008340 dprintk("<-- %s\n", __func__);
8341}
8342
8343static int
8344nfs4_layoutget_handle_exception(struct rpc_task *task,
8345 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8346{
Trond Myklebustee314c22012-10-01 17:25:48 -07008347 struct inode *inode = lgp->args.inode;
8348 struct nfs_server *server = NFS_SERVER(inode);
8349 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008350 int nfs4err = task->tk_status;
8351 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008352 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008353
Boaz Harroshed7e5422014-01-22 20:34:54 +02008354 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008355
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008356 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008357 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008358 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008359
8360 /*
8361 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8362 * on the file. set tk_status to -ENODATA to tell upper layer to
8363 * retry go inband.
8364 */
8365 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008366 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008367 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008368 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008369 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8370 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8371 */
8372 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008373 status = -EOVERFLOW;
8374 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008375 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008376 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008377 * (or clients) writing to the same RAID stripe except when
8378 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008379 *
8380 * Treat it like we would RECALLCONFLICT -- we retry for a little
8381 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008382 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008383 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008384 if (lgp->args.minlength == 0) {
8385 status = -EOVERFLOW;
8386 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008387 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008388 status = -EBUSY;
8389 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008390 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008391 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008392 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008393 case -NFS4ERR_DELEG_REVOKED:
8394 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008395 case -NFS4ERR_EXPIRED:
8396 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008397 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008398 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008399 lo = NFS_I(inode)->layout;
8400 /* If the open stateid was bad, then recover it. */
8401 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008402 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008403 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008404 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008405 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008406 break;
8407 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008408
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008409 /*
8410 * Mark the bad layout state as invalid, then retry
8411 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008412 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008413 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008414 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008415 pnfs_free_lseg_list(&head);
8416 status = -EAGAIN;
8417 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008418 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008419
Trond Myklebust8ac09252017-01-23 22:44:12 -05008420 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008421 err = nfs4_handle_exception(server, nfs4err, exception);
8422 if (!status) {
8423 if (exception->retry)
8424 status = -EAGAIN;
8425 else
8426 status = err;
8427 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008428out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008429 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008430 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008431}
8432
Idan Kedar85541162012-08-02 11:47:10 +03008433static size_t max_response_pages(struct nfs_server *server)
8434{
8435 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8436 return nfs_page_array_len(0, max_resp_sz);
8437}
8438
8439static void nfs4_free_pages(struct page **pages, size_t size)
8440{
8441 int i;
8442
8443 if (!pages)
8444 return;
8445
8446 for (i = 0; i < size; i++) {
8447 if (!pages[i])
8448 break;
8449 __free_page(pages[i]);
8450 }
8451 kfree(pages);
8452}
8453
8454static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8455{
8456 struct page **pages;
8457 int i;
8458
8459 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8460 if (!pages) {
8461 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8462 return NULL;
8463 }
8464
8465 for (i = 0; i < size; i++) {
8466 pages[i] = alloc_page(gfp_flags);
8467 if (!pages[i]) {
8468 dprintk("%s: failed to allocate page\n", __func__);
8469 nfs4_free_pages(pages, size);
8470 return NULL;
8471 }
8472 }
8473
8474 return pages;
8475}
8476
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008477static void nfs4_layoutget_release(void *calldata)
8478{
8479 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008480 struct inode *inode = lgp->args.inode;
8481 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008482 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008483
8484 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008485 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008486 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008487 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008488 put_nfs_open_context(lgp->args.ctx);
8489 kfree(calldata);
8490 dprintk("<-- %s\n", __func__);
8491}
8492
8493static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8494 .rpc_call_prepare = nfs4_layoutget_prepare,
8495 .rpc_call_done = nfs4_layoutget_done,
8496 .rpc_release = nfs4_layoutget_release,
8497};
8498
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008499struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008500nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008501{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008502 struct inode *inode = lgp->args.inode;
8503 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008504 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008505 struct rpc_task *task;
8506 struct rpc_message msg = {
8507 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8508 .rpc_argp = &lgp->args,
8509 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008510 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008511 };
8512 struct rpc_task_setup task_setup_data = {
8513 .rpc_client = server->client,
8514 .rpc_message = &msg,
8515 .callback_ops = &nfs4_layoutget_call_ops,
8516 .callback_data = lgp,
8517 .flags = RPC_TASK_ASYNC,
8518 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008519 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008520 struct nfs4_exception exception = {
8521 .inode = inode,
8522 .timeout = *timeout,
8523 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008524 int status = 0;
8525
8526 dprintk("--> %s\n", __func__);
8527
Peng Tao4bd5a982014-11-17 11:05:17 +08008528 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8529 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8530
Idan Kedar85541162012-08-02 11:47:10 +03008531 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8532 if (!lgp->args.layout.pages) {
8533 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008534 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008535 }
8536 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8537
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008538 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008539 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008540 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008541
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008542 task = rpc_run_task(&task_setup_data);
8543 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008544 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008545 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008546 if (status == 0) {
8547 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8548 *timeout = exception.timeout;
8549 }
8550
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008551 trace_nfs4_layoutget(lgp->args.ctx,
8552 &lgp->args.range,
8553 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008554 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008555 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008556
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008557 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8558 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008559 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008560 rpc_put_task(task);
8561 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008562 if (status)
8563 return ERR_PTR(status);
8564 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008565}
8566
Benny Halevycbe82602011-05-22 19:52:37 +03008567static void
8568nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8569{
8570 struct nfs4_layoutreturn *lrp = calldata;
8571
8572 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008573 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008574 &lrp->args.seq_args,
8575 &lrp->res.seq_res,
8576 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008577}
8578
8579static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8580{
8581 struct nfs4_layoutreturn *lrp = calldata;
8582 struct nfs_server *server;
8583
8584 dprintk("--> %s\n", __func__);
8585
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008586 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008587 return;
8588
8589 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008590 switch (task->tk_status) {
8591 default:
8592 task->tk_status = 0;
8593 case 0:
8594 break;
8595 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008596 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008597 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008598 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008599 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008600 return;
8601 }
Benny Halevycbe82602011-05-22 19:52:37 +03008602 dprintk("<-- %s\n", __func__);
8603}
8604
8605static void nfs4_layoutreturn_release(void *calldata)
8606{
8607 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008608 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008609
8610 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008611 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008612 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008613 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008614 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8615 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008616 pnfs_put_layout_hdr(lrp->args.layout);
8617 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008618 kfree(calldata);
8619 dprintk("<-- %s\n", __func__);
8620}
8621
8622static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8623 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8624 .rpc_call_done = nfs4_layoutreturn_done,
8625 .rpc_release = nfs4_layoutreturn_release,
8626};
8627
Peng Tao6c166052014-11-17 09:30:40 +08008628int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008629{
8630 struct rpc_task *task;
8631 struct rpc_message msg = {
8632 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8633 .rpc_argp = &lrp->args,
8634 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008635 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008636 };
8637 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008638 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008639 .rpc_message = &msg,
8640 .callback_ops = &nfs4_layoutreturn_call_ops,
8641 .callback_data = lrp,
8642 };
Peng Tao6c166052014-11-17 09:30:40 +08008643 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008644
Andrew Elble99ade3c2015-12-02 09:39:51 -05008645 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8646 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8647 &task_setup_data.rpc_client, &msg);
8648
Benny Halevycbe82602011-05-22 19:52:37 +03008649 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008650 if (!sync) {
8651 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8652 if (!lrp->inode) {
8653 nfs4_layoutreturn_release(lrp);
8654 return -EAGAIN;
8655 }
8656 task_setup_data.flags |= RPC_TASK_ASYNC;
8657 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008658 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008659 task = rpc_run_task(&task_setup_data);
8660 if (IS_ERR(task))
8661 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008662 if (sync)
8663 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008664 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008665 dprintk("<-- %s status=%d\n", __func__, status);
8666 rpc_put_task(task);
8667 return status;
8668}
8669
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008670static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008671_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8672 struct pnfs_device *pdev,
8673 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008674{
8675 struct nfs4_getdeviceinfo_args args = {
8676 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008677 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8678 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008679 };
8680 struct nfs4_getdeviceinfo_res res = {
8681 .pdev = pdev,
8682 };
8683 struct rpc_message msg = {
8684 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8685 .rpc_argp = &args,
8686 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008687 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008688 };
8689 int status;
8690
8691 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008692 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008693 if (res.notification & ~args.notify_types)
8694 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008695 if (res.notification != args.notify_types)
8696 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008697
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008698 dprintk("<-- %s status=%d\n", __func__, status);
8699
8700 return status;
8701}
8702
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008703int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8704 struct pnfs_device *pdev,
8705 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008706{
8707 struct nfs4_exception exception = { };
8708 int err;
8709
8710 do {
8711 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008712 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008713 &exception);
8714 } while (exception.retry);
8715 return err;
8716}
8717EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8718
Andy Adamson863a3c62011-03-23 13:27:54 +00008719static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8720{
8721 struct nfs4_layoutcommit_data *data = calldata;
8722 struct nfs_server *server = NFS_SERVER(data->args.inode);
8723
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008724 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008725 &data->args.seq_args,
8726 &data->res.seq_res,
8727 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008728}
8729
8730static void
8731nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8732{
8733 struct nfs4_layoutcommit_data *data = calldata;
8734 struct nfs_server *server = NFS_SERVER(data->args.inode);
8735
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008736 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008737 return;
8738
8739 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008740 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8741 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8742 case -NFS4ERR_BADLAYOUT: /* no layout */
8743 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008744 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008745 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008746 break;
8747 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008748 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008749 rpc_restart_call_prepare(task);
8750 return;
8751 }
8752 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008753}
8754
8755static void nfs4_layoutcommit_release(void *calldata)
8756{
8757 struct nfs4_layoutcommit_data *data = calldata;
8758
Andy Adamsondb29c082011-07-30 20:52:38 -04008759 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008760 nfs_post_op_update_inode_force_wcc(data->args.inode,
8761 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008762 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008763 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008764 kfree(data);
8765}
8766
8767static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8768 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8769 .rpc_call_done = nfs4_layoutcommit_done,
8770 .rpc_release = nfs4_layoutcommit_release,
8771};
8772
8773int
Andy Adamsonef311532011-03-12 02:58:10 -05008774nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008775{
8776 struct rpc_message msg = {
8777 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8778 .rpc_argp = &data->args,
8779 .rpc_resp = &data->res,
8780 .rpc_cred = data->cred,
8781 };
8782 struct rpc_task_setup task_setup_data = {
8783 .task = &data->task,
8784 .rpc_client = NFS_CLIENT(data->args.inode),
8785 .rpc_message = &msg,
8786 .callback_ops = &nfs4_layoutcommit_ops,
8787 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008788 };
8789 struct rpc_task *task;
8790 int status = 0;
8791
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008792 dprintk("NFS: initiating layoutcommit call. sync %d "
8793 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008794 data->args.lastbytewritten,
8795 data->args.inode->i_ino);
8796
Trond Myklebust472e2592015-02-05 16:50:30 -05008797 if (!sync) {
8798 data->inode = nfs_igrab_and_active(data->args.inode);
8799 if (data->inode == NULL) {
8800 nfs4_layoutcommit_release(data);
8801 return -EAGAIN;
8802 }
8803 task_setup_data.flags = RPC_TASK_ASYNC;
8804 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008805 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008806 task = rpc_run_task(&task_setup_data);
8807 if (IS_ERR(task))
8808 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008809 if (sync)
8810 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008811 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008812 dprintk("%s: status %d\n", __func__, status);
8813 rpc_put_task(task);
8814 return status;
8815}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008816
Andy Adamson97431202013-08-08 10:57:56 -04008817/**
8818 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8819 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8820 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008821static int
8822_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008823 struct nfs_fsinfo *info,
8824 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008825{
8826 struct nfs41_secinfo_no_name_args args = {
8827 .style = SECINFO_STYLE_CURRENT_FH,
8828 };
8829 struct nfs4_secinfo_res res = {
8830 .flavors = flavors,
8831 };
8832 struct rpc_message msg = {
8833 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8834 .rpc_argp = &args,
8835 .rpc_resp = &res,
8836 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008837 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008838 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008839 int status;
8840
8841 if (use_integrity) {
8842 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008843 cred = nfs4_get_clid_cred(server->nfs_client);
8844 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008845 }
8846
8847 dprintk("--> %s\n", __func__);
8848 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8849 &res.seq_res, 0);
8850 dprintk("<-- %s status=%d\n", __func__, status);
8851
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008852 if (cred)
8853 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008854
8855 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008856}
8857
8858static int
8859nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8860 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8861{
8862 struct nfs4_exception exception = { };
8863 int err;
8864 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008865 /* first try using integrity protection */
8866 err = -NFS4ERR_WRONGSEC;
8867
8868 /* try to use integrity protection with machine cred */
8869 if (_nfs4_is_integrity_protected(server->nfs_client))
8870 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8871 flavors, true);
8872
8873 /*
8874 * if unable to use integrity protection, or SECINFO with
8875 * integrity protection returns NFS4ERR_WRONGSEC (which is
8876 * disallowed by spec, but exists in deployed servers) use
8877 * the current filesystem's rpc_client and the user cred.
8878 */
8879 if (err == -NFS4ERR_WRONGSEC)
8880 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8881 flavors, false);
8882
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008883 switch (err) {
8884 case 0:
8885 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008886 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008887 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008888 default:
8889 err = nfs4_handle_exception(server, err, &exception);
8890 }
8891 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008892out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008893 return err;
8894}
8895
8896static int
8897nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8898 struct nfs_fsinfo *info)
8899{
8900 int err;
8901 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008902 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008903 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008904 struct nfs4_secinfo4 *secinfo;
8905 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008906
8907 page = alloc_page(GFP_KERNEL);
8908 if (!page) {
8909 err = -ENOMEM;
8910 goto out;
8911 }
8912
8913 flavors = page_address(page);
8914 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8915
8916 /*
8917 * Fall back on "guess and check" method if
8918 * the server doesn't support SECINFO_NO_NAME
8919 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008920 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008921 err = nfs4_find_root_sec(server, fhandle, info);
8922 goto out_freepage;
8923 }
8924 if (err)
8925 goto out_freepage;
8926
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008927 for (i = 0; i < flavors->num_flavors; i++) {
8928 secinfo = &flavors->flavors[i];
8929
8930 switch (secinfo->flavor) {
8931 case RPC_AUTH_NULL:
8932 case RPC_AUTH_UNIX:
8933 case RPC_AUTH_GSS:
8934 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8935 &secinfo->flavor_info);
8936 break;
8937 default:
8938 flavor = RPC_AUTH_MAXFLAVOR;
8939 break;
8940 }
8941
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008942 if (!nfs_auth_info_match(&server->auth_info, flavor))
8943 flavor = RPC_AUTH_MAXFLAVOR;
8944
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008945 if (flavor != RPC_AUTH_MAXFLAVOR) {
8946 err = nfs4_lookup_root_sec(server, fhandle,
8947 info, flavor);
8948 if (!err)
8949 break;
8950 }
8951 }
8952
8953 if (flavor == RPC_AUTH_MAXFLAVOR)
8954 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008955
8956out_freepage:
8957 put_page(page);
8958 if (err == -EACCES)
8959 return -EPERM;
8960out:
8961 return err;
8962}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008963
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008964static int _nfs41_test_stateid(struct nfs_server *server,
8965 nfs4_stateid *stateid,
8966 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008967{
8968 int status;
8969 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008970 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008971 };
8972 struct nfs41_test_stateid_res res;
8973 struct rpc_message msg = {
8974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8975 .rpc_argp = &args,
8976 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008977 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008978 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008979 struct rpc_clnt *rpc_client = server->client;
8980
8981 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8982 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008983
Chuck Lever38527b12012-07-11 16:30:23 -04008984 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008985 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008986 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008987 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008988 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008989 if (status != NFS_OK) {
8990 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008991 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008992 }
8993 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008994 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008995}
8996
Trond Myklebust43912bb2016-09-22 13:38:56 -04008997static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8998 int err, struct nfs4_exception *exception)
8999{
9000 exception->retry = 0;
9001 switch(err) {
9002 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009003 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009004 nfs4_handle_exception(server, err, exception);
9005 break;
9006 case -NFS4ERR_BADSESSION:
9007 case -NFS4ERR_BADSLOT:
9008 case -NFS4ERR_BAD_HIGH_SLOT:
9009 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9010 case -NFS4ERR_DEADSESSION:
9011 nfs4_do_handle_exception(server, err, exception);
9012 }
9013}
9014
Chuck Lever38527b12012-07-11 16:30:23 -04009015/**
9016 * nfs41_test_stateid - perform a TEST_STATEID operation
9017 *
9018 * @server: server / transport on which to perform the operation
9019 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009020 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009021 *
9022 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9023 * Otherwise a negative NFS4ERR value is returned if the operation
9024 * failed or the state ID is not currently valid.
9025 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009026static int nfs41_test_stateid(struct nfs_server *server,
9027 nfs4_stateid *stateid,
9028 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009029{
9030 struct nfs4_exception exception = { };
9031 int err;
9032 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009033 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009034 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009035 } while (exception.retry);
9036 return err;
9037}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009038
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009039struct nfs_free_stateid_data {
9040 struct nfs_server *server;
9041 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009042 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009043};
9044
9045static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9046{
9047 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009048 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009049 &data->args.seq_args,
9050 &data->res.seq_res,
9051 task);
9052}
9053
9054static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9055{
9056 struct nfs_free_stateid_data *data = calldata;
9057
9058 nfs41_sequence_done(task, &data->res.seq_res);
9059
9060 switch (task->tk_status) {
9061 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009062 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009063 rpc_restart_call_prepare(task);
9064 }
9065}
9066
9067static void nfs41_free_stateid_release(void *calldata)
9068{
9069 kfree(calldata);
9070}
9071
Trond Myklebust17f26b12013-08-21 15:48:42 -04009072static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009073 .rpc_call_prepare = nfs41_free_stateid_prepare,
9074 .rpc_call_done = nfs41_free_stateid_done,
9075 .rpc_release = nfs41_free_stateid_release,
9076};
9077
9078static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009079 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009080 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009081 bool privileged)
9082{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009083 struct rpc_message msg = {
9084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009085 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009086 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009087 struct rpc_task_setup task_setup = {
9088 .rpc_client = server->client,
9089 .rpc_message = &msg,
9090 .callback_ops = &nfs41_free_stateid_ops,
9091 .flags = RPC_TASK_ASYNC,
9092 };
9093 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009094
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009095 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9096 &task_setup.rpc_client, &msg);
9097
Chuck Lever38527b12012-07-11 16:30:23 -04009098 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009099 data = kmalloc(sizeof(*data), GFP_NOFS);
9100 if (!data)
9101 return ERR_PTR(-ENOMEM);
9102 data->server = server;
9103 nfs4_stateid_copy(&data->args.stateid, stateid);
9104
9105 task_setup.callback_data = data;
9106
9107 msg.rpc_argp = &data->args;
9108 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009109 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009110 if (privileged)
9111 nfs4_set_sequence_privileged(&data->args.seq_args);
9112
9113 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009114}
9115
Chuck Lever38527b12012-07-11 16:30:23 -04009116/**
9117 * nfs41_free_stateid - perform a FREE_STATEID operation
9118 *
9119 * @server: server / transport on which to perform the operation
9120 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009121 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009122 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009123 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009124 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009125 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009126static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009127 const nfs4_stateid *stateid,
9128 struct rpc_cred *cred,
9129 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009130{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009131 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009132
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009133 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009134 if (IS_ERR(task))
9135 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009136 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009137 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009138}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009139
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009140static void
9141nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009142{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009143 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009144
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009145 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009146 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009147}
9148
Trond Myklebust36281ca2012-03-04 18:13:56 -05009149static bool nfs41_match_stateid(const nfs4_stateid *s1,
9150 const nfs4_stateid *s2)
9151{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009152 if (s1->type != s2->type)
9153 return false;
9154
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009155 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009156 return false;
9157
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009158 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009159 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009160
Anna Schumaker045c5512017-01-11 16:59:48 -05009161 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009162}
9163
Andy Adamson557134a2009-04-01 09:21:53 -04009164#endif /* CONFIG_NFS_V4_1 */
9165
Trond Myklebust36281ca2012-03-04 18:13:56 -05009166static bool nfs4_match_stateid(const nfs4_stateid *s1,
9167 const nfs4_stateid *s2)
9168{
Trond Myklebustf597c532012-03-04 18:13:56 -05009169 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009170}
9171
9172
Trond Myklebust17280172012-03-11 13:11:00 -04009173static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009174 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009175 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009176 .recover_open = nfs4_open_reclaim,
9177 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009178 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009179 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009180};
9181
Andy Adamson591d71c2009-04-01 09:22:47 -04009182#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009183static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009184 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9185 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9186 .recover_open = nfs4_open_reclaim,
9187 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009188 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009189 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009190 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009191};
9192#endif /* CONFIG_NFS_V4_1 */
9193
Trond Myklebust17280172012-03-11 13:11:00 -04009194static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009195 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009196 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009197 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009198 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009199 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009200};
9201
Andy Adamson591d71c2009-04-01 09:22:47 -04009202#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009203static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009204 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9205 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009206 .recover_open = nfs41_open_expired,
9207 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009208 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009209};
9210#endif /* CONFIG_NFS_V4_1 */
9211
Trond Myklebust17280172012-03-11 13:11:00 -04009212static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009213 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009214 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009215 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009216};
9217
9218#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009219static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009220 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009221 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009222 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009223};
9224#endif
9225
Chuck Leverec011fe2013-10-17 14:12:39 -04009226static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009227 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009228 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009229};
9230
9231#if defined(CONFIG_NFS_V4_1)
9232static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009233 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009234 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009235};
9236#endif /* CONFIG_NFS_V4_1 */
9237
Trond Myklebust97dc1352010-06-16 09:52:26 -04009238static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9239 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009240 .init_caps = NFS_CAP_READDIRPLUS
9241 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009242 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009243 .init_client = nfs40_init_client,
9244 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009245 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009246 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009247 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009248 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009249 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009250 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009251 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9252 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9253 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009254 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009255};
9256
9257#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009258static struct nfs_seqid *
9259nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9260{
9261 return NULL;
9262}
9263
Trond Myklebust97dc1352010-06-16 09:52:26 -04009264static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9265 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009266 .init_caps = NFS_CAP_READDIRPLUS
9267 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009268 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009269 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009270 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009271 .init_client = nfs41_init_client,
9272 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009273 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009274 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009275 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009276 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009277 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009278 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009279 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009280 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9281 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9282 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009283 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009284};
9285#endif
9286
Steve Dickson42c2c422013-05-22 12:50:38 -04009287#if defined(CONFIG_NFS_V4_2)
9288static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9289 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009290 .init_caps = NFS_CAP_READDIRPLUS
9291 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009292 | NFS_CAP_POSIX_LOCK
9293 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009294 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009295 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009296 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009297 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009298 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009299 | NFS_CAP_LAYOUTSTATS
9300 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009301 .init_client = nfs41_init_client,
9302 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009303 .match_stateid = nfs41_match_stateid,
9304 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009305 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009306 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009307 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009308 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009309 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009310 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9311 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9312 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009313 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009314};
9315#endif
9316
Trond Myklebust97dc1352010-06-16 09:52:26 -04009317const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9318 [0] = &nfs_v4_0_minor_ops,
9319#if defined(CONFIG_NFS_V4_1)
9320 [1] = &nfs_v4_1_minor_ops,
9321#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009322#if defined(CONFIG_NFS_V4_2)
9323 [2] = &nfs_v4_2_minor_ops,
9324#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009325};
9326
Trond Myklebust13997822016-07-24 17:10:52 -04009327static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009328{
9329 ssize_t error, error2;
9330
9331 error = generic_listxattr(dentry, list, size);
9332 if (error < 0)
9333 return error;
9334 if (list) {
9335 list += error;
9336 size -= error;
9337 }
9338
9339 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9340 if (error2 < 0)
9341 return error2;
9342 return error + error2;
9343}
9344
Trond Myklebust17f26b12013-08-21 15:48:42 -04009345static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009346 .create = nfs_create,
9347 .lookup = nfs_lookup,
9348 .atomic_open = nfs_atomic_open,
9349 .link = nfs_link,
9350 .unlink = nfs_unlink,
9351 .symlink = nfs_symlink,
9352 .mkdir = nfs_mkdir,
9353 .rmdir = nfs_rmdir,
9354 .mknod = nfs_mknod,
9355 .rename = nfs_rename,
9356 .permission = nfs_permission,
9357 .getattr = nfs_getattr,
9358 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009359 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009360};
9361
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009362static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009363 .permission = nfs_permission,
9364 .getattr = nfs_getattr,
9365 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009366 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009367};
9368
David Howells509de812006-08-22 20:06:11 -04009369const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009370 .version = 4, /* protocol version */
9371 .dentry_ops = &nfs4_dentry_operations,
9372 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009373 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009374 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009375 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009376 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009377 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009378 .getattr = nfs4_proc_getattr,
9379 .setattr = nfs4_proc_setattr,
9380 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009381 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009382 .access = nfs4_proc_access,
9383 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009384 .create = nfs4_proc_create,
9385 .remove = nfs4_proc_remove,
9386 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009387 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009388 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009389 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009390 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009391 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009392 .link = nfs4_proc_link,
9393 .symlink = nfs4_proc_symlink,
9394 .mkdir = nfs4_proc_mkdir,
9395 .rmdir = nfs4_proc_remove,
9396 .readdir = nfs4_proc_readdir,
9397 .mknod = nfs4_proc_mknod,
9398 .statfs = nfs4_proc_statfs,
9399 .fsinfo = nfs4_proc_fsinfo,
9400 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009401 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009402 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009403 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009404 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009405 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009406 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009407 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009408 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009409 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009410 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009411 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009412 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009413 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009414 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009415 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009416 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009417 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009418 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009419 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009420 .create_server = nfs4_create_server,
9421 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009422};
9423
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009424static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009425 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009426 .list = nfs4_xattr_list_nfs4_acl,
9427 .get = nfs4_xattr_get_nfs4_acl,
9428 .set = nfs4_xattr_set_nfs4_acl,
9429};
9430
9431const struct xattr_handler *nfs4_xattr_handlers[] = {
9432 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009433#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9434 &nfs4_xattr_nfs4_label_handler,
9435#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009436 NULL
9437};
9438
Linus Torvalds1da177e2005-04-16 15:20:36 -07009439/*
9440 * Local variables:
9441 * c-basic-offset: 8
9442 * End:
9443 */