blob: 01dcd4c8dc4f0b0833dae0c07ea602ff9d758041 [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 Myklebust035168a2010-06-16 09:52:26 -0400842 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
843
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500844 nfs4_setup_sequence(data->seq_server->nfs_client,
845 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400846}
847
Andy Adamson69ab40c2009-04-01 09:22:19 -0400848static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
849{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400850 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400851
Trond Myklebust14516c32010-07-31 14:29:06 -0400852 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400853}
854
Trond Myklebust17280172012-03-11 13:11:00 -0400855static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400856 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400857 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400858};
859
Chuck Lever3bd23842013-08-09 12:49:19 -0400860#else /* !CONFIG_NFS_V4_1 */
861
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400862static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
863{
864 return nfs40_sequence_done(task, res);
865}
866
867static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
868{
869 if (res->sr_slot != NULL)
870 nfs40_sequence_free_slot(res);
871}
872
Peng Tao2c4b1312014-06-11 05:24:16 +0800873int nfs4_sequence_done(struct rpc_task *task,
874 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400875{
Chuck Lever3bd23842013-08-09 12:49:19 -0400876 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400877}
Peng Tao2c4b1312014-06-11 05:24:16 +0800878EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400879
880#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400881
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500882int nfs4_setup_sequence(const struct nfs_client *client,
883 struct nfs4_sequence_args *args,
884 struct nfs4_sequence_res *res,
885 struct rpc_task *task)
886{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500887 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500888 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500889 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500890
Anna Schumaker9dd91072017-01-10 12:01:46 -0500891 /* slot already allocated? */
892 if (res->sr_slot != NULL)
893 goto out_start;
894
Anna Schumaker76ee0352017-01-10 16:49:31 -0500895 if (session) {
896 tbl = &session->fc_slot_table;
897 task->tk_timeout = 0;
898 }
899
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500900 spin_lock(&tbl->slot_tbl_lock);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500901 /* The state manager will wait until the slot table is empty */
902 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
903 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500904
Anna Schumaker3d358082017-01-11 10:54:04 -0500905 slot = nfs4_alloc_slot(tbl);
906 if (IS_ERR(slot)) {
907 /* Try again in 1/4 second */
908 if (slot == ERR_PTR(-ENOMEM))
909 task->tk_timeout = HZ >> 2;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500910 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -0500911 }
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500912 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500913
Anna Schumaker3d358082017-01-11 10:54:04 -0500914 slot->privileged = args->sa_privileged ? 1 : 0;
915 args->sa_slot = slot;
916
917 res->sr_slot = slot;
918 if (session) {
919 res->sr_timestamp = jiffies;
920 res->sr_status_flags = 0;
921 res->sr_status = 1;
Anna Schumaker3d358082017-01-11 10:54:04 -0500922 }
923
Anna Schumakerad05cc02017-01-11 13:37:06 -0500924 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd91072017-01-10 12:01:46 -0500925out_start:
926 rpc_call_start(task);
927 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500928
929out_sleep:
930 if (args->sa_privileged)
931 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
932 NULL, RPC_PRIORITY_PRIVILEGED);
933 else
934 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
935 spin_unlock(&tbl->slot_tbl_lock);
936 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500937}
938EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
939
Chuck Lever9915ea72013-08-09 12:48:27 -0400940static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
941{
942 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500943 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -0400944 data->seq_args, data->seq_res, task);
945}
946
947static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
948{
949 struct nfs4_call_sync_data *data = calldata;
950 nfs4_sequence_done(task, data->seq_res);
951}
952
953static const struct rpc_call_ops nfs40_call_sync_ops = {
954 .rpc_call_prepare = nfs40_call_sync_prepare,
955 .rpc_call_done = nfs40_call_sync_done,
956};
957
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400958static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400959 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500960 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100961 struct nfs4_sequence_args *args,
962 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400963{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100964 int ret;
965 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400966 struct nfs_client *clp = server->nfs_client;
967 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100968 .seq_server = server,
969 .seq_args = args,
970 .seq_res = res,
971 };
972 struct rpc_task_setup task_setup = {
973 .rpc_client = clnt,
974 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400975 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100976 .callback_data = &data
977 };
978
979 task = rpc_run_task(&task_setup);
980 if (IS_ERR(task))
981 ret = PTR_ERR(task);
982 else {
983 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400984 rpc_put_task(task);
985 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100986 return ret;
987}
988
Bryan Schumaker7c513052011-03-24 17:12:24 +0000989int nfs4_call_sync(struct rpc_clnt *clnt,
990 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000991 struct rpc_message *msg,
992 struct nfs4_sequence_args *args,
993 struct nfs4_sequence_res *res,
994 int cache_reply)
995{
Chuck Levera9c92d62013-08-09 12:48:18 -0400996 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400997 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000998}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001000static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1001 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 struct nfs_inode *nfsi = NFS_I(dir);
1004
1005 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001006 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001007 if (cinfo->atomic && cinfo->before == dir->i_version) {
1008 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1009 nfsi->attrtimeo_timestamp = jiffies;
1010 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 nfs_force_lookup_revalidate(dir);
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001012 if (cinfo->before != dir->i_version)
1013 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1014 NFS_INO_INVALID_ACL;
1015 }
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001016 dir->i_version = cinfo->after;
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001017 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001018 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001019 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 spin_unlock(&dir->i_lock);
1021}
1022
1023struct nfs4_opendata {
1024 struct kref kref;
1025 struct nfs_openargs o_arg;
1026 struct nfs_openres o_res;
1027 struct nfs_open_confirmargs c_arg;
1028 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001029 struct nfs4_string owner_name;
1030 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001031 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001033 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001035 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 struct nfs4_state_owner *owner;
1037 struct nfs4_state *state;
1038 struct iattr attrs;
1039 unsigned long timestamp;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001040 bool rpc_done;
1041 bool file_created;
1042 bool is_recover;
1043 bool cancelled;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001044 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001046
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001047static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1048 int err, struct nfs4_exception *exception)
1049{
1050 if (err != -EINVAL)
1051 return false;
1052 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1053 return false;
1054 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1055 exception->retry = 1;
1056 return true;
1057}
1058
Trond Myklebust6ae37332015-01-30 14:21:14 -05001059static u32
1060nfs4_map_atomic_open_share(struct nfs_server *server,
1061 fmode_t fmode, int openflags)
1062{
1063 u32 res = 0;
1064
1065 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1066 case FMODE_READ:
1067 res = NFS4_SHARE_ACCESS_READ;
1068 break;
1069 case FMODE_WRITE:
1070 res = NFS4_SHARE_ACCESS_WRITE;
1071 break;
1072 case FMODE_READ|FMODE_WRITE:
1073 res = NFS4_SHARE_ACCESS_BOTH;
1074 }
1075 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1076 goto out;
1077 /* Want no delegation if we're using O_DIRECT */
1078 if (openflags & O_DIRECT)
1079 res |= NFS4_SHARE_WANT_NO_DELEG;
1080out:
1081 return res;
1082}
1083
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001084static enum open_claim_type4
1085nfs4_map_atomic_open_claim(struct nfs_server *server,
1086 enum open_claim_type4 claim)
1087{
1088 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1089 return claim;
1090 switch (claim) {
1091 default:
1092 return claim;
1093 case NFS4_OPEN_CLAIM_FH:
1094 return NFS4_OPEN_CLAIM_NULL;
1095 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1096 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1097 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1098 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1099 }
1100}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001103{
1104 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001105 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001106 p->o_res.seqid = p->o_arg.seqid;
1107 p->c_res.seqid = p->c_arg.seqid;
1108 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001109 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001110 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001111 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001112}
1113
Al Viro82a2c1b2011-06-22 18:30:55 -04001114static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001115 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001116 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001117 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001118 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001119 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001120{
Al Viro82a2c1b2011-06-22 18:30:55 -04001121 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001122 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001123 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001124 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001125 struct nfs4_opendata *p;
1126
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001127 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001128 if (p == NULL)
1129 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001130
1131 p->f_label = nfs4_label_alloc(server, gfp_mask);
1132 if (IS_ERR(p->f_label))
1133 goto err_free_p;
1134
Kinglong Meea49c2692015-07-27 15:31:38 +08001135 p->a_label = nfs4_label_alloc(server, gfp_mask);
1136 if (IS_ERR(p->a_label))
1137 goto err_free_f;
1138
Trond Myklebust63f5f792015-01-23 19:19:25 -05001139 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1140 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001141 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001142 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001143 nfs_sb_active(dentry->d_sb);
1144 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001145 p->dir = parent;
1146 p->owner = sp;
1147 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001148 p->o_arg.open_flags = flags;
1149 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001150 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001151 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001152 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1153 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001154 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1155 * will return permission denied for all bits until close */
1156 if (!(flags & O_EXCL)) {
1157 /* ask server to check for all possible rights as results
1158 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001159 switch (p->o_arg.claim) {
1160 default:
1161 break;
1162 case NFS4_OPEN_CLAIM_NULL:
1163 case NFS4_OPEN_CLAIM_FH:
1164 p->o_arg.access = NFS4_ACCESS_READ |
1165 NFS4_ACCESS_MODIFY |
1166 NFS4_ACCESS_EXTEND |
1167 NFS4_ACCESS_EXECUTE;
1168 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001169 }
David Howells7539bba2006-08-22 20:06:09 -04001170 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001171 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1172 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001173 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001174 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001175 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001176 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001177 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001178 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001179 case NFS4_OPEN_CLAIM_NULL:
1180 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1181 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1182 p->o_arg.fh = NFS_FH(dir);
1183 break;
1184 case NFS4_OPEN_CLAIM_PREVIOUS:
1185 case NFS4_OPEN_CLAIM_FH:
1186 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1187 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001188 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001189 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001190 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001191 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001192
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001193 p->o_arg.u.attrs = &p->attrs;
1194 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001195
1196 verf[0] = jiffies;
1197 verf[1] = current->pid;
1198 memcpy(p->o_arg.u.verifier.data, verf,
1199 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001200 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001201 p->c_arg.fh = &p->o_res.fh;
1202 p->c_arg.stateid = &p->o_res.stateid;
1203 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001204 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001205 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001206 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001207
1208err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001209 nfs4_label_free(p->a_label);
1210err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001211 nfs4_label_free(p->f_label);
1212err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001213 kfree(p);
1214err:
1215 dput(parent);
1216 return NULL;
1217}
1218
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001219static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001220{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001221 struct nfs4_opendata *p = container_of(kref,
1222 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001223 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001224
1225 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001226 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001227 if (p->state != NULL)
1228 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001229 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001230
Kinglong Meea49c2692015-07-27 15:31:38 +08001231 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001232 nfs4_label_free(p->f_label);
1233
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001234 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001235 dput(p->dentry);
1236 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001237 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001238 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001239 kfree(p);
1240}
1241
1242static void nfs4_opendata_put(struct nfs4_opendata *p)
1243{
1244 if (p != NULL)
1245 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001246}
1247
Trond Myklebust24311f82015-09-20 10:50:17 -04001248static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1249 fmode_t fmode)
1250{
1251 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1252 case FMODE_READ|FMODE_WRITE:
1253 return state->n_rdwr != 0;
1254 case FMODE_WRITE:
1255 return state->n_wronly != 0;
1256 case FMODE_READ:
1257 return state->n_rdonly != 0;
1258 }
1259 WARN_ON_ONCE(1);
1260 return false;
1261}
1262
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001263static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001264{
1265 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001266
Trond Myklebust536e43d2012-01-17 22:04:26 -05001267 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001268 goto out;
1269 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001270 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001271 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1272 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001273 break;
1274 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001275 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1276 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001277 break;
1278 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001279 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1280 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001281 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001282out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001283 return ret;
1284}
1285
Trond Myklebust2a606182015-08-19 22:30:00 -05001286static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1287 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001288{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001289 if (delegation == NULL)
1290 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001291 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001292 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001293 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1294 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001295 switch (claim) {
1296 case NFS4_OPEN_CLAIM_NULL:
1297 case NFS4_OPEN_CLAIM_FH:
1298 break;
1299 case NFS4_OPEN_CLAIM_PREVIOUS:
1300 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1301 break;
1302 default:
1303 return 0;
1304 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001305 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001306 return 1;
1307}
1308
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001309static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001310{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001311 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001312 case FMODE_WRITE:
1313 state->n_wronly++;
1314 break;
1315 case FMODE_READ:
1316 state->n_rdonly++;
1317 break;
1318 case FMODE_READ|FMODE_WRITE:
1319 state->n_rdwr++;
1320 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001321 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001322}
1323
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001324#ifdef CONFIG_NFS_V4_1
1325static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1326{
1327 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1328 return true;
1329 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1330 return true;
1331 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1332 return true;
1333 return false;
1334}
1335#endif /* CONFIG_NFS_V4_1 */
1336
Trond Myklebust4f14c192014-02-12 19:15:06 -05001337static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001338{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001339 struct nfs_client *clp = state->owner->so_server->nfs_client;
1340 bool need_recover = false;
1341
1342 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1343 need_recover = true;
1344 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1345 need_recover = true;
1346 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1347 need_recover = true;
1348 if (need_recover)
1349 nfs4_state_mark_reclaim_nograce(clp, state);
1350}
1351
Trond Myklebuste999e802014-02-10 18:20:47 -05001352static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001353 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001354{
1355 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1356 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001357 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001358 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001359 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001360 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001361 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001362 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1363 return true;
1364 return false;
1365}
1366
Trond Myklebustf95549c2015-01-23 18:06:09 -05001367static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1368{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001369 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1370 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001371 if (state->n_wronly)
1372 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1373 if (state->n_rdonly)
1374 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1375 if (state->n_rdwr)
1376 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001377 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001378}
1379
Trond Myklebust226056c2014-02-11 10:41:07 -05001380static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1381 nfs4_stateid *stateid, fmode_t fmode)
1382{
1383 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1384 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1385 case FMODE_WRITE:
1386 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1387 break;
1388 case FMODE_READ:
1389 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1390 break;
1391 case 0:
1392 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1393 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1394 clear_bit(NFS_OPEN_STATE, &state->flags);
1395 }
1396 if (stateid == NULL)
1397 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001398 /* Handle OPEN+OPEN_DOWNGRADE races */
1399 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1400 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001401 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001402 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001403 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001404 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001405 nfs4_stateid_copy(&state->stateid, stateid);
1406 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001407}
1408
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001409static void nfs_clear_open_stateid(struct nfs4_state *state,
1410 nfs4_stateid *arg_stateid,
1411 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001412{
1413 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001414 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1415 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1416 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001417 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001418 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1419 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001420}
1421
Trond Myklebust1393d962016-09-22 13:39:13 -04001422static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1423 const nfs4_stateid *stateid, fmode_t fmode,
1424 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001425{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001426 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001427 case FMODE_READ:
1428 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1429 break;
1430 case FMODE_WRITE:
1431 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1432 break;
1433 case FMODE_READ|FMODE_WRITE:
1434 set_bit(NFS_O_RDWR_STATE, &state->flags);
1435 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001436 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001437 return;
1438 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1439 nfs4_stateid_copy(&state->stateid, stateid);
1440 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001441}
1442
Trond Myklebust1393d962016-09-22 13:39:13 -04001443static void __update_open_stateid(struct nfs4_state *state,
1444 const nfs4_stateid *open_stateid,
1445 const nfs4_stateid *deleg_stateid,
1446 fmode_t fmode,
1447 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001449 /*
1450 * Protect the call to nfs4_state_set_mode_locked and
1451 * serialise the stateid update
1452 */
Andrew Elble361cad32015-12-02 09:20:57 -05001453 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001454 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001455 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001456 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001457 set_bit(NFS_DELEGATED_STATE, &state->flags);
1458 }
1459 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001460 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001461 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001462 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001463 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Trond Myklebust1393d962016-09-22 13:39:13 -04001466static int update_open_stateid(struct nfs4_state *state,
1467 const nfs4_stateid *open_stateid,
1468 const nfs4_stateid *delegation,
1469 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001470{
Trond Myklebust1393d962016-09-22 13:39:13 -04001471 struct nfs_server *server = NFS_SERVER(state->inode);
1472 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001473 struct nfs_inode *nfsi = NFS_I(state->inode);
1474 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001475 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001476 int ret = 0;
1477
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001478 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001479
1480 rcu_read_lock();
1481 deleg_cur = rcu_dereference(nfsi->delegation);
1482 if (deleg_cur == NULL)
1483 goto no_delegation;
1484
1485 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001486 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001487 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001488 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001489 goto no_delegation_unlock;
1490
1491 if (delegation == NULL)
1492 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001493 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001494 goto no_delegation_unlock;
1495
Trond Myklebustb7391f42008-12-23 15:21:52 -05001496 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001497 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1498 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001499 ret = 1;
1500no_delegation_unlock:
1501 spin_unlock(&deleg_cur->lock);
1502no_delegation:
1503 rcu_read_unlock();
1504
1505 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001506 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001507 ret = 1;
1508 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001509 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001510 nfs4_schedule_state_manager(clp);
1511 if (freeme.type != 0)
1512 nfs4_test_and_free_stateid(server, &freeme,
1513 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001514
1515 return ret;
1516}
1517
Trond Myklebust39071e62015-01-24 15:07:56 -05001518static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1519 const nfs4_stateid *stateid)
1520{
1521 struct nfs4_state *state = lsp->ls_state;
1522 bool ret = false;
1523
1524 spin_lock(&state->state_lock);
1525 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1526 goto out_noupdate;
1527 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1528 goto out_noupdate;
1529 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1530 ret = true;
1531out_noupdate:
1532 spin_unlock(&state->state_lock);
1533 return ret;
1534}
Trond Myklebust34310432008-12-23 15:21:38 -05001535
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001536static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001537{
1538 struct nfs_delegation *delegation;
1539
1540 rcu_read_lock();
1541 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001542 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001543 rcu_read_unlock();
1544 return;
1545 }
1546 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001547 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001548}
1549
Trond Myklebust6ee41262007-07-08 14:11:36 -04001550static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001551{
1552 struct nfs4_state *state = opendata->state;
1553 struct nfs_inode *nfsi = NFS_I(state->inode);
1554 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001555 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001556 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001557 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001558 nfs4_stateid stateid;
1559 int ret = -EAGAIN;
1560
Trond Myklebustaac00a82007-07-05 19:02:21 -04001561 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001562 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001563 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001564 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001565 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001566 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001567 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001568 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001569 rcu_read_lock();
1570 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001571 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001572 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001573 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001574 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001575 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001576 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001577 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001578 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001579 if (!opendata->is_recover) {
1580 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1581 if (ret != 0)
1582 goto out;
1583 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001584 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001585
1586 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001587 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001588 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001589 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001590out:
1591 return ERR_PTR(ret);
1592out_return_state:
1593 atomic_inc(&state->count);
1594 return state;
1595}
1596
Andy Adamsone23008e2012-10-02 21:07:32 -04001597static void
1598nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1599{
1600 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1601 struct nfs_delegation *delegation;
1602 int delegation_flags = 0;
1603
1604 rcu_read_lock();
1605 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1606 if (delegation)
1607 delegation_flags = delegation->flags;
1608 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001609 switch (data->o_arg.claim) {
1610 default:
1611 break;
1612 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1613 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001614 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1615 "returning a delegation for "
1616 "OPEN(CLAIM_DELEGATE_CUR)\n",
1617 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001618 return;
1619 }
1620 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001621 nfs_inode_set_delegation(state->inode,
1622 data->owner->so_cred,
1623 &data->o_res);
1624 else
1625 nfs_inode_reclaim_delegation(state->inode,
1626 data->owner->so_cred,
1627 &data->o_res);
1628}
1629
1630/*
1631 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1632 * and update the nfs4_state.
1633 */
1634static struct nfs4_state *
1635_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1636{
1637 struct inode *inode = data->state->inode;
1638 struct nfs4_state *state = data->state;
1639 int ret;
1640
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001641 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001642 if (data->rpc_status)
1643 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001644 /* cached opens have already been processed */
1645 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001646 }
1647
Andy Adamsone23008e2012-10-02 21:07:32 -04001648 ret = nfs_refresh_inode(inode, &data->f_attr);
1649 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001650 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001651
1652 if (data->o_res.delegation_type != 0)
1653 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001654update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001655 update_open_stateid(state, &data->o_res.stateid, NULL,
1656 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001657 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001658
1659 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001660}
1661
1662static struct nfs4_state *
1663_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001664{
1665 struct inode *inode;
1666 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001667 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001668
Trond Myklebustaac00a82007-07-05 19:02:21 -04001669 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001670 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001671 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001672 goto out;
1673 }
1674
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001675 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001676 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001677 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001678 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001679 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001680 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001681 goto err;
1682 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001683 state = nfs4_get_open_state(inode, data->owner);
1684 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001685 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001686 if (data->o_res.delegation_type != 0)
1687 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001688 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001689 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001690 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001691out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001692 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001693 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001694err_put_inode:
1695 iput(inode);
1696err:
1697 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001698}
1699
Andy Adamsone23008e2012-10-02 21:07:32 -04001700static struct nfs4_state *
1701nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1702{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001703 struct nfs4_state *ret;
1704
Andy Adamsone23008e2012-10-02 21:07:32 -04001705 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001706 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1707 else
1708 ret = _nfs4_opendata_to_nfs4_state(data);
1709 nfs4_sequence_free_slot(&data->o_res.seq_res);
1710 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001711}
1712
Trond Myklebust864472e2006-01-03 09:55:15 +01001713static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1714{
1715 struct nfs_inode *nfsi = NFS_I(state->inode);
1716 struct nfs_open_context *ctx;
1717
1718 spin_lock(&state->inode->i_lock);
1719 list_for_each_entry(ctx, &nfsi->open_files, list) {
1720 if (ctx->state != state)
1721 continue;
1722 get_nfs_open_context(ctx);
1723 spin_unlock(&state->inode->i_lock);
1724 return ctx;
1725 }
1726 spin_unlock(&state->inode->i_lock);
1727 return ERR_PTR(-ENOENT);
1728}
1729
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001730static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1731 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001732{
1733 struct nfs4_opendata *opendata;
1734
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001735 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001736 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001737 if (opendata == NULL)
1738 return ERR_PTR(-ENOMEM);
1739 opendata->state = state;
1740 atomic_inc(&state->count);
1741 return opendata;
1742}
1743
Trond Myklebust24311f82015-09-20 10:50:17 -04001744static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1745 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001746{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001747 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001748 int ret;
1749
Trond Myklebust24311f82015-09-20 10:50:17 -04001750 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001751 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001752 opendata->o_arg.open_flags = 0;
1753 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001754 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1755 NFS_SB(opendata->dentry->d_sb),
1756 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001757 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1758 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1759 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001760 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001761 if (ret != 0)
1762 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001763 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001764 if (IS_ERR(newstate))
1765 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001766 if (newstate != opendata->state)
1767 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001768 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001769 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001770}
1771
1772static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1773{
Trond Myklebust864472e2006-01-03 09:55:15 +01001774 int ret;
1775
Trond Myklebust4f14c192014-02-12 19:15:06 -05001776 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1777 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1778 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1779 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001780 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001781 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001782 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001783 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001784 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1785 if (ret != 0)
1786 return ret;
1787 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1788 if (ret != 0)
1789 return ret;
1790 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1791 if (ret != 0)
1792 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001793 /*
1794 * We may have performed cached opens for all three recoveries.
1795 * Check if we need to update the current stateid.
1796 */
1797 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001798 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001799 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001800 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001801 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001802 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001803 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001804 return 0;
1805}
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807/*
1808 * OPEN_RECLAIM:
1809 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001811static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001813 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001814 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001815 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 int status;
1817
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001818 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1819 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001820 if (IS_ERR(opendata))
1821 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001822 rcu_read_lock();
1823 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001824 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001825 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001826 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001827 opendata->o_arg.u.delegation_type = delegation_type;
1828 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001829 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return status;
1831}
1832
Trond Myklebust539cd032007-06-05 11:46:42 -04001833static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
1835 struct nfs_server *server = NFS_SERVER(state->inode);
1836 struct nfs4_exception exception = { };
1837 int err;
1838 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001839 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001840 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001841 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1842 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001843 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001844 break;
1845 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 } while (exception.retry);
1847 return err;
1848}
1849
Trond Myklebust864472e2006-01-03 09:55:15 +01001850static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1851{
1852 struct nfs_open_context *ctx;
1853 int ret;
1854
1855 ctx = nfs4_state_find_open_context(state);
1856 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001857 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001858 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001859 put_nfs_open_context(ctx);
1860 return ret;
1861}
1862
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001863static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001865 switch (err) {
1866 default:
1867 printk(KERN_ERR "NFS: %s: unhandled error "
1868 "%d.\n", __func__, err);
1869 case 0:
1870 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001871 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001872 case -ESTALE:
1873 break;
1874 case -NFS4ERR_BADSESSION:
1875 case -NFS4ERR_BADSLOT:
1876 case -NFS4ERR_BAD_HIGH_SLOT:
1877 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1878 case -NFS4ERR_DEADSESSION:
1879 set_bit(NFS_DELEGATED_STATE, &state->flags);
1880 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1881 return -EAGAIN;
1882 case -NFS4ERR_STALE_CLIENTID:
1883 case -NFS4ERR_STALE_STATEID:
1884 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001885 /* Don't recall a delegation if it was lost */
1886 nfs4_schedule_lease_recovery(server->nfs_client);
1887 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001888 case -NFS4ERR_MOVED:
1889 nfs4_schedule_migration_recovery(server);
1890 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001891 case -NFS4ERR_LEASE_MOVED:
1892 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1893 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001894 case -NFS4ERR_DELEG_REVOKED:
1895 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001896 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001897 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001898 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001899 nfs_inode_find_state_and_recover(state->inode,
1900 stateid);
1901 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001902 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001903 case -NFS4ERR_DELAY:
1904 case -NFS4ERR_GRACE:
1905 set_bit(NFS_DELEGATED_STATE, &state->flags);
1906 ssleep(1);
1907 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001908 case -ENOMEM:
1909 case -NFS4ERR_DENIED:
1910 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1911 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return err;
1914}
1915
Trond Myklebust24311f82015-09-20 10:50:17 -04001916int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1917 struct nfs4_state *state, const nfs4_stateid *stateid,
1918 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001919{
1920 struct nfs_server *server = NFS_SERVER(state->inode);
1921 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001922 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001923
1924 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1925 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1926 if (IS_ERR(opendata))
1927 return PTR_ERR(opendata);
1928 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001929 write_seqlock(&state->seqlock);
1930 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1931 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001932 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1933 switch (type & (FMODE_READ|FMODE_WRITE)) {
1934 case FMODE_READ|FMODE_WRITE:
1935 case FMODE_WRITE:
1936 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1937 if (err)
1938 break;
1939 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1940 if (err)
1941 break;
1942 case FMODE_READ:
1943 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1944 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001945 nfs4_opendata_put(opendata);
1946 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1947}
1948
Chuck Leverbe05c862013-08-09 12:49:47 -04001949static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1950{
1951 struct nfs4_opendata *data = calldata;
1952
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001953 nfs4_setup_sequence(data->o_arg.server->nfs_client,
1954 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001955}
1956
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001957static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1958{
1959 struct nfs4_opendata *data = calldata;
1960
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001961 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001962
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001963 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001964 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001965 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001966 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001967 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001968 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001969 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001970}
1971
1972static void nfs4_open_confirm_release(void *calldata)
1973{
1974 struct nfs4_opendata *data = calldata;
1975 struct nfs4_state *state = NULL;
1976
1977 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001978 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001979 goto out_free;
1980 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001981 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001982 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001983 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001984 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001985 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001986out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001987 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001988}
1989
1990static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001991 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001992 .rpc_call_done = nfs4_open_confirm_done,
1993 .rpc_release = nfs4_open_confirm_release,
1994};
1995
1996/*
1997 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1998 */
1999static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2000{
David Howells2b0143b2015-03-17 22:25:59 +00002001 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002002 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002003 struct rpc_message msg = {
2004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2005 .rpc_argp = &data->c_arg,
2006 .rpc_resp = &data->c_res,
2007 .rpc_cred = data->owner->so_cred,
2008 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002009 struct rpc_task_setup task_setup_data = {
2010 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002011 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002012 .callback_ops = &nfs4_open_confirm_ops,
2013 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002014 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002015 .flags = RPC_TASK_ASYNC,
2016 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 int status;
2018
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002019 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002020 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002021 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002022 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002023 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002024 if (data->is_recover)
2025 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002026 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002027 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002028 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002029 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002030 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002031 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002032 smp_wmb();
2033 } else
2034 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002035 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return status;
2037}
2038
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002039static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002041 struct nfs4_opendata *data = calldata;
2042 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002043 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002044 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002045
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002046 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002047 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002048 /*
2049 * Check if we still need to send an OPEN call, or if we can use
2050 * a delegation instead.
2051 */
2052 if (data->state != NULL) {
2053 struct nfs_delegation *delegation;
2054
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002055 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002056 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002057 rcu_read_lock();
2058 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002059 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002060 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002061 rcu_read_unlock();
2062 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002063 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002064 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002065 switch (claim) {
2066 default:
2067 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002068 case NFS4_OPEN_CLAIM_PREVIOUS:
2069 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2070 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002071 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002072 case NFS4_OPEN_CLAIM_FH:
2073 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002074 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2075 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002076 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002077 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002078 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002079 &data->o_res.seq_res,
2080 task) != 0)
2081 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002082
2083 /* Set the create mode (note dependency on the session type) */
2084 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2085 if (data->o_arg.open_flags & O_EXCL) {
2086 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2087 if (nfs4_has_persistent_session(clp))
2088 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2089 else if (clp->cl_mvops->minor_version > 0)
2090 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2091 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002092 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002093unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002094 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002095 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002096out_no_action:
2097 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002098out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002099 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002100}
2101
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002102static void nfs4_open_done(struct rpc_task *task, void *calldata)
2103{
2104 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002106 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002107
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002108 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002109 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002110
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002111 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002112 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2113 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002114 case S_IFREG:
2115 break;
2116 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002117 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002118 break;
2119 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002120 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002121 break;
2122 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002123 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002124 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002125 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002126 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002127 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2128 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002129 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002130 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002131}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002132
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002133static void nfs4_open_release(void *calldata)
2134{
2135 struct nfs4_opendata *data = calldata;
2136 struct nfs4_state *state = NULL;
2137
2138 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002139 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002140 goto out_free;
2141 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002142 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002143 goto out_free;
2144 /* In case we need an open_confirm, no cleanup! */
2145 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2146 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002147 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002148 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002149 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002150out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002151 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002152}
2153
2154static const struct rpc_call_ops nfs4_open_ops = {
2155 .rpc_call_prepare = nfs4_open_prepare,
2156 .rpc_call_done = nfs4_open_done,
2157 .rpc_release = nfs4_open_release,
2158};
2159
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002160static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002161{
David Howells2b0143b2015-03-17 22:25:59 +00002162 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002163 struct nfs_server *server = NFS_SERVER(dir);
2164 struct nfs_openargs *o_arg = &data->o_arg;
2165 struct nfs_openres *o_res = &data->o_res;
2166 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002167 struct rpc_message msg = {
2168 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2169 .rpc_argp = o_arg,
2170 .rpc_resp = o_res,
2171 .rpc_cred = data->owner->so_cred,
2172 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002173 struct rpc_task_setup task_setup_data = {
2174 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002175 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002176 .callback_ops = &nfs4_open_ops,
2177 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002178 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002179 .flags = RPC_TASK_ASYNC,
2180 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002181 int status;
2182
Chuck Levera9c92d62013-08-09 12:48:18 -04002183 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002184 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002185 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002186 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002187 data->cancelled = false;
2188 data->is_recover = false;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002189 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002190 nfs4_set_sequence_privileged(&o_arg->seq_args);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002191 data->is_recover = true;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002192 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002193 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002194 if (IS_ERR(task))
2195 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002196 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002197 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002198 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002199 smp_wmb();
2200 } else
2201 status = data->rpc_status;
2202 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002203
2204 return status;
2205}
2206
2207static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2208{
David Howells2b0143b2015-03-17 22:25:59 +00002209 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002210 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002211 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002212
2213 status = nfs4_run_open_task(data, 1);
2214 if (status != 0 || !data->rpc_done)
2215 return status;
2216
Trond Myklebust6926afd2012-01-07 13:22:46 -05002217 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2218
Anna Schumakerd7e98252017-01-11 16:13:29 -05002219 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002220 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002221
2222 return status;
2223}
2224
Trond Myklebustf3792d62014-07-10 08:54:32 -04002225/*
2226 * Additional permission checks in order to distinguish between an
2227 * open for read, and an open for execute. This works around the
2228 * fact that NFSv4 OPEN treats read and execute permissions as being
2229 * the same.
2230 * Note that in the non-execute case, we want to turn off permission
2231 * checking if we just created a new file (POSIX open() semantics).
2232 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002233static int nfs4_opendata_access(struct rpc_cred *cred,
2234 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002235 struct nfs4_state *state, fmode_t fmode,
2236 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002237{
2238 struct nfs_access_entry cache;
2239 u32 mask;
2240
2241 /* access call failed or for some reason the server doesn't
2242 * support any access modes -- defer access call until later */
2243 if (opendata->o_res.access_supported == 0)
2244 return 0;
2245
2246 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002247 /*
2248 * Use openflags to check for exec, because fmode won't
2249 * always have FMODE_EXEC set when file open for exec.
2250 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002251 if (openflags & __FMODE_EXEC) {
2252 /* ONLY check for exec rights */
2253 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002254 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002255 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002256
2257 cache.cred = cred;
2258 cache.jiffies = jiffies;
2259 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2260 nfs_access_add_cache(state->inode, &cache);
2261
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002262 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002263 return 0;
2264
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002265 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002266}
2267
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002268/*
2269 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2270 */
2271static int _nfs4_proc_open(struct nfs4_opendata *data)
2272{
David Howells2b0143b2015-03-17 22:25:59 +00002273 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002274 struct nfs_server *server = NFS_SERVER(dir);
2275 struct nfs_openargs *o_arg = &data->o_arg;
2276 struct nfs_openres *o_res = &data->o_res;
2277 int status;
2278
2279 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002280 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002281 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002282 if (status != 0) {
2283 if (status == -NFS4ERR_BADNAME &&
2284 !(o_arg->open_flags & O_CREAT))
2285 return -ENOENT;
2286 return status;
2287 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002288
Trond Myklebust6926afd2012-01-07 13:22:46 -05002289 nfs_fattr_map_and_free_names(server, &data->f_attr);
2290
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002291 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002292 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002293 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002294 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002295 data->file_created = true;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002296 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002297 update_changeattr(dir, &o_res->cinfo,
2298 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002299 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002300 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2301 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002303 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002305 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002307 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2308 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002309 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002310 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002311 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314/*
2315 * OPEN_EXPIRED:
2316 * reclaim state on the server after a network partition.
2317 * Assumes caller holds the appropriate lock
2318 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002319static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002321 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002322 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002324 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002325 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002326 if (IS_ERR(opendata))
2327 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002328 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002329 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002330 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002331 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002332 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333}
2334
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002335static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002336{
Trond Myklebust539cd032007-06-05 11:46:42 -04002337 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002338 struct nfs4_exception exception = { };
2339 int err;
2340
2341 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002342 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002343 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002344 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2345 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002346 switch (err) {
2347 default:
2348 goto out;
2349 case -NFS4ERR_GRACE:
2350 case -NFS4ERR_DELAY:
2351 nfs4_handle_exception(server, err, &exception);
2352 err = 0;
2353 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002354 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002355out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002356 return err;
2357}
2358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2360{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002362 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Trond Myklebust864472e2006-01-03 09:55:15 +01002364 ctx = nfs4_state_find_open_context(state);
2365 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002366 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002367 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002368 put_nfs_open_context(ctx);
2369 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370}
2371
Trond Myklebust41020b62016-09-22 13:38:58 -04002372static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2373 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002374{
Trond Myklebust41020b62016-09-22 13:38:58 -04002375 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002376 write_seqlock(&state->seqlock);
2377 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2378 write_sequnlock(&state->seqlock);
2379 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2380}
2381
2382static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2383{
2384 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002385 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002386}
2387
2388static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2389{
2390 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2391 nfs40_clear_delegation_stateid(state);
2392 return nfs4_open_expired(sp, state);
2393}
2394
Trond Myklebust45870d62016-09-22 13:38:59 -04002395static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2396 nfs4_stateid *stateid,
2397 struct rpc_cred *cred)
2398{
2399 return -NFS4ERR_BAD_STATEID;
2400}
2401
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002402#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002403static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2404 nfs4_stateid *stateid,
2405 struct rpc_cred *cred)
2406{
2407 int status;
2408
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002409 switch (stateid->type) {
2410 default:
2411 break;
2412 case NFS4_INVALID_STATEID_TYPE:
2413 case NFS4_SPECIAL_STATEID_TYPE:
2414 return -NFS4ERR_BAD_STATEID;
2415 case NFS4_REVOKED_STATEID_TYPE:
2416 goto out_free;
2417 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002418
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002419 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002420 switch (status) {
2421 case -NFS4ERR_EXPIRED:
2422 case -NFS4ERR_ADMIN_REVOKED:
2423 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002424 break;
2425 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002426 return status;
2427 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002428out_free:
2429 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002430 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002431 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002432}
2433
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002434static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002435{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002436 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002437 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002438 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002439 struct rpc_cred *cred;
2440 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002441
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002442 /* Get the delegation credential for use by test/free_stateid */
2443 rcu_read_lock();
2444 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002445 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002446 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002447 return;
2448 }
2449
2450 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002451 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2452 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2453 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002454 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002455 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002456 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002457 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002458
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002459 cred = get_rpccred(delegation->cred);
2460 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002461 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002462 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002463 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002464 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002465
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002466 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002467}
2468
2469/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002470 * nfs41_check_expired_locks - possibly free a lock stateid
2471 *
2472 * @state: NFSv4 state for an inode
2473 *
2474 * Returns NFS_OK if recovery for this stateid is now finished.
2475 * Otherwise a negative NFS4ERR value is returned.
2476 */
2477static int nfs41_check_expired_locks(struct nfs4_state *state)
2478{
2479 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002480 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002481 struct nfs_server *server = NFS_SERVER(state->inode);
2482
2483 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2484 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002485
2486 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002487 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2488 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2489 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2490
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002491 atomic_inc(&lsp->ls_count);
2492 spin_unlock(&state->state_lock);
2493
2494 nfs4_put_lock_state(prev);
2495 prev = lsp;
2496
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002497 status = nfs41_test_and_free_expired_stateid(server,
2498 &lsp->ls_stateid,
2499 cred);
2500 trace_nfs4_test_lock_stateid(state, lsp, status);
2501 if (status == -NFS4ERR_EXPIRED ||
2502 status == -NFS4ERR_BAD_STATEID) {
2503 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002504 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002505 if (!recover_lost_locks)
2506 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2507 } else if (status != NFS_OK) {
2508 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002509 nfs4_put_lock_state(prev);
2510 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002511 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002512 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002513 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002514 }
2515 spin_unlock(&state->state_lock);
2516 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002517out:
2518 return ret;
2519}
2520
2521/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002522 * nfs41_check_open_stateid - possibly free an open stateid
2523 *
2524 * @state: NFSv4 state for an inode
2525 *
2526 * Returns NFS_OK if recovery for this stateid is now finished.
2527 * Otherwise a negative NFS4ERR value is returned.
2528 */
2529static int nfs41_check_open_stateid(struct nfs4_state *state)
2530{
2531 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002532 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002533 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002534 int status;
2535
Trond Myklebustb134fc42016-09-22 13:39:16 -04002536 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002537 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2538 if (nfs4_have_delegation(state->inode, state->state))
2539 return NFS_OK;
2540 return -NFS4ERR_OPENMODE;
2541 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002542 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002543 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002544 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002545 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002546 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002547 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2548 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2549 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002550 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002551 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002552 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002553 if (status != NFS_OK)
2554 return status;
2555 if (nfs_open_stateid_recover_openmode(state))
2556 return -NFS4ERR_OPENMODE;
2557 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002558}
2559
2560static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2561{
Chuck Levereb64cf92012-07-11 16:30:05 -04002562 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002563
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002564 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002565 status = nfs41_check_expired_locks(state);
2566 if (status != NFS_OK)
2567 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002568 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002569 if (status != NFS_OK)
2570 status = nfs4_open_expired(sp, state);
2571 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002572}
2573#endif
2574
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002576 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2577 * fields corresponding to attributes that were used to store the verifier.
2578 * Make sure we clobber those fields in the later setattr call
2579 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002580static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2581 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002582{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002583 const u32 *attrset = opendata->o_res.attrset;
2584
2585 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002586 !(sattr->ia_valid & ATTR_ATIME_SET))
2587 sattr->ia_valid |= ATTR_ATIME;
2588
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002589 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002590 !(sattr->ia_valid & ATTR_MTIME_SET))
2591 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002592
2593 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002594 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2595 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002596 sattr->ia_valid &= ~ATTR_MODE;
2597
2598 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2599 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002600}
2601
Trond Myklebustc21443c2013-02-07 14:26:21 -05002602static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2603 fmode_t fmode,
2604 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002605 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002606{
2607 struct nfs4_state_owner *sp = opendata->owner;
2608 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002609 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002610 struct nfs4_state *state;
2611 unsigned int seq;
2612 int ret;
2613
2614 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2615
2616 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002617 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002618 goto out;
2619
2620 state = nfs4_opendata_to_nfs4_state(opendata);
2621 ret = PTR_ERR(state);
2622 if (IS_ERR(state))
2623 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002624 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002625 if (server->caps & NFS_CAP_POSIX_LOCK)
2626 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002627 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2628 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002629
Trond Myklebust275bb302013-05-29 13:11:28 -04002630 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002631 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002632 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002633 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002634 alias = d_exact_alias(dentry, state->inode);
2635 if (!alias)
2636 alias = d_splice_alias(igrab(state->inode), dentry);
2637 /* d_splice_alias() can't fail here - it's a non-directory */
2638 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002639 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002640 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002641 }
2642 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002643 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002644 }
2645
Trond Myklebustc21443c2013-02-07 14:26:21 -05002646 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2647 if (ret != 0)
2648 goto out;
2649
David Howells2b0143b2015-03-17 22:25:59 +00002650 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002651 nfs_inode_attach_open_context(ctx);
2652 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2653 nfs4_schedule_stateid_recovery(server, state);
2654 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002655out:
2656 return ret;
2657}
2658
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002659/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002660 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 */
Andy Adamson82be4172012-05-23 05:02:35 -04002662static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002663 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002664 int flags,
2665 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002666 struct nfs4_label *label,
2667 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
2669 struct nfs4_state_owner *sp;
2670 struct nfs4_state *state = NULL;
2671 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002672 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002673 struct dentry *dentry = ctx->dentry;
2674 struct rpc_cred *cred = ctx->cred;
2675 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2676 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002677 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002678 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002679 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
2681 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002683 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2684 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2686 goto out_err;
2687 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002688 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002689 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002690 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002691 if (d_really_is_positive(dentry))
2692 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002693 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002694 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002695 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002696 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002697 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002698 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002699 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
David Quigley14c43f72013-05-22 12:50:43 -04002701 if (label) {
2702 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2703 if (IS_ERR(olabel)) {
2704 status = PTR_ERR(olabel);
2705 goto err_opendata_put;
2706 }
2707 }
2708
Trond Myklebuste911b812014-03-26 13:24:37 -07002709 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2710 if (!opendata->f_attr.mdsthreshold) {
2711 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2712 if (!opendata->f_attr.mdsthreshold)
2713 goto err_free_label;
2714 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002715 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002716 }
David Howells2b0143b2015-03-17 22:25:59 +00002717 if (d_really_is_positive(dentry))
2718 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002719
Trond Myklebust3efb9722013-05-29 13:17:04 -04002720 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002721 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002722 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002723 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002724
NeilBrownefcbc042015-07-30 13:00:56 +10002725 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002726 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002727 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002728 /*
2729 * send create attributes which was not set by open
2730 * with an extra setattr.
2731 */
2732 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2733 nfs_fattr_init(opendata->o_res.f_attr);
2734 status = nfs4_do_setattr(state->inode, cred,
2735 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002736 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002737 if (status == 0) {
2738 nfs_setattr_update_inode(state->inode, sattr,
2739 opendata->o_res.f_attr);
2740 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2741 }
David Quigley1775fd32013-05-22 12:50:42 -04002742 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002743 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002744 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002745 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002746
Trond Myklebuste911b812014-03-26 13:24:37 -07002747 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002748 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002749 opendata->f_attr.mdsthreshold = NULL;
2750 }
Andy Adamson82be4172012-05-23 05:02:35 -04002751
David Quigley14c43f72013-05-22 12:50:43 -04002752 nfs4_label_free(olabel);
2753
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002754 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002757err_free_label:
2758 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002759err_opendata_put:
2760 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002761err_put_state_owner:
2762 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 return status;
2765}
2766
2767
Andy Adamson82be4172012-05-23 05:02:35 -04002768static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002769 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002770 int flags,
2771 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002772 struct nfs4_label *label,
2773 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002775 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 struct nfs4_exception exception = { };
2777 struct nfs4_state *res;
2778 int status;
2779
2780 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002781 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002782 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002783 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 if (status == 0)
2785 break;
2786 /* NOTE: BAD_SEQID means the server and client disagree about the
2787 * book-keeping w.r.t. state-changing operations
2788 * (OPEN/CLOSE/LOCK/LOCKU...)
2789 * It is actually a sign of a bug on the client or on the server.
2790 *
2791 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002792 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 * have unhashed the old state_owner for us, and that we can
2794 * therefore safely retry using a new one. We should still warn
2795 * the user though...
2796 */
2797 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002798 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002799 " returned a bad sequence-id error!\n",
2800 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 exception.retry = 1;
2802 continue;
2803 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002804 /*
2805 * BAD_STATEID on OPEN means that the server cancelled our
2806 * state before it received the OPEN_CONFIRM.
2807 * Recover by retrying the request as per the discussion
2808 * on Page 181 of RFC3530.
2809 */
2810 if (status == -NFS4ERR_BAD_STATEID) {
2811 exception.retry = 1;
2812 continue;
2813 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002814 if (status == -EAGAIN) {
2815 /* We must have found a delegation */
2816 exception.retry = 1;
2817 continue;
2818 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002819 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2820 continue;
2821 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 status, &exception));
2823 } while (exception.retry);
2824 return res;
2825}
2826
Trond Myklebust8487c472016-06-26 08:44:35 -04002827static int _nfs4_do_setattr(struct inode *inode,
2828 struct nfs_setattrargs *arg,
2829 struct nfs_setattrres *res,
2830 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002831 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002832{
2833 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002834 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04002835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2836 .rpc_argp = arg,
2837 .rpc_resp = res,
2838 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002839 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002840 struct rpc_cred *delegation_cred = NULL;
2841 unsigned long timestamp = jiffies;
2842 fmode_t fmode;
2843 bool truncate;
2844 int status;
2845
2846 nfs_fattr_init(res->fattr);
2847
2848 /* Servers should only apply open mode checks for file size changes */
2849 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2850 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2851
2852 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2853 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002854 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002855 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002856 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002857 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002858 l_ctx = nfs_get_lock_context(ctx);
2859 if (IS_ERR(l_ctx))
2860 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002861 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2862 &arg->stateid, &delegation_cred);
2863 nfs_put_lock_context(l_ctx);
2864 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002865 return -EBADF;
2866 } else
2867 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2868 if (delegation_cred)
2869 msg.rpc_cred = delegation_cred;
2870
2871 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2872
2873 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002874 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002875 renew_lease(server, timestamp);
2876 trace_nfs4_setattr(inode, &arg->stateid, status);
2877 return status;
2878}
2879
2880static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2881 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002882 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002883 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002885 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002886 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002887 struct nfs_setattrargs arg = {
2888 .fh = NFS_FH(inode),
2889 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 .server = server,
2891 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002892 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002893 };
2894 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002896 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002898 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002899 struct nfs4_exception exception = {
2900 .state = state,
2901 .inode = inode,
2902 .stateid = &arg.stateid,
2903 };
2904 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
David Quigleyaa9c2662013-05-22 12:50:44 -04002906 arg.bitmask = nfs4_bitmask(server, ilabel);
2907 if (ilabel)
2908 arg.bitmask = nfs4_bitmask(server, olabel);
2909
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 do {
NeilBrown29b59f92016-10-13 15:26:47 +11002911 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04002912 switch (err) {
2913 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002914 if (!(sattr->ia_valid & ATTR_SIZE)) {
2915 pr_warn_once("NFSv4: server %s is incorrectly "
2916 "applying open mode checks to "
2917 "a SETATTR that is not "
2918 "changing file size.\n",
2919 server->nfs_client->cl_hostname);
2920 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002921 if (state && !(state->state & FMODE_WRITE)) {
2922 err = -EBADF;
2923 if (sattr->ia_valid & ATTR_OPEN)
2924 err = -EACCES;
2925 goto out;
2926 }
2927 }
2928 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002930out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 return err;
2932}
2933
Peng Tao500d7012015-09-22 11:35:22 +08002934static bool
2935nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2936{
2937 if (inode == NULL || !nfs_have_layout(inode))
2938 return false;
2939
2940 return pnfs_wait_on_layoutreturn(inode, task);
2941}
2942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943struct nfs4_closedata {
2944 struct inode *inode;
2945 struct nfs4_state *state;
2946 struct nfs_closeargs arg;
2947 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05002948 struct {
2949 struct nfs4_layoutreturn_args arg;
2950 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04002951 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05002952 u32 roc_barrier;
2953 bool roc;
2954 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002955 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002956 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957};
2958
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002959static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002960{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002961 struct nfs4_closedata *calldata = data;
2962 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002963 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002964
Trond Myklebustcf805162016-11-15 14:56:07 -05002965 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05002966 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
2967 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07002968 nfs4_put_open_state(calldata->state);
2969 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002970 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002971 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002972 kfree(calldata);
2973}
2974
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002975static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002977 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002980 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Chuck Levera3ca5652012-03-01 17:00:40 -05002982 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002983 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2984 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002985 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05002986
2987 /* Handle Layoutreturn errors */
2988 if (calldata->arg.lr_args && task->tk_status != 0) {
2989 switch (calldata->res.lr_ret) {
2990 default:
2991 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
2992 break;
2993 case 0:
2994 calldata->arg.lr_args = NULL;
2995 calldata->res.lr_res = NULL;
2996 break;
2997 case -NFS4ERR_ADMIN_REVOKED:
2998 case -NFS4ERR_DELEG_REVOKED:
2999 case -NFS4ERR_EXPIRED:
3000 case -NFS4ERR_BAD_STATEID:
3001 case -NFS4ERR_OLD_STATEID:
3002 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3003 case -NFS4ERR_WRONG_CRED:
3004 calldata->arg.lr_args = NULL;
3005 calldata->res.lr_res = NULL;
3006 calldata->res.lr_ret = 0;
3007 rpc_restart_call_prepare(task);
3008 return;
3009 }
3010 }
3011
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003012 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 * the state_owner. we keep this around to process errors
3014 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 switch (task->tk_status) {
3016 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003017 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003018 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003019 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003020 case -NFS4ERR_ACCESS:
3021 if (calldata->arg.bitmask != NULL) {
3022 calldata->arg.bitmask = NULL;
3023 calldata->res.fattr = NULL;
3024 task->tk_status = 0;
3025 rpc_restart_call_prepare(task);
3026 goto out_release;
3027
3028 }
3029 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003030 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003032 case -NFS4ERR_EXPIRED:
3033 nfs4_free_revoked_stateid(server,
3034 &calldata->arg.stateid,
3035 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003036 case -NFS4ERR_OLD_STATEID:
3037 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003038 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003039 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003040 rpc_restart_call_prepare(task);
3041 goto out_release;
3042 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003043 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003044 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003046 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003047 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003048 goto out_release;
3049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003051 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3052 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003053out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003054 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003055 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003056 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057}
3058
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003059static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003061 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003062 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003063 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003064 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003065 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003066
Chuck Levera3ca5652012-03-01 17:00:40 -05003067 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003068 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003069 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003070
Trond Myklebust88069f72009-12-08 08:33:16 -05003071 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003072 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003073 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3074 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3075 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003076 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003077 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003078 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003079 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003080 if (state->n_rdonly == 0)
3081 call_close |= is_rdonly;
3082 else if (is_rdonly)
3083 calldata->arg.fmode |= FMODE_READ;
3084 if (state->n_wronly == 0)
3085 call_close |= is_wronly;
3086 else if (is_wronly)
3087 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003088 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3089 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003090 } else if (is_rdwr)
3091 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3092
Trond Myklebust5cc78612016-11-14 11:19:56 -05003093 if (!nfs4_valid_open_stateid(state) ||
3094 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003095 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003096 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003097
3098 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003099 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003100 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003101 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003102
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003103 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003104 nfs_release_seqid(calldata->arg.seqid);
3105 goto out_wait;
3106 }
3107
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003108 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003109 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003110
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003111 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003112 /* Close-to-open cache consistency revalidation */
3113 if (!nfs4_have_delegation(inode, FMODE_READ))
3114 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3115 else
3116 calldata->arg.bitmask = NULL;
3117 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003118
Trond Myklebust6ae37332015-01-30 14:21:14 -05003119 calldata->arg.share_access =
3120 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3121 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003122
Trond Myklebustd8d84982016-12-19 12:14:44 -05003123 if (calldata->res.fattr == NULL)
3124 calldata->arg.bitmask = NULL;
3125 else if (calldata->arg.bitmask == NULL)
3126 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003127 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003128 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003129 &calldata->arg.seq_args,
3130 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003131 task) != 0)
3132 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003133 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003134 return;
3135out_no_action:
3136 task->tk_action = NULL;
3137out_wait:
3138 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139}
3140
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003141static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003142 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003143 .rpc_call_done = nfs4_close_done,
3144 .rpc_release = nfs4_free_closedata,
3145};
3146
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147/*
3148 * It is possible for data to be read/written from a mem-mapped file
3149 * after the sys_close call (which hits the vfs layer as a flush).
3150 * This means that we can't safely call nfsv4 close on a file until
3151 * the inode is cleared. This in turn means that we are not good
3152 * NFSv4 citizens - we do not indicate to the server to update the file's
3153 * share state even when we are done with one of the three share
3154 * stateid's in the inode.
3155 *
3156 * NOTE: Caller must be holding the sp->so_owner semaphore!
3157 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003158int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003160 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003161 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003163 struct nfs4_state_owner *sp = state->owner;
3164 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003165 struct rpc_message msg = {
3166 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3167 .rpc_cred = state->owner->so_cred,
3168 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003169 struct rpc_task_setup task_setup_data = {
3170 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003171 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003172 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003173 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003174 .flags = RPC_TASK_ASYNC,
3175 };
Trond Myklebust95121352005-10-18 14:20:12 -07003176 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003178 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3179 &task_setup_data.rpc_client, &msg);
3180
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003181 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003183 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003184 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003185 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003187 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003189 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3190 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003191 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003192 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003193 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003194 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003195 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003196 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003197 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003198 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003199 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003200 calldata->lr.roc = pnfs_roc(state->inode,
3201 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3202 if (calldata->lr.roc) {
3203 calldata->arg.lr_args = &calldata->lr.arg;
3204 calldata->res.lr_res = &calldata->lr.res;
3205 }
Al Viro643168c2011-06-22 18:20:23 -04003206 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003207
Trond Myklebust1174dd12010-12-21 10:52:24 -05003208 msg.rpc_argp = &calldata->arg;
3209 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003210 task_setup_data.callback_data = calldata;
3211 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003212 if (IS_ERR(task))
3213 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003214 status = 0;
3215 if (wait)
3216 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003217 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003218 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003219out_free_calldata:
3220 kfree(calldata);
3221out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003222 nfs4_put_open_state(state);
3223 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003224 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225}
3226
Trond Myklebust2b484292010-09-17 10:56:51 -04003227static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003228nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3229 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003232 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3233
3234 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Trond Myklebust565277f2007-10-15 18:17:53 -04003236 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003237 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003238
3239 nfs4_label_release_security(label);
3240
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003241 if (IS_ERR(state))
3242 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003243 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244}
3245
Trond Myklebust1185a552009-12-03 15:54:02 -05003246static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003247{
3248 if (ctx->state == NULL)
3249 return;
3250 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003251 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003252 else
Al Viro643168c2011-06-22 18:20:23 -04003253 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003254}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
Trond Myklebustb944dba2013-11-04 15:20:20 -05003256#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3257#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003258#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003259
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3261{
Kinglong Mee8c612822015-08-26 21:12:58 +08003262 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003263 struct nfs4_server_caps_arg args = {
3264 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003265 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003266 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 struct nfs4_server_caps_res res = {};
3268 struct rpc_message msg = {
3269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003270 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 .rpc_resp = &res,
3272 };
3273 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003274 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Kinglong Mee8c612822015-08-26 21:12:58 +08003276 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3277 FATTR4_WORD0_FH_EXPIRE_TYPE |
3278 FATTR4_WORD0_LINK_SUPPORT |
3279 FATTR4_WORD0_SYMLINK_SUPPORT |
3280 FATTR4_WORD0_ACLSUPPORT;
3281 if (minorversion)
3282 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3283
Bryan Schumaker7c513052011-03-24 17:12:24 +00003284 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003286 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003287 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003288 case 0:
3289 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3290 res.attr_bitmask[2] = 0;
3291 break;
3292 case 1:
3293 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3294 break;
3295 case 2:
3296 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003299 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3300 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3301 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3302 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003303 NFS_CAP_CTIME|NFS_CAP_MTIME|
3304 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003305 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3306 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 server->caps |= NFS_CAP_ACLS;
3308 if (res.has_links != 0)
3309 server->caps |= NFS_CAP_HARDLINKS;
3310 if (res.has_symlinks != 0)
3311 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003312 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3313 server->caps |= NFS_CAP_FILEID;
3314 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3315 server->caps |= NFS_CAP_MODE;
3316 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3317 server->caps |= NFS_CAP_NLINK;
3318 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3319 server->caps |= NFS_CAP_OWNER;
3320 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3321 server->caps |= NFS_CAP_OWNER_GROUP;
3322 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3323 server->caps |= NFS_CAP_ATIME;
3324 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3325 server->caps |= NFS_CAP_CTIME;
3326 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3327 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003328#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3329 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3330 server->caps |= NFS_CAP_SECURITY_LABEL;
3331#endif
3332 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3333 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003334 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003335
Trond Myklebusta65318b2009-03-11 14:10:28 -04003336 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3337 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3338 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003339 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003340
3341 /* Avoid a regression due to buggy server */
3342 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3343 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003344 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3345 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003346
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003348 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003350
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 return status;
3352}
3353
Trond Myklebust55a97592006-06-09 09:34:19 -04003354int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355{
3356 struct nfs4_exception exception = { };
3357 int err;
3358 do {
3359 err = nfs4_handle_exception(server,
3360 _nfs4_server_capabilities(server, fhandle),
3361 &exception);
3362 } while (exception.retry);
3363 return err;
3364}
3365
3366static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3367 struct nfs_fsinfo *info)
3368{
David Quigleyaa9c2662013-05-22 12:50:44 -04003369 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003371 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 };
3373 struct nfs4_lookup_res res = {
3374 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003375 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 .fh = fhandle,
3377 };
3378 struct rpc_message msg = {
3379 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3380 .rpc_argp = &args,
3381 .rpc_resp = &res,
3382 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003383
David Quigleyaa9c2662013-05-22 12:50:44 -04003384 bitmask[0] = nfs4_fattr_bitmap[0];
3385 bitmask[1] = nfs4_fattr_bitmap[1];
3386 /*
3387 * Process the label in the upcoming getfattr
3388 */
3389 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3390
Trond Myklebust0e574af2005-10-27 22:12:38 -04003391 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003392 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393}
3394
3395static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3396 struct nfs_fsinfo *info)
3397{
3398 struct nfs4_exception exception = { };
3399 int err;
3400 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003401 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003402 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003403 switch (err) {
3404 case 0:
3405 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003406 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003407 default:
3408 err = nfs4_handle_exception(server, err, &exception);
3409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003411out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 return err;
3413}
3414
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003415static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3416 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3417{
Trond Myklebustc2190662013-08-26 19:23:04 -04003418 struct rpc_auth_create_args auth_args = {
3419 .pseudoflavor = flavor,
3420 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003421 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003422
Trond Myklebustc2190662013-08-26 19:23:04 -04003423 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003424 if (IS_ERR(auth))
3425 return -EACCES;
3426 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003427}
3428
Chuck Lever9a744ba2013-03-16 15:56:02 -04003429/*
3430 * Retry pseudoroot lookup with various security flavors. We do this when:
3431 *
3432 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3433 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3434 *
3435 * Returns zero on success, or a negative NFS4ERR value, or a
3436 * negative errno value.
3437 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003438static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003439 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003441 /* Per 3530bis 15.33.5 */
3442 static const rpc_authflavor_t flav_array[] = {
3443 RPC_AUTH_GSS_KRB5P,
3444 RPC_AUTH_GSS_KRB5I,
3445 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003446 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003447 RPC_AUTH_NULL,
3448 };
3449 int status = -EPERM;
3450 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003452 if (server->auth_info.flavor_len > 0) {
3453 /* try each flavor specified by user */
3454 for (i = 0; i < server->auth_info.flavor_len; i++) {
3455 status = nfs4_lookup_root_sec(server, fhandle, info,
3456 server->auth_info.flavors[i]);
3457 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3458 continue;
3459 break;
3460 }
3461 } else {
3462 /* no flavors specified by user, try default list */
3463 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3464 status = nfs4_lookup_root_sec(server, fhandle, info,
3465 flav_array[i]);
3466 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3467 continue;
3468 break;
3469 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003470 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003471
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003472 /*
3473 * -EACCESS could mean that the user doesn't have correct permissions
3474 * to access the mount. It could also mean that we tried to mount
3475 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3476 * existing mount programs don't handle -EACCES very well so it should
3477 * be mapped to -EPERM instead.
3478 */
3479 if (status == -EACCES)
3480 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003481 return status;
3482}
3483
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003484/**
3485 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3486 * @server: initialized nfs_server handle
3487 * @fhandle: we fill in the pseudo-fs root file handle
3488 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003489 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003490 *
3491 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003492 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003493int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003494 struct nfs_fsinfo *info,
3495 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003496{
Andre Przywarac7757072015-04-23 17:17:40 +01003497 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003498
Andre Przywarac7757072015-04-23 17:17:40 +01003499 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003500 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003501
3502 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003503 status = server->nfs_client->cl_mvops->find_root_sec(server,
3504 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003505
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 if (status == 0)
3507 status = nfs4_server_capabilities(server, fhandle);
3508 if (status == 0)
3509 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003510
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003511 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512}
3513
Bryan Schumakerbae36242012-05-10 15:07:31 -04003514static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3515 struct nfs_fsinfo *info)
3516{
3517 int error;
3518 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003519 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003520
3521 error = nfs4_server_capabilities(server, mntfh);
3522 if (error < 0) {
3523 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3524 return error;
3525 }
3526
David Quigley14c43f72013-05-22 12:50:43 -04003527 label = nfs4_label_alloc(server, GFP_KERNEL);
3528 if (IS_ERR(label))
3529 return PTR_ERR(label);
3530
David Quigley1775fd32013-05-22 12:50:42 -04003531 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003532 if (error < 0) {
3533 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003534 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003535 }
3536
3537 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3538 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3539 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3540
David Quigley14c43f72013-05-22 12:50:43 -04003541err_free_label:
3542 nfs4_label_free(label);
3543
Bryan Schumakerbae36242012-05-10 15:07:31 -04003544 return error;
3545}
3546
Manoj Naik6b97fd32006-06-09 09:34:29 -04003547/*
3548 * Get locations and (maybe) other attributes of a referral.
3549 * Note that we'll actually follow the referral later when
3550 * we detect fsid mismatch in inode revalidation
3551 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003552static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3553 const struct qstr *name, struct nfs_fattr *fattr,
3554 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003555{
3556 int status = -ENOMEM;
3557 struct page *page = NULL;
3558 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003559
3560 page = alloc_page(GFP_KERNEL);
3561 if (page == NULL)
3562 goto out;
3563 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3564 if (locations == NULL)
3565 goto out;
3566
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003567 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003568 if (status != 0)
3569 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003570
3571 /*
3572 * If the fsid didn't change, this is a migration event, not a
3573 * referral. Cause us to drop into the exception handler, which
3574 * will kick off migration recovery.
3575 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003576 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003577 dprintk("%s: server did not return a different fsid for"
3578 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003579 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003580 goto out;
3581 }
Andy Adamson533eb462011-06-13 18:25:56 -04003582 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3583 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003584
Andy Adamson533eb462011-06-13 18:25:56 -04003585 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003586 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003587 memset(fhandle, 0, sizeof(struct nfs_fh));
3588out:
3589 if (page)
3590 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003591 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003592 return status;
3593}
3594
David Quigley1775fd32013-05-22 12:50:42 -04003595static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3596 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597{
3598 struct nfs4_getattr_arg args = {
3599 .fh = fhandle,
3600 .bitmask = server->attr_bitmask,
3601 };
3602 struct nfs4_getattr_res res = {
3603 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003604 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 .server = server,
3606 };
3607 struct rpc_message msg = {
3608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3609 .rpc_argp = &args,
3610 .rpc_resp = &res,
3611 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003612
3613 args.bitmask = nfs4_bitmask(server, label);
3614
Trond Myklebust0e574af2005-10-27 22:12:38 -04003615 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003616 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617}
3618
David Quigley1775fd32013-05-22 12:50:42 -04003619static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3620 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621{
3622 struct nfs4_exception exception = { };
3623 int err;
3624 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003625 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3626 trace_nfs4_getattr(server, fhandle, fattr, err);
3627 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 &exception);
3629 } while (exception.retry);
3630 return err;
3631}
3632
3633/*
3634 * The file is not closed if it is opened due to the a request to change
3635 * the size of the file. The open call will not be needed once the
3636 * VFS layer lookup-intents are implemented.
3637 *
3638 * Close is called when the inode is destroyed.
3639 * If we haven't opened the file for O_WRONLY, we
3640 * need to in the size_change case to obtain a stateid.
3641 *
3642 * Got race?
3643 * Because OPEN is always done by name in nfsv4, it is
3644 * possible that we opened a different file by the same
3645 * name. We can recognize this race condition, but we
3646 * can't do anything about it besides returning an error.
3647 *
3648 * This will be fixed with VFS changes (lookup-intent).
3649 */
3650static int
3651nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3652 struct iattr *sattr)
3653{
David Howells2b0143b2015-03-17 22:25:59 +00003654 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003655 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003656 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003657 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 int status;
3659
Peng Tao88ac8152014-09-12 11:04:10 +08003660 if (pnfs_ld_layoutret_on_setattr(inode) &&
3661 sattr->ia_valid & ATTR_SIZE &&
3662 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003663 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003664
Trond Myklebust0e574af2005-10-27 22:12:38 -04003665 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
Andy Adamson26699402012-05-30 16:12:24 -04003667 /* Deal with open(O_TRUNC) */
3668 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003669 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003670
3671 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003672 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003673 return 0;
3674
Trond Myklebustd5308382005-11-04 15:33:38 -05003675 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003676 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003677
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003678 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003679 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003680 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003681 }
3682
David Quigley14c43f72013-05-22 12:50:43 -04003683 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3684 if (IS_ERR(label))
3685 return PTR_ERR(label);
3686
NeilBrown29b59f92016-10-13 15:26:47 +11003687 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003688 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003689 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003690 nfs_setsecurity(inode, fattr, label);
3691 }
David Quigley14c43f72013-05-22 12:50:43 -04003692 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 return status;
3694}
3695
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003696static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3697 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003698 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003699{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003700 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003701 int status;
3702 struct nfs4_lookup_arg args = {
3703 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003704 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003705 .name = name,
3706 };
3707 struct nfs4_lookup_res res = {
3708 .server = server,
3709 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003710 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003711 .fh = fhandle,
3712 };
3713 struct rpc_message msg = {
3714 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3715 .rpc_argp = &args,
3716 .rpc_resp = &res,
3717 };
3718
David Quigleyaa9c2662013-05-22 12:50:44 -04003719 args.bitmask = nfs4_bitmask(server, label);
3720
David Howells2b3de442006-08-22 20:06:09 -04003721 nfs_fattr_init(fattr);
3722
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003724 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 dprintk("NFS reply lookup: %d\n", status);
3726 return status;
3727}
3728
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003729static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003730{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003731 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003732 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003733 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3734 fattr->nlink = 2;
3735}
3736
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003737static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003738 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003739 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003740{
3741 struct nfs4_exception exception = { };
3742 struct rpc_clnt *client = *clnt;
3743 int err;
3744 do {
David Quigley1775fd32013-05-22 12:50:42 -04003745 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003746 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003747 switch (err) {
3748 case -NFS4ERR_BADNAME:
3749 err = -ENOENT;
3750 goto out;
3751 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003752 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003753 if (err == -NFS4ERR_MOVED)
3754 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003755 goto out;
3756 case -NFS4ERR_WRONGSEC:
3757 err = -EPERM;
3758 if (client != *clnt)
3759 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003760 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003761 if (IS_ERR(client))
3762 return PTR_ERR(client);
3763
3764 exception.retry = 1;
3765 break;
3766 default:
3767 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3768 }
3769 } while (exception.retry);
3770
3771out:
3772 if (err == 0)
3773 *clnt = client;
3774 else if (client != *clnt)
3775 rpc_shutdown_client(client);
3776
3777 return err;
3778}
3779
Al Virobeffb8f2016-07-20 16:34:42 -04003780static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003781 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3782 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003784 int status;
3785 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003786
David Quigley1775fd32013-05-22 12:50:42 -04003787 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003788 if (client != NFS_CLIENT(dir)) {
3789 rpc_shutdown_client(client);
3790 nfs_fixup_secinfo_attributes(fattr);
3791 }
3792 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793}
3794
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003795struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003796nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003797 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3798{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003799 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003800 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003801
David Quigley1775fd32013-05-22 12:50:42 -04003802 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003803 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003804 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003805 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003806}
3807
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3809{
Trond Myklebust76b32992007-08-10 17:45:11 -04003810 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 struct nfs4_accessargs args = {
3812 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003813 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003815 struct nfs4_accessres res = {
3816 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003817 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 struct rpc_message msg = {
3819 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3820 .rpc_argp = &args,
3821 .rpc_resp = &res,
3822 .rpc_cred = entry->cred,
3823 };
3824 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003825 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
3827 /*
3828 * Determine which access bits we want to ask for...
3829 */
3830 if (mode & MAY_READ)
3831 args.access |= NFS4_ACCESS_READ;
3832 if (S_ISDIR(inode->i_mode)) {
3833 if (mode & MAY_WRITE)
3834 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3835 if (mode & MAY_EXEC)
3836 args.access |= NFS4_ACCESS_LOOKUP;
3837 } else {
3838 if (mode & MAY_WRITE)
3839 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3840 if (mode & MAY_EXEC)
3841 args.access |= NFS4_ACCESS_EXECUTE;
3842 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003843
3844 res.fattr = nfs_alloc_fattr();
3845 if (res.fattr == NULL)
3846 return -ENOMEM;
3847
Bryan Schumaker7c513052011-03-24 17:12:24 +00003848 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003850 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003851 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003853 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 return status;
3855}
3856
3857static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3858{
3859 struct nfs4_exception exception = { };
3860 int err;
3861 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003862 err = _nfs4_proc_access(inode, entry);
3863 trace_nfs4_access(inode, err);
3864 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 &exception);
3866 } while (exception.retry);
3867 return err;
3868}
3869
3870/*
3871 * TODO: For the time being, we don't try to get any attributes
3872 * along with any of the zero-copy operations READ, READDIR,
3873 * READLINK, WRITE.
3874 *
3875 * In the case of the first three, we want to put the GETATTR
3876 * after the read-type operation -- this is because it is hard
3877 * to predict the length of a GETATTR response in v4, and thus
3878 * align the READ data correctly. This means that the GETATTR
3879 * may end up partially falling into the page cache, and we should
3880 * shift it into the 'tail' of the xdr_buf before processing.
3881 * To do this efficiently, we need to know the total length
3882 * of data received, which doesn't seem to be available outside
3883 * of the RPC layer.
3884 *
3885 * In the case of WRITE, we also want to put the GETATTR after
3886 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003887 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 *
3889 * Both of these changes to the XDR layer would in fact be quite
3890 * minor, but I decided to leave them for a subsequent patch.
3891 */
3892static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3893 unsigned int pgbase, unsigned int pglen)
3894{
3895 struct nfs4_readlink args = {
3896 .fh = NFS_FH(inode),
3897 .pgbase = pgbase,
3898 .pglen = pglen,
3899 .pages = &page,
3900 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003901 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 struct rpc_message msg = {
3903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3904 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003905 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 };
3907
Bryan Schumaker7c513052011-03-24 17:12:24 +00003908 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 -07003909}
3910
3911static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3912 unsigned int pgbase, unsigned int pglen)
3913{
3914 struct nfs4_exception exception = { };
3915 int err;
3916 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003917 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3918 trace_nfs4_readlink(inode, err);
3919 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 &exception);
3921 } while (exception.retry);
3922 return err;
3923}
3924
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003926 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928static int
3929nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003930 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003932 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04003933 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003934 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 int status = 0;
3937
NeilBrown532d4de2016-10-13 15:26:47 +11003938 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003939 if (IS_ERR(ctx))
3940 return PTR_ERR(ctx);
3941
David Quigleyaa9c2662013-05-22 12:50:44 -04003942 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3943
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003944 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
3945 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003946 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 if (IS_ERR(state)) {
3948 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003949 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003952 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003953 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 return status;
3955}
3956
Al Virobeffb8f2016-07-20 16:34:42 -04003957static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958{
Trond Myklebust16e42952005-10-27 22:12:44 -04003959 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003960 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003962 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003964 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003965 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003966 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003968 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3969 .rpc_argp = &args,
3970 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05003972 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04003973 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003974
Bryan Schumaker7c513052011-03-24 17:12:24 +00003975 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003976 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05003977 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 return status;
3979}
3980
Al Virobeffb8f2016-07-20 16:34:42 -04003981static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982{
3983 struct nfs4_exception exception = { };
3984 int err;
3985 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003986 err = _nfs4_proc_remove(dir, name);
3987 trace_nfs4_remove(dir, name, err);
3988 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 &exception);
3990 } while (exception.retry);
3991 return err;
3992}
3993
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003994static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003996 struct nfs_server *server = NFS_SERVER(dir);
3997 struct nfs_removeargs *args = msg->rpc_argp;
3998 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004000 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004002 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004003
4004 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005}
4006
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004007static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4008{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004009 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004010 &data->args.seq_args,
4011 &data->res.seq_res,
4012 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013}
4014
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004015static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004017 struct nfs_unlinkdata *data = task->tk_calldata;
4018 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004019
Trond Myklebust14516c32010-07-31 14:29:06 -04004020 if (!nfs4_sequence_done(task, &res->seq_res))
4021 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004022 if (nfs4_async_handle_error(task, res->server, NULL,
4023 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004024 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004025 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004026 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004027 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028}
4029
Jeff Laytond3d41522010-09-17 17:31:57 -04004030static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4031{
4032 struct nfs_server *server = NFS_SERVER(dir);
4033 struct nfs_renameargs *arg = msg->rpc_argp;
4034 struct nfs_renameres *res = msg->rpc_resp;
4035
4036 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004037 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004038 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004039}
4040
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004041static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4042{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004043 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004044 &data->args.seq_args,
4045 &data->res.seq_res,
4046 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004047}
4048
4049static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4050 struct inode *new_dir)
4051{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004052 struct nfs_renamedata *data = task->tk_calldata;
4053 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004054
4055 if (!nfs4_sequence_done(task, &res->seq_res))
4056 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004057 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004058 return 0;
4059
Trond Myklebustc733c492017-01-11 12:32:26 -05004060 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004061 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004062 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004063 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004064 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004065 return 1;
4066}
4067
Al Virobeffb8f2016-07-20 16:34:42 -04004068static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004070 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 struct nfs4_link_arg arg = {
4072 .fh = NFS_FH(inode),
4073 .dir_fh = NFS_FH(dir),
4074 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004075 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004077 struct nfs4_link_res res = {
4078 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004079 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004080 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 struct rpc_message msg = {
4082 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4083 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004084 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004086 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087
Trond Myklebust136f2622010-04-16 16:22:49 -04004088 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004089 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004090 goto out;
4091
David Quigley14c43f72013-05-22 12:50:43 -04004092 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4093 if (IS_ERR(res.label)) {
4094 status = PTR_ERR(res.label);
4095 goto out;
4096 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004097 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004098
Bryan Schumaker7c513052011-03-24 17:12:24 +00004099 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004100 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004101 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004102 status = nfs_post_op_update_inode(inode, res.fattr);
4103 if (!status)
4104 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004105 }
David Quigley14c43f72013-05-22 12:50:43 -04004106
4107
4108 nfs4_label_free(res.label);
4109
Trond Myklebust136f2622010-04-16 16:22:49 -04004110out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004111 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 return status;
4113}
4114
Al Virobeffb8f2016-07-20 16:34:42 -04004115static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116{
4117 struct nfs4_exception exception = { };
4118 int err;
4119 do {
4120 err = nfs4_handle_exception(NFS_SERVER(inode),
4121 _nfs4_proc_link(inode, dir, name),
4122 &exception);
4123 } while (exception.retry);
4124 return err;
4125}
4126
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004127struct nfs4_createdata {
4128 struct rpc_message msg;
4129 struct nfs4_create_arg arg;
4130 struct nfs4_create_res res;
4131 struct nfs_fh fh;
4132 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004133 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004134};
4135
4136static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004137 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004138{
4139 struct nfs4_createdata *data;
4140
4141 data = kzalloc(sizeof(*data), GFP_KERNEL);
4142 if (data != NULL) {
4143 struct nfs_server *server = NFS_SERVER(dir);
4144
David Quigley14c43f72013-05-22 12:50:43 -04004145 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4146 if (IS_ERR(data->label))
4147 goto out_free;
4148
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004149 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4150 data->msg.rpc_argp = &data->arg;
4151 data->msg.rpc_resp = &data->res;
4152 data->arg.dir_fh = NFS_FH(dir);
4153 data->arg.server = server;
4154 data->arg.name = name;
4155 data->arg.attrs = sattr;
4156 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004157 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004158 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004159 data->res.server = server;
4160 data->res.fh = &data->fh;
4161 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004162 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004163 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004164 }
4165 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004166out_free:
4167 kfree(data);
4168 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004169}
4170
4171static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4172{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004173 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004174 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004175 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004176 update_changeattr(dir, &data->res.dir_cinfo,
4177 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004178 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004179 }
4180 return status;
4181}
4182
4183static void nfs4_free_createdata(struct nfs4_createdata *data)
4184{
David Quigley14c43f72013-05-22 12:50:43 -04004185 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004186 kfree(data);
4187}
4188
Chuck Lever4f390c12006-08-22 20:06:22 -04004189static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004190 struct page *page, unsigned int len, struct iattr *sattr,
4191 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004193 struct nfs4_createdata *data;
4194 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
Chuck Lever94a6d752006-08-22 20:06:23 -04004196 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004197 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004198
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004199 status = -ENOMEM;
4200 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4201 if (data == NULL)
4202 goto out;
4203
4204 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4205 data->arg.u.symlink.pages = &page;
4206 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004207 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004209 status = nfs4_do_create(dir, dentry, data);
4210
4211 nfs4_free_createdata(data);
4212out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 return status;
4214}
4215
Chuck Lever4f390c12006-08-22 20:06:22 -04004216static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004217 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218{
4219 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004220 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004222
4223 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4224
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004226 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4227 trace_nfs4_symlink(dir, &dentry->d_name, err);
4228 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 &exception);
4230 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004231
4232 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 return err;
4234}
4235
4236static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004237 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004239 struct nfs4_createdata *data;
4240 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004242 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4243 if (data == NULL)
4244 goto out;
4245
David Quigley1775fd32013-05-22 12:50:42 -04004246 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004247 status = nfs4_do_create(dir, dentry, data);
4248
4249 nfs4_free_createdata(data);
4250out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 return status;
4252}
4253
4254static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4255 struct iattr *sattr)
4256{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004257 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004259 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004261
David Quigleyaa9c2662013-05-22 12:50:44 -04004262 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4263
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004264 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4265 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004267 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4268 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4269 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 &exception);
4271 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004272 nfs4_label_release_security(label);
4273
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 return err;
4275}
4276
4277static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004278 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279{
David Howells2b0143b2015-03-17 22:25:59 +00004280 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 struct nfs4_readdir_arg args = {
4282 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004283 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 .pgbase = 0,
4285 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004286 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004287 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 };
4289 struct nfs4_readdir_res res;
4290 struct rpc_message msg = {
4291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4292 .rpc_argp = &args,
4293 .rpc_resp = &res,
4294 .rpc_cred = cred,
4295 };
4296 int status;
4297
Al Viro6de14722013-09-16 10:53:17 -04004298 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4299 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004300 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004301 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004303 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 -05004304 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004305 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004306 status += args.pgbase;
4307 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004308
4309 nfs_invalidate_atime(dir);
4310
Harvey Harrison3110ff82008-05-02 13:42:44 -07004311 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 return status;
4313}
4314
4315static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004316 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317{
4318 struct nfs4_exception exception = { };
4319 int err;
4320 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004321 err = _nfs4_proc_readdir(dentry, cred, cookie,
4322 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004323 trace_nfs4_readdir(d_inode(dentry), err);
4324 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 &exception);
4326 } while (exception.retry);
4327 return err;
4328}
4329
4330static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004331 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004333 struct nfs4_createdata *data;
4334 int mode = sattr->ia_mode;
4335 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004337 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4338 if (data == NULL)
4339 goto out;
4340
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004342 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004344 data->arg.ftype = NF4BLK;
4345 data->arg.u.device.specdata1 = MAJOR(rdev);
4346 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 }
4348 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004349 data->arg.ftype = NF4CHR;
4350 data->arg.u.device.specdata1 = MAJOR(rdev);
4351 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004352 } else if (!S_ISSOCK(mode)) {
4353 status = -EINVAL;
4354 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 }
David Quigley1775fd32013-05-22 12:50:42 -04004356
David Quigleyaa9c2662013-05-22 12:50:44 -04004357 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004358 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004359out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004360 nfs4_free_createdata(data);
4361out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 return status;
4363}
4364
4365static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4366 struct iattr *sattr, dev_t rdev)
4367{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004368 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004370 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004372
David Quigleyaa9c2662013-05-22 12:50:44 -04004373 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4374
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004375 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4376 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004378 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4379 trace_nfs4_mknod(dir, &dentry->d_name, err);
4380 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 &exception);
4382 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004383
4384 nfs4_label_release_security(label);
4385
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 return err;
4387}
4388
4389static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4390 struct nfs_fsstat *fsstat)
4391{
4392 struct nfs4_statfs_arg args = {
4393 .fh = fhandle,
4394 .bitmask = server->attr_bitmask,
4395 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004396 struct nfs4_statfs_res res = {
4397 .fsstat = fsstat,
4398 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 struct rpc_message msg = {
4400 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4401 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004402 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 };
4404
Trond Myklebust0e574af2005-10-27 22:12:38 -04004405 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004406 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407}
4408
4409static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4410{
4411 struct nfs4_exception exception = { };
4412 int err;
4413 do {
4414 err = nfs4_handle_exception(server,
4415 _nfs4_proc_statfs(server, fhandle, fsstat),
4416 &exception);
4417 } while (exception.retry);
4418 return err;
4419}
4420
4421static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4422 struct nfs_fsinfo *fsinfo)
4423{
4424 struct nfs4_fsinfo_arg args = {
4425 .fh = fhandle,
4426 .bitmask = server->attr_bitmask,
4427 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004428 struct nfs4_fsinfo_res res = {
4429 .fsinfo = fsinfo,
4430 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 struct rpc_message msg = {
4432 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4433 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004434 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 };
4436
Bryan Schumaker7c513052011-03-24 17:12:24 +00004437 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438}
4439
4440static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4441{
4442 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004443 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 int err;
4445
4446 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004447 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004448 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004449 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004450 nfs4_set_lease_period(server->nfs_client,
4451 fsinfo->lease_time * HZ,
4452 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004453 break;
4454 }
4455 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 } while (exception.retry);
4457 return err;
4458}
4459
4460static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4461{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004462 int error;
4463
Trond Myklebust0e574af2005-10-27 22:12:38 -04004464 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004465 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004466 if (error == 0) {
4467 /* block layout checks this! */
4468 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004469 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004470 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004471
4472 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473}
4474
4475static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4476 struct nfs_pathconf *pathconf)
4477{
4478 struct nfs4_pathconf_arg args = {
4479 .fh = fhandle,
4480 .bitmask = server->attr_bitmask,
4481 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004482 struct nfs4_pathconf_res res = {
4483 .pathconf = pathconf,
4484 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 struct rpc_message msg = {
4486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4487 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004488 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 };
4490
4491 /* None of the pathconf attributes are mandatory to implement */
4492 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4493 memset(pathconf, 0, sizeof(*pathconf));
4494 return 0;
4495 }
4496
Trond Myklebust0e574af2005-10-27 22:12:38 -04004497 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004498 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499}
4500
4501static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4502 struct nfs_pathconf *pathconf)
4503{
4504 struct nfs4_exception exception = { };
4505 int err;
4506
4507 do {
4508 err = nfs4_handle_exception(server,
4509 _nfs4_proc_pathconf(server, fhandle, pathconf),
4510 &exception);
4511 } while (exception.retry);
4512 return err;
4513}
4514
Trond Myklebust5521abf2013-03-16 20:54:34 -04004515int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004516 const struct nfs_open_context *ctx,
4517 const struct nfs_lock_context *l_ctx,
4518 fmode_t fmode)
4519{
NeilBrown17393472016-10-13 15:26:47 +11004520 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004521}
4522EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4523
Trond Myklebust5521abf2013-03-16 20:54:34 -04004524static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4525 const struct nfs_open_context *ctx,
4526 const struct nfs_lock_context *l_ctx,
4527 fmode_t fmode)
4528{
4529 nfs4_stateid current_stateid;
4530
Trond Myklebuste1253be2014-03-05 08:44:23 -05004531 /* If the current stateid represents a lost lock, then exit */
4532 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4533 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004534 return nfs4_stateid_match(stateid, &current_stateid);
4535}
4536
4537static bool nfs4_error_stateid_expired(int err)
4538{
4539 switch (err) {
4540 case -NFS4ERR_DELEG_REVOKED:
4541 case -NFS4ERR_ADMIN_REVOKED:
4542 case -NFS4ERR_BAD_STATEID:
4543 case -NFS4ERR_STALE_STATEID:
4544 case -NFS4ERR_OLD_STATEID:
4545 case -NFS4ERR_OPENMODE:
4546 case -NFS4ERR_EXPIRED:
4547 return true;
4548 }
4549 return false;
4550}
4551
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004552static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004554 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004556 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004557 if (task->tk_status < 0) {
4558 struct nfs4_exception exception = {
4559 .inode = hdr->inode,
4560 .state = hdr->args.context->state,
4561 .stateid = &hdr->args.stateid,
4562 };
4563 task->tk_status = nfs4_async_handle_exception(task,
4564 server, task->tk_status, &exception);
4565 if (exception.retry) {
4566 rpc_restart_call_prepare(task);
4567 return -EAGAIN;
4568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004570
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004572 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004573 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574}
4575
Trond Myklebust5521abf2013-03-16 20:54:34 -04004576static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004577 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004578{
4579
4580 if (!nfs4_error_stateid_expired(task->tk_status) ||
4581 nfs4_stateid_is_current(&args->stateid,
4582 args->context,
4583 args->lock_context,
4584 FMODE_READ))
4585 return false;
4586 rpc_restart_call_prepare(task);
4587 return true;
4588}
4589
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004590static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004591{
4592
4593 dprintk("--> %s\n", __func__);
4594
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004595 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004596 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004597 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004598 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004599 if (task->tk_status > 0)
4600 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004601 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4602 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004603}
4604
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004605static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4606 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004608 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004609 if (!hdr->pgio_done_cb)
4610 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004611 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004612 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613}
4614
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004615static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4616 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004617{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004618 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004619 &hdr->args.seq_args,
4620 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004621 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004622 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004623 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4624 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004625 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004626 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004627 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004628 return -EIO;
4629 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630}
4631
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004632static int nfs4_write_done_cb(struct rpc_task *task,
4633 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004635 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004636
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004637 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004638 if (task->tk_status < 0) {
4639 struct nfs4_exception exception = {
4640 .inode = hdr->inode,
4641 .state = hdr->args.context->state,
4642 .stateid = &hdr->args.stateid,
4643 };
4644 task->tk_status = nfs4_async_handle_exception(task,
4645 NFS_SERVER(inode), task->tk_status,
4646 &exception);
4647 if (exception.retry) {
4648 rpc_restart_call_prepare(task);
4649 return -EAGAIN;
4650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004652 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004653 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004654 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004655 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004656 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657}
4658
Trond Myklebust5521abf2013-03-16 20:54:34 -04004659static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004660 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004661{
4662
4663 if (!nfs4_error_stateid_expired(task->tk_status) ||
4664 nfs4_stateid_is_current(&args->stateid,
4665 args->context,
4666 args->lock_context,
4667 FMODE_WRITE))
4668 return false;
4669 rpc_restart_call_prepare(task);
4670 return true;
4671}
4672
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004673static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004674{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004675 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004676 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004677 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004678 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004679 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4680 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004681}
4682
Trond Myklebust5a37f852012-04-28 14:55:16 -04004683static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004684bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004685{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004686 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004687 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004688 return false;
4689 /* Otherwise, request attributes if and only if we don't hold
4690 * a delegation
4691 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004692 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004693}
Fred Isamana69aef12011-03-03 15:13:47 +00004694
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004695static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4696 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004698 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004699
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004700 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4701 hdr->args.bitmask = NULL;
4702 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004703 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004704 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004705
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004706 if (!hdr->pgio_done_cb)
4707 hdr->pgio_done_cb = nfs4_write_done_cb;
4708 hdr->res.server = server;
4709 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004711 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004712 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713}
4714
Fred Isaman0b7c0152012-04-20 14:47:39 -04004715static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4716{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004717 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004718 &data->args.seq_args,
4719 &data->res.seq_res,
4720 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721}
4722
Fred Isaman0b7c0152012-04-20 14:47:39 -04004723static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004726
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004727 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004728 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4729 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004730 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004731 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004733 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734}
4735
Fred Isaman0b7c0152012-04-20 14:47:39 -04004736static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004737{
4738 if (!nfs4_sequence_done(task, &data->res.seq_res))
4739 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004740 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004741}
4742
Fred Isaman0b7c0152012-04-20 14:47:39 -04004743static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004745 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004746
Fred Isaman0b7c0152012-04-20 14:47:39 -04004747 if (data->commit_done_cb == NULL)
4748 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004749 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004750 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004751 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752}
4753
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004754struct nfs4_renewdata {
4755 struct nfs_client *client;
4756 unsigned long timestamp;
4757};
4758
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759/*
4760 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4761 * standalone procedure for queueing an asynchronous RENEW.
4762 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004763static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004764{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004765 struct nfs4_renewdata *data = calldata;
4766 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004767
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004768 if (atomic_read(&clp->cl_count) > 1)
4769 nfs4_schedule_state_renewal(clp);
4770 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004771 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004772}
4773
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004774static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004776 struct nfs4_renewdata *data = calldata;
4777 struct nfs_client *clp = data->client;
4778 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004780 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004781 switch (task->tk_status) {
4782 case 0:
4783 break;
4784 case -NFS4ERR_LEASE_MOVED:
4785 nfs4_schedule_lease_moved_recovery(clp);
4786 break;
4787 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004788 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004789 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4790 return;
4791 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004792 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004793 return;
4794 }
4795 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004797 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798}
4799
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004800static const struct rpc_call_ops nfs4_renew_ops = {
4801 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004802 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004803};
4804
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004805static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806{
4807 struct rpc_message msg = {
4808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4809 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004810 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004812 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004814 if (renew_flags == 0)
4815 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004816 if (!atomic_inc_not_zero(&clp->cl_count))
4817 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004818 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04004819 if (data == NULL) {
4820 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004821 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04004822 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004823 data->client = clp;
4824 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004825 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004826 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827}
4828
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004829static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830{
4831 struct rpc_message msg = {
4832 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4833 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004834 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 };
4836 unsigned long now = jiffies;
4837 int status;
4838
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004839 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 if (status < 0)
4841 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004842 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 return 0;
4844}
4845
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004846static inline int nfs4_server_supports_acls(struct nfs_server *server)
4847{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004848 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004849}
4850
Trond Myklebust21f498c2012-08-24 10:59:25 -04004851/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4852 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004853 * the stack.
4854 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004855#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004856
Neil Hormane9e3d722011-03-04 19:26:03 -05004857static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004858 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004859{
4860 struct page *newpage, **spages;
4861 int rc = 0;
4862 size_t len;
4863 spages = pages;
4864
4865 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004866 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004867 newpage = alloc_page(GFP_KERNEL);
4868
4869 if (newpage == NULL)
4870 goto unwind;
4871 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05004872 buf += len;
4873 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05004874 *pages++ = newpage;
4875 rc++;
4876 } while (buflen != 0);
4877
4878 return rc;
4879
4880unwind:
4881 for(; rc > 0; rc--)
4882 __free_page(spages[rc-1]);
4883 return -ENOMEM;
4884}
4885
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004886struct nfs4_cached_acl {
4887 int cached;
4888 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004889 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004890};
4891
4892static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004893{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004894 struct nfs_inode *nfsi = NFS_I(inode);
4895
4896 spin_lock(&inode->i_lock);
4897 kfree(nfsi->nfs4_acl);
4898 nfsi->nfs4_acl = acl;
4899 spin_unlock(&inode->i_lock);
4900}
4901
4902static void nfs4_zap_acl_attr(struct inode *inode)
4903{
4904 nfs4_set_cached_acl(inode, NULL);
4905}
4906
4907static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4908{
4909 struct nfs_inode *nfsi = NFS_I(inode);
4910 struct nfs4_cached_acl *acl;
4911 int ret = -ENOENT;
4912
4913 spin_lock(&inode->i_lock);
4914 acl = nfsi->nfs4_acl;
4915 if (acl == NULL)
4916 goto out;
4917 if (buf == NULL) /* user is just asking for length */
4918 goto out_len;
4919 if (acl->cached == 0)
4920 goto out;
4921 ret = -ERANGE; /* see getxattr(2) man page */
4922 if (acl->len > buflen)
4923 goto out;
4924 memcpy(buf, acl->data, acl->len);
4925out_len:
4926 ret = acl->len;
4927out:
4928 spin_unlock(&inode->i_lock);
4929 return ret;
4930}
4931
Sachin Prabhu5794d212012-04-17 14:36:40 +01004932static 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 +00004933{
4934 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004935 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004936
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004937 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004938 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004939 if (acl == NULL)
4940 goto out;
4941 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004942 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004943 } else {
4944 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4945 if (acl == NULL)
4946 goto out;
4947 acl->cached = 0;
4948 }
4949 acl->len = acl_len;
4950out:
4951 nfs4_set_cached_acl(inode, acl);
4952}
4953
Andy Adamsonbf118a32011-12-07 11:55:27 -05004954/*
4955 * The getxattr API returns the required buffer length when called with a
4956 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4957 * the required buf. On a NULL buf, we send a page of data to the server
4958 * guessing that the ACL request can be serviced by a page. If so, we cache
4959 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4960 * the cache. If not so, we throw away the page, and cache the required
4961 * length. The next getxattr call will then produce another round trip to
4962 * the server, this time with the input buf of the required size.
4963 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004964static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004965{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05004966 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004967 struct nfs_getaclargs args = {
4968 .fh = NFS_FH(inode),
4969 .acl_pages = pages,
4970 .acl_len = buflen,
4971 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004972 struct nfs_getaclres res = {
4973 .acl_len = buflen,
4974 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004975 struct rpc_message msg = {
4976 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4977 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004978 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004979 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05004980 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004981 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004982
Trond Myklebust21f498c2012-08-24 10:59:25 -04004983 if (npages > ARRAY_SIZE(pages))
4984 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004985
Andy Adamsonbf118a32011-12-07 11:55:27 -05004986 for (i = 0; i < npages; i++) {
4987 pages[i] = alloc_page(GFP_KERNEL);
4988 if (!pages[i])
4989 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004990 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004991
4992 /* for decoding across pages */
4993 res.acl_scratch = alloc_page(GFP_KERNEL);
4994 if (!res.acl_scratch)
4995 goto out_free;
4996
Andy Adamsonbf118a32011-12-07 11:55:27 -05004997 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004998
Peng Taode040be2012-01-10 22:42:47 +08004999 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005000 __func__, buf, buflen, npages, args.acl_len);
5001 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5002 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005003 if (ret)
5004 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005005
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005006 /* Handle the case where the passed-in buffer is too short */
5007 if (res.acl_flags & NFS4_ACL_TRUNC) {
5008 /* Did the user only issue a request for the acl length? */
5009 if (buf == NULL)
5010 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005011 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005012 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005013 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005014 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005015 if (buf) {
5016 if (res.acl_len > buflen) {
5017 ret = -ERANGE;
5018 goto out_free;
5019 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005020 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005021 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005022out_ok:
5023 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005024out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005025 for (i = 0; i < npages; i++)
5026 if (pages[i])
5027 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005028 if (res.acl_scratch)
5029 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005030 return ret;
5031}
5032
Trond Myklebust16b42892006-08-24 12:27:15 -04005033static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5034{
5035 struct nfs4_exception exception = { };
5036 ssize_t ret;
5037 do {
5038 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005039 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005040 if (ret >= 0)
5041 break;
5042 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5043 } while (exception.retry);
5044 return ret;
5045}
5046
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005047static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5048{
5049 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005050 int ret;
5051
5052 if (!nfs4_server_supports_acls(server))
5053 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005054 ret = nfs_revalidate_inode(server, inode);
5055 if (ret < 0)
5056 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005057 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5058 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005059 ret = nfs4_read_cached_acl(inode, buf, buflen);
5060 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005061 /* -ENOENT is returned if there is no ACL or if there is an ACL
5062 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005063 return ret;
5064 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005065}
5066
Trond Myklebust16b42892006-08-24 12:27:15 -04005067static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005068{
5069 struct nfs_server *server = NFS_SERVER(inode);
5070 struct page *pages[NFS4ACL_MAXPAGES];
5071 struct nfs_setaclargs arg = {
5072 .fh = NFS_FH(inode),
5073 .acl_pages = pages,
5074 .acl_len = buflen,
5075 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005076 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005077 struct rpc_message msg = {
5078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5079 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005080 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005081 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005082 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005083 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005084
5085 if (!nfs4_server_supports_acls(server))
5086 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005087 if (npages > ARRAY_SIZE(pages))
5088 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005089 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005090 if (i < 0)
5091 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005092 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005093 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005094
5095 /*
5096 * Free each page after tx, so the only ref left is
5097 * held by the network stack
5098 */
5099 for (; i > 0; i--)
5100 put_page(pages[i-1]);
5101
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005102 /*
5103 * Acl update can result in inode attribute update.
5104 * so mark the attribute cache invalid.
5105 */
5106 spin_lock(&inode->i_lock);
5107 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5108 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005109 nfs_access_zap_cache(inode);
5110 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005111 return ret;
5112}
5113
Trond Myklebust16b42892006-08-24 12:27:15 -04005114static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5115{
5116 struct nfs4_exception exception = { };
5117 int err;
5118 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005119 err = __nfs4_proc_set_acl(inode, buf, buflen);
5120 trace_nfs4_set_acl(inode, err);
5121 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005122 &exception);
5123 } while (exception.retry);
5124 return err;
5125}
5126
David Quigleyaa9c2662013-05-22 12:50:44 -04005127#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5128static int _nfs4_get_security_label(struct inode *inode, void *buf,
5129 size_t buflen)
5130{
5131 struct nfs_server *server = NFS_SERVER(inode);
5132 struct nfs_fattr fattr;
5133 struct nfs4_label label = {0, 0, buflen, buf};
5134
5135 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005136 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005137 .fh = NFS_FH(inode),
5138 .bitmask = bitmask,
5139 };
5140 struct nfs4_getattr_res res = {
5141 .fattr = &fattr,
5142 .label = &label,
5143 .server = server,
5144 };
5145 struct rpc_message msg = {
5146 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005147 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005148 .rpc_resp = &res,
5149 };
5150 int ret;
5151
5152 nfs_fattr_init(&fattr);
5153
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005154 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005155 if (ret)
5156 return ret;
5157 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5158 return -ENOENT;
5159 if (buflen < label.len)
5160 return -ERANGE;
5161 return 0;
5162}
5163
5164static int nfs4_get_security_label(struct inode *inode, void *buf,
5165 size_t buflen)
5166{
5167 struct nfs4_exception exception = { };
5168 int err;
5169
5170 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5171 return -EOPNOTSUPP;
5172
5173 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005174 err = _nfs4_get_security_label(inode, buf, buflen);
5175 trace_nfs4_get_security_label(inode, err);
5176 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005177 &exception);
5178 } while (exception.retry);
5179 return err;
5180}
5181
5182static int _nfs4_do_set_security_label(struct inode *inode,
5183 struct nfs4_label *ilabel,
5184 struct nfs_fattr *fattr,
5185 struct nfs4_label *olabel)
5186{
5187
5188 struct iattr sattr = {0};
5189 struct nfs_server *server = NFS_SERVER(inode);
5190 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005191 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005192 .fh = NFS_FH(inode),
5193 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005194 .server = server,
5195 .bitmask = bitmask,
5196 .label = ilabel,
5197 };
5198 struct nfs_setattrres res = {
5199 .fattr = fattr,
5200 .label = olabel,
5201 .server = server,
5202 };
5203 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005204 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5205 .rpc_argp = &arg,
5206 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005207 };
5208 int status;
5209
Jeff Layton12207f62013-11-01 10:49:32 -04005210 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005211
Jeff Layton12207f62013-11-01 10:49:32 -04005212 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005213 if (status)
5214 dprintk("%s failed: %d\n", __func__, status);
5215
5216 return status;
5217}
5218
5219static int nfs4_do_set_security_label(struct inode *inode,
5220 struct nfs4_label *ilabel,
5221 struct nfs_fattr *fattr,
5222 struct nfs4_label *olabel)
5223{
5224 struct nfs4_exception exception = { };
5225 int err;
5226
5227 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005228 err = _nfs4_do_set_security_label(inode, ilabel,
5229 fattr, olabel);
5230 trace_nfs4_set_security_label(inode, err);
5231 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005232 &exception);
5233 } while (exception.retry);
5234 return err;
5235}
5236
5237static int
Al Viro59301222016-05-27 10:19:30 -04005238nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005239{
5240 struct nfs4_label ilabel, *olabel = NULL;
5241 struct nfs_fattr fattr;
5242 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005243 int status;
5244
5245 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5246 return -EOPNOTSUPP;
5247
5248 nfs_fattr_init(&fattr);
5249
5250 ilabel.pi = 0;
5251 ilabel.lfs = 0;
5252 ilabel.label = (char *)buf;
5253 ilabel.len = buflen;
5254
5255 cred = rpc_lookup_cred();
5256 if (IS_ERR(cred))
5257 return PTR_ERR(cred);
5258
5259 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5260 if (IS_ERR(olabel)) {
5261 status = -PTR_ERR(olabel);
5262 goto out;
5263 }
5264
5265 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5266 if (status == 0)
5267 nfs_setsecurity(inode, &fattr, olabel);
5268
5269 nfs4_label_free(olabel);
5270out:
5271 put_rpccred(cred);
5272 return status;
5273}
5274#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5275
5276
Chuck Leverf0920752012-05-21 22:45:41 -04005277static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5278 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005279{
5280 __be32 verf[2];
5281
Chuck Lever2c820d92012-05-21 22:45:33 -04005282 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5283 /* An impossible timestamp guarantees this value
5284 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005285 verf[0] = cpu_to_be32(U32_MAX);
5286 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005287 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005288 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005289 u64 ns = ktime_to_ns(nn->boot_time);
5290
5291 verf[0] = cpu_to_be32(ns >> 32);
5292 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005293 }
Chuck Levercd937102012-03-02 17:14:31 -05005294 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5295}
5296
Jeff Laytona3192682015-06-09 19:43:59 -04005297static int
5298nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005299{
Jeff Laytona3192682015-06-09 19:43:59 -04005300 size_t len;
5301 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005302
Trond Myklebustceb3a162015-01-03 15:16:04 -05005303 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005304 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005305
Jeff Laytona3192682015-06-09 19:43:59 -04005306 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005307 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005308 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5309 1 +
5310 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5311 1;
5312 rcu_read_unlock();
5313
5314 if (len > NFS4_OPAQUE_LIMIT + 1)
5315 return -EINVAL;
5316
5317 /*
5318 * Since this string is allocated at mount time, and held until the
5319 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5320 * about a memory-reclaim deadlock.
5321 */
5322 str = kmalloc(len, GFP_KERNEL);
5323 if (!str)
5324 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005325
Chuck Levere984a552012-09-14 17:24:21 -04005326 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005327 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005328 clp->cl_ipaddr,
5329 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5330 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005331 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005332
Jeff Laytona3192682015-06-09 19:43:59 -04005333 clp->cl_owner_id = str;
5334 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005335}
5336
Jeff Layton873e3852015-06-09 19:44:00 -04005337static int
5338nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005339{
Jeff Layton873e3852015-06-09 19:44:00 -04005340 size_t len;
5341 char *str;
5342
5343 len = 10 + 10 + 1 + 10 + 1 +
5344 strlen(nfs4_client_id_uniquifier) + 1 +
5345 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5346
5347 if (len > NFS4_OPAQUE_LIMIT + 1)
5348 return -EINVAL;
5349
5350 /*
5351 * Since this string is allocated at mount time, and held until the
5352 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5353 * about a memory-reclaim deadlock.
5354 */
5355 str = kmalloc(len, GFP_KERNEL);
5356 if (!str)
5357 return -ENOMEM;
5358
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005359 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005360 clp->rpc_ops->version, clp->cl_minorversion,
5361 nfs4_client_id_uniquifier,
5362 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005363 clp->cl_owner_id = str;
5364 return 0;
5365}
5366
5367static int
5368nfs4_init_uniform_client_string(struct nfs_client *clp)
5369{
Jeff Layton873e3852015-06-09 19:44:00 -04005370 size_t len;
5371 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005372
5373 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005374 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005375
5376 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005377 return nfs4_init_uniquifier_client_string(clp);
5378
5379 len = 10 + 10 + 1 + 10 + 1 +
5380 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5381
5382 if (len > NFS4_OPAQUE_LIMIT + 1)
5383 return -EINVAL;
5384
5385 /*
5386 * Since this string is allocated at mount time, and held until the
5387 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5388 * about a memory-reclaim deadlock.
5389 */
5390 str = kmalloc(len, GFP_KERNEL);
5391 if (!str)
5392 return -ENOMEM;
5393
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005394 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005395 clp->rpc_ops->version, clp->cl_minorversion,
5396 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005397 clp->cl_owner_id = str;
5398 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005399}
5400
Chuck Lever706cb8d2014-03-12 12:51:47 -04005401/*
5402 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5403 * services. Advertise one based on the address family of the
5404 * clientaddr.
5405 */
5406static unsigned int
5407nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5408{
5409 if (strchr(clp->cl_ipaddr, ':') != NULL)
5410 return scnprintf(buf, len, "tcp6");
5411 else
5412 return scnprintf(buf, len, "tcp");
5413}
5414
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005415static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5416{
5417 struct nfs4_setclientid *sc = calldata;
5418
5419 if (task->tk_status == 0)
5420 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5421}
5422
5423static const struct rpc_call_ops nfs4_setclientid_ops = {
5424 .rpc_call_done = nfs4_setclientid_done,
5425};
5426
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005427/**
5428 * nfs4_proc_setclientid - Negotiate client ID
5429 * @clp: state data structure
5430 * @program: RPC program for NFSv4 callback service
5431 * @port: IP port number for NFS4 callback service
5432 * @cred: RPC credential to use for this call
5433 * @res: where to place the result
5434 *
5435 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5436 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005437int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5438 unsigned short port, struct rpc_cred *cred,
5439 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440{
5441 nfs4_verifier sc_verifier;
5442 struct nfs4_setclientid setclientid = {
5443 .sc_verifier = &sc_verifier,
5444 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005445 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 };
5447 struct rpc_message msg = {
5448 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5449 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005450 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005451 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005453 struct rpc_task *task;
5454 struct rpc_task_setup task_setup_data = {
5455 .rpc_client = clp->cl_rpcclient,
5456 .rpc_message = &msg,
5457 .callback_ops = &nfs4_setclientid_ops,
5458 .callback_data = &setclientid,
5459 .flags = RPC_TASK_TIMEOUT,
5460 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005461 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
Chuck Leverde734832012-07-11 16:30:50 -04005463 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005464 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005465
5466 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5467 status = nfs4_init_uniform_client_string(clp);
5468 else
Jeff Laytona3192682015-06-09 19:43:59 -04005469 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005470
5471 if (status)
5472 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005473
Chuck Leverde734832012-07-11 16:30:50 -04005474 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005475 setclientid.sc_netid_len =
5476 nfs4_init_callback_netid(clp,
5477 setclientid.sc_netid,
5478 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005479 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005480 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 clp->cl_ipaddr, port >> 8, port & 255);
5482
Jeff Layton3a6bb732015-06-09 19:43:57 -04005483 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005484 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005485 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005486 task = rpc_run_task(&task_setup_data);
5487 if (IS_ERR(task)) {
5488 status = PTR_ERR(task);
5489 goto out;
5490 }
5491 status = task->tk_status;
5492 if (setclientid.sc_cred) {
5493 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5494 put_rpccred(setclientid.sc_cred);
5495 }
5496 rpc_put_task(task);
5497out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005498 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005499 dprintk("NFS reply setclientid: %d\n", status);
5500 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501}
5502
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005503/**
5504 * nfs4_proc_setclientid_confirm - Confirm client ID
5505 * @clp: state data structure
5506 * @res: result of a previous SETCLIENTID
5507 * @cred: RPC credential to use for this call
5508 *
5509 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5510 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005511int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005512 struct nfs4_setclientid_res *arg,
5513 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 struct rpc_message msg = {
5516 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005517 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005518 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 int status;
5521
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005522 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5523 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5524 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005525 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005526 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005527 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 return status;
5529}
5530
Trond Myklebustfe650402006-01-03 09:55:18 +01005531struct nfs4_delegreturndata {
5532 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005533 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005534 struct nfs_fh fh;
5535 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005536 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005537 struct {
5538 struct nfs4_layoutreturn_args arg;
5539 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005540 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005541 u32 roc_barrier;
5542 bool roc;
5543 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005544 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005545 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005546 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005547};
5548
Trond Myklebustfe650402006-01-03 09:55:18 +01005549static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5550{
5551 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005552
Trond Myklebust14516c32010-07-31 14:29:06 -04005553 if (!nfs4_sequence_done(task, &data->res.seq_res))
5554 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005555
Trond Myklebustca8acf82013-08-13 10:36:56 -04005556 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005557
5558 /* Handle Layoutreturn errors */
5559 if (data->args.lr_args && task->tk_status != 0) {
5560 switch(data->res.lr_ret) {
5561 default:
5562 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5563 break;
5564 case 0:
5565 data->args.lr_args = NULL;
5566 data->res.lr_res = NULL;
5567 break;
5568 case -NFS4ERR_ADMIN_REVOKED:
5569 case -NFS4ERR_DELEG_REVOKED:
5570 case -NFS4ERR_EXPIRED:
5571 case -NFS4ERR_BAD_STATEID:
5572 case -NFS4ERR_OLD_STATEID:
5573 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5574 case -NFS4ERR_WRONG_CRED:
5575 data->args.lr_args = NULL;
5576 data->res.lr_res = NULL;
5577 data->res.lr_ret = 0;
5578 rpc_restart_call_prepare(task);
5579 return;
5580 }
5581 }
5582
Ricardo Labiaga79708862009-12-07 09:23:21 -05005583 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005584 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005585 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005586 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005587 case -NFS4ERR_ADMIN_REVOKED:
5588 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005589 case -NFS4ERR_EXPIRED:
5590 nfs4_free_revoked_stateid(data->res.server,
5591 data->args.stateid,
5592 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005593 case -NFS4ERR_BAD_STATEID:
5594 case -NFS4ERR_OLD_STATEID:
5595 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005596 task->tk_status = 0;
5597 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005598 case -NFS4ERR_ACCESS:
5599 if (data->args.bitmask) {
5600 data->args.bitmask = NULL;
5601 data->res.fattr = NULL;
5602 task->tk_status = 0;
5603 rpc_restart_call_prepare(task);
5604 return;
5605 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005606 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005607 if (nfs4_async_handle_error(task, data->res.server,
5608 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005609 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005610 return;
5611 }
5612 }
5613 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005614}
5615
5616static void nfs4_delegreturn_release(void *calldata)
5617{
Peng Tao039b7562014-07-03 13:05:02 +08005618 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005619 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005620
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005621 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005622 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005623 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5624 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005625 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005626 nfs_iput_and_deactive(inode);
5627 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005628 kfree(calldata);
5629}
5630
Andy Adamson938e1012009-04-01 09:22:28 -04005631static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5632{
5633 struct nfs4_delegreturndata *d_data;
5634
5635 d_data = (struct nfs4_delegreturndata *)data;
5636
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005637 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005638 return;
5639
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005640 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005641 &d_data->args.seq_args,
5642 &d_data->res.seq_res,
5643 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005644}
Andy Adamson938e1012009-04-01 09:22:28 -04005645
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005646static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005647 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005648 .rpc_call_done = nfs4_delegreturn_done,
5649 .rpc_release = nfs4_delegreturn_release,
5650};
5651
Trond Myklebuste6f81072008-01-24 18:14:34 -05005652static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005653{
5654 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005655 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005656 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005657 struct rpc_message msg = {
5658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5659 .rpc_cred = cred,
5660 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005661 struct rpc_task_setup task_setup_data = {
5662 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005663 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005664 .callback_ops = &nfs4_delegreturn_ops,
5665 .flags = RPC_TASK_ASYNC,
5666 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005667 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005668
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005669 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005670 if (data == NULL)
5671 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005672 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005673
5674 nfs4_state_protect(server->nfs_client,
5675 NFS_SP4_MACH_CRED_CLEANUP,
5676 &task_setup_data.rpc_client, &msg);
5677
Trond Myklebustfe650402006-01-03 09:55:18 +01005678 data->args.fhandle = &data->fh;
5679 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005680 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005681 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005682 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005683 data->res.fattr = &data->fattr;
5684 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005685 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005686 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005687 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005688 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005689 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005690 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005691 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005692 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005693 if (data->lr.roc) {
5694 data->args.lr_args = &data->lr.arg;
5695 data->res.lr_res = &data->lr.res;
5696 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005697 } else if (data->lr.roc) {
5698 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5699 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005700 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005701
Trond Myklebustc970aa82007-07-14 15:39:59 -04005702 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005703 msg.rpc_argp = &data->args;
5704 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005705 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005706 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005707 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005708 if (!issync)
5709 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005710 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005711 if (status != 0)
5712 goto out;
5713 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005714out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005715 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005716 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717}
5718
Trond Myklebuste6f81072008-01-24 18:14:34 -05005719int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720{
5721 struct nfs_server *server = NFS_SERVER(inode);
5722 struct nfs4_exception exception = { };
5723 int err;
5724 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005725 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005726 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 switch (err) {
5728 case -NFS4ERR_STALE_STATEID:
5729 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 case 0:
5731 return 0;
5732 }
5733 err = nfs4_handle_exception(server, err, &exception);
5734 } while (exception.retry);
5735 return err;
5736}
5737
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5739{
5740 struct inode *inode = state->inode;
5741 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005742 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005743 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005745 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005747 struct nfs_lockt_res res = {
5748 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 };
5750 struct rpc_message msg = {
5751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005752 .rpc_argp = &arg,
5753 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 .rpc_cred = state->owner->so_cred,
5755 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 struct nfs4_lock_state *lsp;
5757 int status;
5758
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005759 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005760 status = nfs4_set_lock_state(state, request);
5761 if (status != 0)
5762 goto out;
5763 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005764 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005765 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005766 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005767 switch (status) {
5768 case 0:
5769 request->fl_type = F_UNLCK;
5770 break;
5771 case -NFS4ERR_DENIED:
5772 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005774 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005775 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005776out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 return status;
5778}
5779
5780static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5781{
5782 struct nfs4_exception exception = { };
5783 int err;
5784
5785 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005786 err = _nfs4_proc_getlk(state, cmd, request);
5787 trace_nfs4_get_lock(request, state, cmd, err);
5788 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 &exception);
5790 } while (exception.retry);
5791 return err;
5792}
5793
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005794struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005795 struct nfs_locku_args arg;
5796 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005797 struct nfs4_lock_state *lsp;
5798 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005799 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005800 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005801 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005802 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005803};
5804
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005805static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5806 struct nfs_open_context *ctx,
5807 struct nfs4_lock_state *lsp,
5808 struct nfs_seqid *seqid)
5809{
5810 struct nfs4_unlockdata *p;
5811 struct inode *inode = lsp->ls_state->inode;
5812
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005813 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005814 if (p == NULL)
5815 return NULL;
5816 p->arg.fh = NFS_FH(inode);
5817 p->arg.fl = &p->fl;
5818 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005819 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005820 p->lsp = lsp;
5821 atomic_inc(&lsp->ls_count);
5822 /* Ensure we don't close file until we're done freeing locks! */
5823 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005824 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005825 memcpy(&p->fl, fl, sizeof(p->fl));
5826 p->server = NFS_SERVER(inode);
5827 return p;
5828}
5829
Trond Myklebust06f814a2006-01-03 09:55:07 +01005830static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005831{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005832 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005833 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005834 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005835 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005836 put_nfs_open_context(calldata->ctx);
5837 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005838}
5839
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005840static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005841{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005842 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005843
Trond Myklebust14516c32010-07-31 14:29:06 -04005844 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5845 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005846 switch (task->tk_status) {
5847 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005848 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005849 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005850 if (nfs4_update_lock_stateid(calldata->lsp,
5851 &calldata->res.stateid))
5852 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005853 case -NFS4ERR_ADMIN_REVOKED:
5854 case -NFS4ERR_EXPIRED:
5855 nfs4_free_revoked_stateid(calldata->server,
5856 &calldata->arg.stateid,
5857 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005858 case -NFS4ERR_BAD_STATEID:
5859 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005860 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005861 if (!nfs4_stateid_match(&calldata->arg.stateid,
5862 &calldata->lsp->ls_stateid))
5863 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005864 break;
5865 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005866 if (nfs4_async_handle_error(task, calldata->server,
5867 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005868 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005869 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005870 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005871}
5872
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005873static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005874{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005875 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005877 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
5878 nfs_async_iocounter_wait(task, calldata->l_ctx))
5879 return;
5880
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005881 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005882 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005883 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005884 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005885 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005886 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005887 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005888 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005889 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04005890 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005891 &calldata->res.seq_res,
5892 task) != 0)
5893 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005894 return;
5895out_no_action:
5896 task->tk_action = NULL;
5897out_wait:
5898 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899}
5900
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005901static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005902 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005903 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005904 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005905};
5906
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005907static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5908 struct nfs_open_context *ctx,
5909 struct nfs4_lock_state *lsp,
5910 struct nfs_seqid *seqid)
5911{
5912 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005913 struct rpc_message msg = {
5914 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5915 .rpc_cred = ctx->cred,
5916 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005917 struct rpc_task_setup task_setup_data = {
5918 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005919 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005920 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005921 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005922 .flags = RPC_TASK_ASYNC,
5923 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005924
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005925 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5926 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5927
Frank Filz137d6ac2007-07-09 15:32:29 -07005928 /* Ensure this is an unlock - when canceling a lock, the
5929 * canceled lock is passed in, and it won't be an unlock.
5930 */
5931 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005932 if (fl->fl_flags & FL_CLOSE)
5933 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07005934
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005935 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5936 if (data == NULL) {
5937 nfs_free_seqid(seqid);
5938 return ERR_PTR(-ENOMEM);
5939 }
5940
Chuck Levera9c92d62013-08-09 12:48:18 -04005941 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005942 msg.rpc_argp = &data->arg;
5943 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005944 task_setup_data.callback_data = data;
5945 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005946}
5947
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5949{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005950 struct inode *inode = state->inode;
5951 struct nfs4_state_owner *sp = state->owner;
5952 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005953 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005954 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005955 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005956 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005957 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005958 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959
Trond Myklebust9b073572006-06-29 16:38:34 -04005960 status = nfs4_set_lock_state(state, request);
5961 /* Unlock _before_ we do the RPC call */
5962 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005963 /* Exclude nfs_delegation_claim_locks() */
5964 mutex_lock(&sp->so_delegreturn_mutex);
5965 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005966 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005967 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005968 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005969 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005970 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005971 }
5972 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005973 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005974 if (status != 0)
5975 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005976 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005977 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005978 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5979 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005980 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5981 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005982 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005983 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005984 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005985 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005986 status = PTR_ERR(task);
5987 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005988 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005989 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005990 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005991out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005992 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005993 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005994 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995}
5996
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005997struct nfs4_lockdata {
5998 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005999 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006000 struct nfs4_lock_state *lsp;
6001 struct nfs_open_context *ctx;
6002 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006003 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006004 int rpc_status;
6005 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006006 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006007};
6008
6009static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006010 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6011 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006012{
6013 struct nfs4_lockdata *p;
6014 struct inode *inode = lsp->ls_state->inode;
6015 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006016 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006017
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006018 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006019 if (p == NULL)
6020 return NULL;
6021
6022 p->arg.fh = NFS_FH(inode);
6023 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006024 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006025 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006026 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006027 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6028 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006029 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006030 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006031 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006032 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006033 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006034 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006035 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006036 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006037 atomic_inc(&lsp->ls_count);
6038 p->ctx = get_nfs_open_context(ctx);
6039 memcpy(&p->fl, fl, sizeof(p->fl));
6040 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006041out_free_seqid:
6042 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006043out_free:
6044 kfree(p);
6045 return NULL;
6046}
6047
6048static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6049{
6050 struct nfs4_lockdata *data = calldata;
6051 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052
Harvey Harrison3110ff82008-05-02 13:42:44 -07006053 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006054 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006055 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006056 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006057 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006058 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006059 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006060 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006061 nfs4_stateid_copy(&data->arg.open_stateid,
6062 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006063 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006064 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006065 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006066 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006067 nfs4_stateid_copy(&data->arg.lock_stateid,
6068 &data->lsp->ls_stateid);
6069 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006070 if (!nfs4_valid_open_stateid(state)) {
6071 data->rpc_status = -EBADF;
6072 task->tk_action = NULL;
6073 goto out_release_open_seqid;
6074 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006075 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006076 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168a2010-06-16 09:52:26 -04006077 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006078 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006079 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006080 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006081out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006082 nfs_release_seqid(data->arg.open_seqid);
6083out_release_lock_seqid:
6084 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006085out_wait:
6086 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006087 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006088}
6089
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006090static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6091{
6092 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006093 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006094
Harvey Harrison3110ff82008-05-02 13:42:44 -07006095 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006096
Trond Myklebust14516c32010-07-31 14:29:06 -04006097 if (!nfs4_sequence_done(task, &data->res.seq_res))
6098 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006099
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006100 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006101 switch (task->tk_status) {
6102 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006103 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006104 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006105 if (data->arg.new_lock) {
6106 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006107 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006108 rpc_restart_call_prepare(task);
6109 break;
6110 }
6111 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006112 if (data->arg.new_lock_owner != 0) {
6113 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6114 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6115 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6116 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6117 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006118 break;
6119 case -NFS4ERR_BAD_STATEID:
6120 case -NFS4ERR_OLD_STATEID:
6121 case -NFS4ERR_STALE_STATEID:
6122 case -NFS4ERR_EXPIRED:
6123 if (data->arg.new_lock_owner != 0) {
6124 if (!nfs4_stateid_match(&data->arg.open_stateid,
6125 &lsp->ls_state->open_stateid))
6126 rpc_restart_call_prepare(task);
6127 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6128 &lsp->ls_stateid))
6129 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006130 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006131 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006132}
6133
6134static void nfs4_lock_release(void *calldata)
6135{
6136 struct nfs4_lockdata *data = calldata;
6137
Harvey Harrison3110ff82008-05-02 13:42:44 -07006138 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006139 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006140 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006141 struct rpc_task *task;
6142 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6143 data->arg.lock_seqid);
6144 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006145 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006146 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006147 } else
6148 nfs_free_seqid(data->arg.lock_seqid);
6149 nfs4_put_lock_state(data->lsp);
6150 put_nfs_open_context(data->ctx);
6151 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006152 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006153}
6154
6155static const struct rpc_call_ops nfs4_lock_ops = {
6156 .rpc_call_prepare = nfs4_lock_prepare,
6157 .rpc_call_done = nfs4_lock_done,
6158 .rpc_release = nfs4_lock_release,
6159};
6160
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006161static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6162{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006163 switch (error) {
6164 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006165 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006166 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006167 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006168 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006169 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006170 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006171 break;
6172 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006173 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006174 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006175 };
6176}
6177
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006178static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006179{
6180 struct nfs4_lockdata *data;
6181 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006182 struct rpc_message msg = {
6183 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6184 .rpc_cred = state->owner->so_cred,
6185 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006186 struct rpc_task_setup task_setup_data = {
6187 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006188 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006189 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006190 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006191 .flags = RPC_TASK_ASYNC,
6192 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006193 int ret;
6194
Harvey Harrison3110ff82008-05-02 13:42:44 -07006195 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006196 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006197 fl->fl_u.nfs4_fl.owner,
6198 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006199 if (data == NULL)
6200 return -ENOMEM;
6201 if (IS_SETLKW(cmd))
6202 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006203 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006204 msg.rpc_argp = &data->arg;
6205 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006206 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006207 if (recovery_type > NFS_LOCK_NEW) {
6208 if (recovery_type == NFS_LOCK_RECLAIM)
6209 data->arg.reclaim = NFS_LOCK_RECLAIM;
6210 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006211 } else
6212 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006213 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006214 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006215 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006216 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006217 if (ret == 0) {
6218 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006219 if (ret)
6220 nfs4_handle_setlk_error(data->server, data->lsp,
6221 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006222 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006223 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006224 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006225 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006226 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006227 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228}
6229
6230static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6231{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006232 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006233 struct nfs4_exception exception = {
6234 .inode = state->inode,
6235 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006236 int err;
6237
6238 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006239 /* Cache the lock if possible... */
6240 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6241 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006242 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006243 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006244 break;
6245 nfs4_handle_exception(server, err, &exception);
6246 } while (exception.retry);
6247 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248}
6249
6250static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6251{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006252 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006253 struct nfs4_exception exception = {
6254 .inode = state->inode,
6255 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006256 int err;
6257
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006258 err = nfs4_set_lock_state(state, request);
6259 if (err != 0)
6260 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006261 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006262 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6263 return 0;
6264 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006265 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006266 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6267 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006268 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006269 switch (err) {
6270 default:
6271 goto out;
6272 case -NFS4ERR_GRACE:
6273 case -NFS4ERR_DELAY:
6274 nfs4_handle_exception(server, err, &exception);
6275 err = 0;
6276 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006277 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006278out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006279 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280}
6281
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006282#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006283static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6284{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006285 struct nfs4_lock_state *lsp;
6286 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006287
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006288 status = nfs4_set_lock_state(state, request);
6289 if (status != 0)
6290 return status;
6291 lsp = request->fl_u.nfs4_fl.owner;
6292 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6293 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6294 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006295 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006296}
6297#endif
6298
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6300{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006301 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006302 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006303 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006304 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305
Trond Myklebust01c3b862006-06-29 16:38:39 -04006306 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006307 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006308 if (status < 0)
6309 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006310 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006311 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006312 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006313 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006314 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006315 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006316 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006317 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006318 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006319 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006320 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006321 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006322 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006323 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006324out:
6325 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 return status;
6327}
6328
6329static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6330{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006331 struct nfs4_exception exception = {
6332 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006333 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006334 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 int err;
6336
6337 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006338 err = _nfs4_proc_setlk(state, cmd, request);
6339 if (err == -NFS4ERR_DENIED)
6340 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006342 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 } while (exception.retry);
6344 return err;
6345}
6346
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006347#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6348#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6349
6350static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006351nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6352 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006353{
6354 int status = -ERESTARTSYS;
6355 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6356
6357 while(!signalled()) {
6358 status = nfs4_proc_setlk(state, cmd, request);
6359 if ((status != -EAGAIN) || IS_SETLK(cmd))
6360 break;
6361 freezable_schedule_timeout_interruptible(timeout);
6362 timeout *= 2;
6363 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6364 status = -ERESTARTSYS;
6365 }
6366 return status;
6367}
6368
Jeff Laytona1d617d82016-09-17 18:17:39 -04006369#ifdef CONFIG_NFS_V4_1
6370struct nfs4_lock_waiter {
6371 struct task_struct *task;
6372 struct inode *inode;
6373 struct nfs_lowner *owner;
6374 bool notified;
6375};
6376
6377static int
6378nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6379{
6380 int ret;
6381 struct cb_notify_lock_args *cbnl = key;
6382 struct nfs4_lock_waiter *waiter = wait->private;
6383 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6384 *wowner = waiter->owner;
6385
6386 /* Only wake if the callback was for the same owner */
6387 if (lowner->clientid != wowner->clientid ||
6388 lowner->id != wowner->id ||
6389 lowner->s_dev != wowner->s_dev)
6390 return 0;
6391
6392 /* Make sure it's for the right inode */
6393 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6394 return 0;
6395
6396 waiter->notified = true;
6397
6398 /* override "private" so we can use default_wake_function */
6399 wait->private = waiter->task;
6400 ret = autoremove_wake_function(wait, mode, flags, key);
6401 wait->private = waiter;
6402 return ret;
6403}
6404
6405static int
6406nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6407{
6408 int status = -ERESTARTSYS;
6409 unsigned long flags;
6410 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6411 struct nfs_server *server = NFS_SERVER(state->inode);
6412 struct nfs_client *clp = server->nfs_client;
6413 wait_queue_head_t *q = &clp->cl_lock_waitq;
6414 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6415 .id = lsp->ls_seqid.owner_id,
6416 .s_dev = server->s_dev };
6417 struct nfs4_lock_waiter waiter = { .task = current,
6418 .inode = state->inode,
6419 .owner = &owner,
6420 .notified = false };
6421 wait_queue_t wait;
6422
6423 /* Don't bother with waitqueue if we don't expect a callback */
6424 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6425 return nfs4_retry_setlk_simple(state, cmd, request);
6426
6427 init_wait(&wait);
6428 wait.private = &waiter;
6429 wait.func = nfs4_wake_lock_waiter;
6430 add_wait_queue(q, &wait);
6431
6432 while(!signalled()) {
6433 status = nfs4_proc_setlk(state, cmd, request);
6434 if ((status != -EAGAIN) || IS_SETLK(cmd))
6435 break;
6436
6437 status = -ERESTARTSYS;
6438 spin_lock_irqsave(&q->lock, flags);
6439 if (waiter.notified) {
6440 spin_unlock_irqrestore(&q->lock, flags);
6441 continue;
6442 }
6443 set_current_state(TASK_INTERRUPTIBLE);
6444 spin_unlock_irqrestore(&q->lock, flags);
6445
6446 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6447 }
6448
6449 finish_wait(q, &wait);
6450 return status;
6451}
6452#else /* !CONFIG_NFS_V4_1 */
6453static inline int
6454nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6455{
6456 return nfs4_retry_setlk_simple(state, cmd, request);
6457}
6458#endif
6459
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460static int
6461nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6462{
6463 struct nfs_open_context *ctx;
6464 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 int status;
6466
6467 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006468 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469 state = ctx->state;
6470
Trond Myklebustd9531262009-07-21 19:22:38 -04006471 if (IS_GETLK(cmd)) {
6472 if (state != NULL)
6473 return nfs4_proc_getlk(state, F_GETLK, request);
6474 return 0;
6475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476
6477 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6478 return -EINVAL;
6479
Trond Myklebustd9531262009-07-21 19:22:38 -04006480 if (request->fl_type == F_UNLCK) {
6481 if (state != NULL)
6482 return nfs4_proc_unlck(state, cmd, request);
6483 return 0;
6484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485
Trond Myklebustd9531262009-07-21 19:22:38 -04006486 if (state == NULL)
6487 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006488
6489 if ((request->fl_flags & FL_POSIX) &&
6490 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6491 return -ENOLCK;
6492
Jeff Layton1ea67db2016-09-17 18:17:37 -04006493 status = nfs4_set_lock_state(state, request);
6494 if (status != 0)
6495 return status;
6496
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006497 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498}
6499
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006500int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006501{
6502 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006503 int err;
6504
6505 err = nfs4_set_lock_state(state, fl);
6506 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006507 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006508 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006509 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006510}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006511
Trond Myklebustcf470c32012-03-07 13:49:12 -05006512struct nfs_release_lockowner_data {
6513 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006514 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006515 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006516 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006517 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006518};
6519
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006520static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6521{
6522 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006523 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006524 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6525 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006526 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006527 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006528}
6529
6530static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6531{
6532 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006533 struct nfs_server *server = data->server;
6534
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006535 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006536
6537 switch (task->tk_status) {
6538 case 0:
6539 renew_lease(server, data->timestamp);
6540 break;
6541 case -NFS4ERR_STALE_CLIENTID:
6542 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006543 nfs4_schedule_lease_recovery(server->nfs_client);
6544 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006545 case -NFS4ERR_LEASE_MOVED:
6546 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006547 if (nfs4_async_handle_error(task, server,
6548 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006549 rpc_restart_call_prepare(task);
6550 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006551}
6552
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006553static void nfs4_release_lockowner_release(void *calldata)
6554{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006555 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006556 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006557 kfree(calldata);
6558}
6559
Trond Myklebust17280172012-03-11 13:11:00 -04006560static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006561 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6562 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006563 .rpc_release = nfs4_release_lockowner_release,
6564};
6565
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006566static void
6567nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006568{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006569 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006570 struct rpc_message msg = {
6571 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6572 };
6573
6574 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006575 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006576
Trond Myklebustcf470c32012-03-07 13:49:12 -05006577 data = kmalloc(sizeof(*data), GFP_NOFS);
6578 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006579 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006580 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006581 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006582 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6583 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6584 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006585
Trond Myklebustcf470c32012-03-07 13:49:12 -05006586 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006587 msg.rpc_resp = &data->res;
6588 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006589 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006590}
6591
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006592#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6593
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006594static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006595 struct dentry *unused, struct inode *inode,
6596 const char *key, const void *buf,
6597 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006598{
Al Viro59301222016-05-27 10:19:30 -04006599 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006600}
6601
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006602static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006603 struct dentry *unused, struct inode *inode,
6604 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006605{
Al Virob2968212016-04-10 20:48:24 -04006606 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006607}
6608
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006609static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006610{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006611 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006612}
6613
David Quigleyc9bccef2013-05-22 12:50:45 -04006614#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006615
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006616static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006617 struct dentry *unused, struct inode *inode,
6618 const char *key, const void *buf,
6619 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006620{
6621 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006622 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006623
6624 return -EOPNOTSUPP;
6625}
6626
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006627static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006628 struct dentry *unused, struct inode *inode,
6629 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006630{
6631 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006632 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006633 return -EOPNOTSUPP;
6634}
6635
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006636static ssize_t
6637nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006638{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006639 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006640
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006641 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6642 len = security_inode_listsecurity(inode, list, list_len);
6643 if (list_len && len > list_len)
6644 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006645 }
6646 return len;
6647}
6648
6649static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6650 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006651 .get = nfs4_xattr_get_nfs4_label,
6652 .set = nfs4_xattr_set_nfs4_label,
6653};
David Quigleyc9bccef2013-05-22 12:50:45 -04006654
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006655#else
6656
6657static ssize_t
6658nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6659{
6660 return 0;
6661}
6662
6663#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006664
Andy Adamson533eb462011-06-13 18:25:56 -04006665/*
6666 * nfs_fhget will use either the mounted_on_fileid or the fileid
6667 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006668static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6669{
Andy Adamson533eb462011-06-13 18:25:56 -04006670 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6671 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6672 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006673 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006674 return;
6675
6676 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006677 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006678 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6679 fattr->nlink = 2;
6680}
6681
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006682static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6683 const struct qstr *name,
6684 struct nfs4_fs_locations *fs_locations,
6685 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006686{
6687 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006688 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006689 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006690 };
6691 struct nfs4_fs_locations_arg args = {
6692 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006693 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006694 .page = page,
6695 .bitmask = bitmask,
6696 };
Benny Halevy22958462009-04-01 09:22:02 -04006697 struct nfs4_fs_locations_res res = {
6698 .fs_locations = fs_locations,
6699 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006700 struct rpc_message msg = {
6701 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6702 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006703 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006704 };
6705 int status;
6706
Harvey Harrison3110ff82008-05-02 13:42:44 -07006707 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006708
6709 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6710 * is not supported */
6711 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6712 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6713 else
6714 bitmask[0] |= FATTR4_WORD0_FILEID;
6715
Trond Myklebustc228fd32007-01-13 02:28:11 -05006716 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006717 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006718 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006719 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006720 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006721 return status;
6722}
6723
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006724int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6725 const struct qstr *name,
6726 struct nfs4_fs_locations *fs_locations,
6727 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006728{
6729 struct nfs4_exception exception = { };
6730 int err;
6731 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006732 err = _nfs4_proc_fs_locations(client, dir, name,
6733 fs_locations, page);
6734 trace_nfs4_get_fs_locations(dir, name, err);
6735 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006736 &exception);
6737 } while (exception.retry);
6738 return err;
6739}
6740
Chuck Leverb03d7352013-10-17 14:12:50 -04006741/*
6742 * This operation also signals the server that this client is
6743 * performing migration recovery. The server can stop returning
6744 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6745 * appended to this compound to identify the client ID which is
6746 * performing recovery.
6747 */
6748static int _nfs40_proc_get_locations(struct inode *inode,
6749 struct nfs4_fs_locations *locations,
6750 struct page *page, struct rpc_cred *cred)
6751{
6752 struct nfs_server *server = NFS_SERVER(inode);
6753 struct rpc_clnt *clnt = server->client;
6754 u32 bitmask[2] = {
6755 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6756 };
6757 struct nfs4_fs_locations_arg args = {
6758 .clientid = server->nfs_client->cl_clientid,
6759 .fh = NFS_FH(inode),
6760 .page = page,
6761 .bitmask = bitmask,
6762 .migration = 1, /* skip LOOKUP */
6763 .renew = 1, /* append RENEW */
6764 };
6765 struct nfs4_fs_locations_res res = {
6766 .fs_locations = locations,
6767 .migration = 1,
6768 .renew = 1,
6769 };
6770 struct rpc_message msg = {
6771 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6772 .rpc_argp = &args,
6773 .rpc_resp = &res,
6774 .rpc_cred = cred,
6775 };
6776 unsigned long now = jiffies;
6777 int status;
6778
6779 nfs_fattr_init(&locations->fattr);
6780 locations->server = server;
6781 locations->nlocations = 0;
6782
6783 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6784 nfs4_set_sequence_privileged(&args.seq_args);
6785 status = nfs4_call_sync_sequence(clnt, server, &msg,
6786 &args.seq_args, &res.seq_res);
6787 if (status)
6788 return status;
6789
6790 renew_lease(server, now);
6791 return 0;
6792}
6793
6794#ifdef CONFIG_NFS_V4_1
6795
6796/*
6797 * This operation also signals the server that this client is
6798 * performing migration recovery. The server can stop asserting
6799 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6800 * performing this operation is identified in the SEQUENCE
6801 * operation in this compound.
6802 *
6803 * When the client supports GETATTR(fs_locations_info), it can
6804 * be plumbed in here.
6805 */
6806static int _nfs41_proc_get_locations(struct inode *inode,
6807 struct nfs4_fs_locations *locations,
6808 struct page *page, struct rpc_cred *cred)
6809{
6810 struct nfs_server *server = NFS_SERVER(inode);
6811 struct rpc_clnt *clnt = server->client;
6812 u32 bitmask[2] = {
6813 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6814 };
6815 struct nfs4_fs_locations_arg args = {
6816 .fh = NFS_FH(inode),
6817 .page = page,
6818 .bitmask = bitmask,
6819 .migration = 1, /* skip LOOKUP */
6820 };
6821 struct nfs4_fs_locations_res res = {
6822 .fs_locations = locations,
6823 .migration = 1,
6824 };
6825 struct rpc_message msg = {
6826 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6827 .rpc_argp = &args,
6828 .rpc_resp = &res,
6829 .rpc_cred = cred,
6830 };
6831 int status;
6832
6833 nfs_fattr_init(&locations->fattr);
6834 locations->server = server;
6835 locations->nlocations = 0;
6836
6837 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6838 nfs4_set_sequence_privileged(&args.seq_args);
6839 status = nfs4_call_sync_sequence(clnt, server, &msg,
6840 &args.seq_args, &res.seq_res);
6841 if (status == NFS4_OK &&
6842 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6843 status = -NFS4ERR_LEASE_MOVED;
6844 return status;
6845}
6846
6847#endif /* CONFIG_NFS_V4_1 */
6848
6849/**
6850 * nfs4_proc_get_locations - discover locations for a migrated FSID
6851 * @inode: inode on FSID that is migrating
6852 * @locations: result of query
6853 * @page: buffer
6854 * @cred: credential to use for this operation
6855 *
6856 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6857 * operation failed, or a negative errno if a local error occurred.
6858 *
6859 * On success, "locations" is filled in, but if the server has
6860 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6861 * asserted.
6862 *
6863 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6864 * from this client that require migration recovery.
6865 */
6866int nfs4_proc_get_locations(struct inode *inode,
6867 struct nfs4_fs_locations *locations,
6868 struct page *page, struct rpc_cred *cred)
6869{
6870 struct nfs_server *server = NFS_SERVER(inode);
6871 struct nfs_client *clp = server->nfs_client;
6872 const struct nfs4_mig_recovery_ops *ops =
6873 clp->cl_mvops->mig_recovery_ops;
6874 struct nfs4_exception exception = { };
6875 int status;
6876
6877 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6878 (unsigned long long)server->fsid.major,
6879 (unsigned long long)server->fsid.minor,
6880 clp->cl_hostname);
6881 nfs_display_fhandle(NFS_FH(inode), __func__);
6882
6883 do {
6884 status = ops->get_locations(inode, locations, page, cred);
6885 if (status != -NFS4ERR_DELAY)
6886 break;
6887 nfs4_handle_exception(server, status, &exception);
6888 } while (exception.retry);
6889 return status;
6890}
6891
Chuck Lever44c99932013-10-17 14:13:30 -04006892/*
6893 * This operation also signals the server that this client is
6894 * performing "lease moved" recovery. The server can stop
6895 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6896 * is appended to this compound to identify the client ID which is
6897 * performing recovery.
6898 */
6899static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6900{
6901 struct nfs_server *server = NFS_SERVER(inode);
6902 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6903 struct rpc_clnt *clnt = server->client;
6904 struct nfs4_fsid_present_arg args = {
6905 .fh = NFS_FH(inode),
6906 .clientid = clp->cl_clientid,
6907 .renew = 1, /* append RENEW */
6908 };
6909 struct nfs4_fsid_present_res res = {
6910 .renew = 1,
6911 };
6912 struct rpc_message msg = {
6913 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6914 .rpc_argp = &args,
6915 .rpc_resp = &res,
6916 .rpc_cred = cred,
6917 };
6918 unsigned long now = jiffies;
6919 int status;
6920
6921 res.fh = nfs_alloc_fhandle();
6922 if (res.fh == NULL)
6923 return -ENOMEM;
6924
6925 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6926 nfs4_set_sequence_privileged(&args.seq_args);
6927 status = nfs4_call_sync_sequence(clnt, server, &msg,
6928 &args.seq_args, &res.seq_res);
6929 nfs_free_fhandle(res.fh);
6930 if (status)
6931 return status;
6932
6933 do_renew_lease(clp, now);
6934 return 0;
6935}
6936
6937#ifdef CONFIG_NFS_V4_1
6938
6939/*
6940 * This operation also signals the server that this client is
6941 * performing "lease moved" recovery. The server can stop asserting
6942 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6943 * this operation is identified in the SEQUENCE operation in this
6944 * compound.
6945 */
6946static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6947{
6948 struct nfs_server *server = NFS_SERVER(inode);
6949 struct rpc_clnt *clnt = server->client;
6950 struct nfs4_fsid_present_arg args = {
6951 .fh = NFS_FH(inode),
6952 };
6953 struct nfs4_fsid_present_res res = {
6954 };
6955 struct rpc_message msg = {
6956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6957 .rpc_argp = &args,
6958 .rpc_resp = &res,
6959 .rpc_cred = cred,
6960 };
6961 int status;
6962
6963 res.fh = nfs_alloc_fhandle();
6964 if (res.fh == NULL)
6965 return -ENOMEM;
6966
6967 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6968 nfs4_set_sequence_privileged(&args.seq_args);
6969 status = nfs4_call_sync_sequence(clnt, server, &msg,
6970 &args.seq_args, &res.seq_res);
6971 nfs_free_fhandle(res.fh);
6972 if (status == NFS4_OK &&
6973 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6974 status = -NFS4ERR_LEASE_MOVED;
6975 return status;
6976}
6977
6978#endif /* CONFIG_NFS_V4_1 */
6979
6980/**
6981 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6982 * @inode: inode on FSID to check
6983 * @cred: credential to use for this operation
6984 *
6985 * Server indicates whether the FSID is present, moved, or not
6986 * recognized. This operation is necessary to clear a LEASE_MOVED
6987 * condition for this client ID.
6988 *
6989 * Returns NFS4_OK if the FSID is present on this server,
6990 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6991 * NFS4ERR code if some error occurred on the server, or a
6992 * negative errno if a local failure occurred.
6993 */
6994int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6995{
6996 struct nfs_server *server = NFS_SERVER(inode);
6997 struct nfs_client *clp = server->nfs_client;
6998 const struct nfs4_mig_recovery_ops *ops =
6999 clp->cl_mvops->mig_recovery_ops;
7000 struct nfs4_exception exception = { };
7001 int status;
7002
7003 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7004 (unsigned long long)server->fsid.major,
7005 (unsigned long long)server->fsid.minor,
7006 clp->cl_hostname);
7007 nfs_display_fhandle(NFS_FH(inode), __func__);
7008
7009 do {
7010 status = ops->fsid_present(inode, cred);
7011 if (status != -NFS4ERR_DELAY)
7012 break;
7013 nfs4_handle_exception(server, status, &exception);
7014 } while (exception.retry);
7015 return status;
7016}
7017
Andy Adamson5ec16a82013-08-08 10:57:55 -04007018/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007019 * If 'use_integrity' is true and the state managment nfs_client
7020 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7021 * and the machine credential as per RFC3530bis and RFC5661 Security
7022 * Considerations sections. Otherwise, just use the user cred with the
7023 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007024 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007025static 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 +00007026{
7027 int status;
7028 struct nfs4_secinfo_arg args = {
7029 .dir_fh = NFS_FH(dir),
7030 .name = name,
7031 };
7032 struct nfs4_secinfo_res res = {
7033 .flavors = flavors,
7034 };
7035 struct rpc_message msg = {
7036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7037 .rpc_argp = &args,
7038 .rpc_resp = &res,
7039 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007040 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007041 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007042
7043 if (use_integrity) {
7044 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007045 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7046 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007047 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007048
7049 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007050
7051 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7052 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7053
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007054 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7055 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007056 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007057
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007058 if (cred)
7059 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007060
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007061 return status;
7062}
7063
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007064int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7065 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007066{
7067 struct nfs4_exception exception = { };
7068 int err;
7069 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007070 err = -NFS4ERR_WRONGSEC;
7071
7072 /* try to use integrity protection with machine cred */
7073 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7074 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7075
7076 /*
7077 * if unable to use integrity protection, or SECINFO with
7078 * integrity protection returns NFS4ERR_WRONGSEC (which is
7079 * disallowed by spec, but exists in deployed servers) use
7080 * the current filesystem's rpc_client and the user cred.
7081 */
7082 if (err == -NFS4ERR_WRONGSEC)
7083 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7084
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007085 trace_nfs4_secinfo(dir, name, err);
7086 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007087 &exception);
7088 } while (exception.retry);
7089 return err;
7090}
7091
Andy Adamson557134a2009-04-01 09:21:53 -04007092#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007093/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007094 * Check the exchange flags returned by the server for invalid flags, having
7095 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7096 * DS flags set.
7097 */
7098static int nfs4_check_cl_exchange_flags(u32 flags)
7099{
7100 if (flags & ~EXCHGID4_FLAG_MASK_R)
7101 goto out_inval;
7102 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7103 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7104 goto out_inval;
7105 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7106 goto out_inval;
7107 return NFS_OK;
7108out_inval:
7109 return -NFS4ERR_INVAL;
7110}
7111
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007112static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007113nfs41_same_server_scope(struct nfs41_server_scope *a,
7114 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007115{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007116 if (a->server_scope_sz != b->server_scope_sz)
7117 return false;
7118 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007119}
7120
Andy Adamson02a95de2016-02-05 16:08:37 -05007121static void
7122nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7123{
7124}
7125
7126static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7127 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7128};
7129
Andy Adamson357f54d2010-12-14 10:11:57 -05007130/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007131 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007132 *
7133 * The 4.1 client currently uses the same TCP connection for the
7134 * fore and backchannel.
7135 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007136static
7137int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7138 struct rpc_xprt *xprt,
7139 struct nfs_client *clp,
7140 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007141{
7142 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007143 struct nfs41_bind_conn_to_session_args args = {
7144 .client = clp,
7145 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7146 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007147 struct nfs41_bind_conn_to_session_res res;
7148 struct rpc_message msg = {
7149 .rpc_proc =
7150 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007151 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007152 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007153 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007154 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007155 struct rpc_task_setup task_setup_data = {
7156 .rpc_client = clnt,
7157 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007158 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007159 .rpc_message = &msg,
7160 .flags = RPC_TASK_TIMEOUT,
7161 };
7162 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007163
Trond Myklebust71a097c2015-02-18 09:27:18 -08007164 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7165 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7166 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007167
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007168 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7169 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7170 args.dir = NFS4_CDFC4_FORE;
7171
7172 task = rpc_run_task(&task_setup_data);
7173 if (!IS_ERR(task)) {
7174 status = task->tk_status;
7175 rpc_put_task(task);
7176 } else
7177 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007178 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007179 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007180 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007181 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7182 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007183 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007184 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007185 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007186 dprintk("NFS: %s: Unexpected direction from server\n",
7187 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007188 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007189 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007190 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007191 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7192 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007193 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007194 }
7195 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007196
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007197 return status;
7198}
7199
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007200struct rpc_bind_conn_calldata {
7201 struct nfs_client *clp;
7202 struct rpc_cred *cred;
7203};
7204
7205static int
7206nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7207 struct rpc_xprt *xprt,
7208 void *calldata)
7209{
7210 struct rpc_bind_conn_calldata *p = calldata;
7211
7212 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7213}
7214
7215int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7216{
7217 struct rpc_bind_conn_calldata data = {
7218 .clp = clp,
7219 .cred = cred,
7220 };
7221 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7222 nfs4_proc_bind_conn_to_session_callback, &data);
7223}
7224
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007225/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007226 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7227 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007228 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007229static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7230 .how = SP4_MACH_CRED,
7231 .enforce.u.words = {
7232 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7233 1 << (OP_EXCHANGE_ID - 32) |
7234 1 << (OP_CREATE_SESSION - 32) |
7235 1 << (OP_DESTROY_SESSION - 32) |
7236 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007237 },
7238 .allow.u.words = {
7239 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007240 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007241 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007242 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007243 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007244 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007245 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007246 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007247 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007248 1 << (OP_FREE_STATEID - 32) |
7249 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007250 }
7251};
7252
7253/*
7254 * Select the state protection mode for client `clp' given the server results
7255 * from exchange_id in `sp'.
7256 *
7257 * Returns 0 on success, negative errno otherwise.
7258 */
7259static int nfs4_sp4_select_mode(struct nfs_client *clp,
7260 struct nfs41_state_protection *sp)
7261{
7262 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7263 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7264 1 << (OP_EXCHANGE_ID - 32) |
7265 1 << (OP_CREATE_SESSION - 32) |
7266 1 << (OP_DESTROY_SESSION - 32) |
7267 1 << (OP_DESTROY_CLIENTID - 32)
7268 };
7269 unsigned int i;
7270
7271 if (sp->how == SP4_MACH_CRED) {
7272 /* Print state protect result */
7273 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7274 for (i = 0; i <= LAST_NFS4_OP; i++) {
7275 if (test_bit(i, sp->enforce.u.longs))
7276 dfprintk(MOUNT, " enforce op %d\n", i);
7277 if (test_bit(i, sp->allow.u.longs))
7278 dfprintk(MOUNT, " allow op %d\n", i);
7279 }
7280
7281 /* make sure nothing is on enforce list that isn't supported */
7282 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7283 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7284 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7285 return -EINVAL;
7286 }
7287 }
7288
7289 /*
7290 * Minimal mode - state operations are allowed to use machine
7291 * credential. Note this already happens by default, so the
7292 * client doesn't have to do anything more than the negotiation.
7293 *
7294 * NOTE: we don't care if EXCHANGE_ID is in the list -
7295 * we're already using the machine cred for exchange_id
7296 * and will never use a different cred.
7297 */
7298 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7299 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7300 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7301 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7302 dfprintk(MOUNT, "sp4_mach_cred:\n");
7303 dfprintk(MOUNT, " minimal mode enabled\n");
7304 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7305 } else {
7306 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7307 return -EINVAL;
7308 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007309
7310 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007311 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7312 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007313 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7314 dfprintk(MOUNT, " cleanup mode enabled\n");
7315 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7316 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007317
Andrew Elble99ade3c2015-12-02 09:39:51 -05007318 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7319 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7320 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7321 &clp->cl_sp4_flags);
7322 }
7323
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007324 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7325 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7326 dfprintk(MOUNT, " secinfo mode enabled\n");
7327 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7328 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007329
7330 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7331 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7332 dfprintk(MOUNT, " stateid mode enabled\n");
7333 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7334 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007335
7336 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7337 dfprintk(MOUNT, " write mode enabled\n");
7338 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7339 }
7340
7341 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7342 dfprintk(MOUNT, " commit mode enabled\n");
7343 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7344 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007345 }
7346
7347 return 0;
7348}
7349
Andy Adamson8d89bd72016-09-09 09:22:18 -04007350struct nfs41_exchange_id_data {
7351 struct nfs41_exchange_id_res res;
7352 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007353 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007354 int rpc_status;
7355};
7356
7357static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007358{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007359 struct nfs41_exchange_id_data *cdata =
7360 (struct nfs41_exchange_id_data *)data;
7361 struct nfs_client *clp = cdata->args.client;
7362 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007363
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007364 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007365
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007366 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007367 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007368
7369 if (cdata->xprt && status == 0) {
7370 status = nfs4_detect_session_trunking(clp, &cdata->res,
7371 cdata->xprt);
7372 goto out;
7373 }
7374
Andy Adamson8d89bd72016-09-09 09:22:18 -04007375 if (status == 0)
7376 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007377
Chuck Lever177313f2012-05-21 22:44:58 -04007378 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007379 clp->cl_clientid = cdata->res.clientid;
7380 clp->cl_exchange_flags = cdata->res.flags;
Chuck Lever838edb92017-06-08 11:52:52 -04007381 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007382 /* Client ID is not confirmed */
Chuck Lever838edb92017-06-08 11:52:52 -04007383 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R))
Trond Myklebuste11259f2015-03-03 20:35:31 -05007384 clear_bit(NFS4_SESSION_ESTABLISHED,
Chuck Lever838edb92017-06-08 11:52:52 -04007385 &clp->cl_session->session_state);
Trond Myklebust32b01312012-05-26 13:41:04 -04007386
Chuck Leveracdeb692012-05-21 22:46:16 -04007387 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007388 clp->cl_serverowner = cdata->res.server_owner;
7389 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007390
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007391 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007392 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007393 clp->cl_implid = cdata->res.impl_id;
7394 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007395
Chuck Lever177313f2012-05-21 22:44:58 -04007396 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007397 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007398 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007399 dprintk("%s: server_scope mismatch detected\n",
7400 __func__);
7401 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007402 kfree(clp->cl_serverscope);
7403 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007404 }
7405
Chuck Lever177313f2012-05-21 22:44:58 -04007406 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007407 clp->cl_serverscope = cdata->res.server_scope;
7408 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007409 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007410 /* Save the EXCHANGE_ID verifier session trunk tests */
7411 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7412 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007413 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007414out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007415 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007416 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007417}
7418
7419static void nfs4_exchange_id_release(void *data)
7420{
7421 struct nfs41_exchange_id_data *cdata =
7422 (struct nfs41_exchange_id_data *)data;
7423
Andy Adamsonad0849a2016-09-09 09:22:28 -04007424 if (cdata->xprt) {
7425 xprt_put(cdata->xprt);
7426 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7427 }
Olga Kornievskaia63513232017-03-13 10:36:19 -04007428 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007429 kfree(cdata->res.impl_id);
7430 kfree(cdata->res.server_scope);
7431 kfree(cdata->res.server_owner);
7432 kfree(cdata);
7433}
7434
7435static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7436 .rpc_call_done = nfs4_exchange_id_done,
7437 .rpc_release = nfs4_exchange_id_release,
7438};
7439
Benny Halevy99fe60d2009-04-01 09:22:29 -04007440/*
7441 * _nfs4_proc_exchange_id()
7442 *
7443 * Wrapper for EXCHANGE_ID operation.
7444 */
7445static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007446 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007447{
7448 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007449 struct rpc_message msg = {
7450 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007451 .rpc_cred = cred,
7452 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007453 struct rpc_task_setup task_setup_data = {
7454 .rpc_client = clp->cl_rpcclient,
7455 .callback_ops = &nfs4_exchange_id_call_ops,
7456 .rpc_message = &msg,
7457 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7458 };
7459 struct nfs41_exchange_id_data *calldata;
7460 struct rpc_task *task;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007461 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007462
7463 if (!atomic_inc_not_zero(&clp->cl_count))
Anna Schumakere917f0d2017-04-07 14:15:22 -04007464 return -EIO;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007465
Andy Adamson8d89bd72016-09-09 09:22:18 -04007466 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04007467 if (!calldata) {
7468 nfs_put_client(clp);
Anna Schumakere917f0d2017-04-07 14:15:22 -04007469 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04007470 }
Benny Halevy99fe60d2009-04-01 09:22:29 -04007471
Andy Adamsonad0849a2016-09-09 09:22:28 -04007472 if (!xprt)
7473 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007474
7475 status = nfs4_init_uniform_client_string(clp);
7476 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007477 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007478
Andy Adamson8d89bd72016-09-09 09:22:18 -04007479 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7480 GFP_NOFS);
7481 status = -ENOMEM;
7482 if (unlikely(calldata->res.server_owner == NULL))
7483 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007484
Andy Adamson8d89bd72016-09-09 09:22:18 -04007485 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007486 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007487 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007488 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007489
Andy Adamson8d89bd72016-09-09 09:22:18 -04007490 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7491 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007492 goto out_server_scope;
7493
7494 switch (sp4_how) {
7495 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007496 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007497 break;
7498
7499 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007500 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007501 break;
7502
7503 default:
7504 /* unsupported! */
7505 WARN_ON_ONCE(1);
7506 status = -EINVAL;
7507 goto out_impl_id;
7508 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007509 if (xprt) {
7510 calldata->xprt = xprt;
7511 task_setup_data.rpc_xprt = xprt;
7512 task_setup_data.flags =
7513 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7514 calldata->args.verifier = &clp->cl_confirm;
7515 } else {
7516 calldata->args.verifier = &verifier;
7517 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007518 calldata->args.client = clp;
7519#ifdef CONFIG_NFS_V4_1_MIGRATION
7520 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7521 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7522 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7523#else
7524 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7525 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7526#endif
7527 msg.rpc_argp = &calldata->args;
7528 msg.rpc_resp = &calldata->res;
7529 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007530
Andy Adamson8d89bd72016-09-09 09:22:18 -04007531 task = rpc_run_task(&task_setup_data);
Olga Kornievskaia63513232017-03-13 10:36:19 -04007532 if (IS_ERR(task))
7533 return PTR_ERR(task);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007534
Andy Adamsonad0849a2016-09-09 09:22:28 -04007535 if (!xprt) {
7536 status = rpc_wait_for_completion_task(task);
7537 if (!status)
7538 status = calldata->rpc_status;
7539 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007540 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007541
Andy Adamson8d89bd72016-09-09 09:22:18 -04007542 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007543out:
Benny Halevy99fe60d2009-04-01 09:22:29 -04007544 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007545
7546out_impl_id:
7547 kfree(calldata->res.impl_id);
7548out_server_scope:
7549 kfree(calldata->res.server_scope);
7550out_server_owner:
7551 kfree(calldata->res.server_owner);
7552out_calldata:
7553 kfree(calldata);
Olga Kornievskaia63513232017-03-13 10:36:19 -04007554 nfs_put_client(clp);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007555 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007556}
7557
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007558/*
7559 * nfs4_proc_exchange_id()
7560 *
7561 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7562 *
7563 * Since the clientid has expired, all compounds using sessions
7564 * associated with the stale clientid will be returning
7565 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7566 * be in some phase of session reset.
7567 *
7568 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7569 */
7570int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7571{
7572 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7573 int status;
7574
7575 /* try SP4_MACH_CRED if krb5i/p */
7576 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7577 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007578 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007579 if (!status)
7580 return 0;
7581 }
7582
7583 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007584 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007585}
7586
Andy Adamson04fa2c62016-09-09 09:22:29 -04007587/**
7588 * nfs4_test_session_trunk
7589 *
7590 * This is an add_xprt_test() test function called from
7591 * rpc_clnt_setup_test_and_add_xprt.
7592 *
7593 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7594 * and is dereferrenced in nfs4_exchange_id_release
7595 *
7596 * Upon success, add the new transport to the rpc_clnt
7597 *
7598 * @clnt: struct rpc_clnt to get new transport
7599 * @xprt: the rpc_xprt to test
7600 * @data: call data for _nfs4_proc_exchange_id.
7601 */
7602int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7603 void *data)
7604{
7605 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7606 u32 sp4_how;
7607
7608 dprintk("--> %s try %s\n", __func__,
7609 xprt->address_strings[RPC_DISPLAY_ADDR]);
7610
7611 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7612
7613 /* Test connection for session trunking. Async exchange_id call */
7614 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7615}
7616EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7617
Trond Myklebust66245532012-05-25 17:18:09 -04007618static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7619 struct rpc_cred *cred)
7620{
7621 struct rpc_message msg = {
7622 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7623 .rpc_argp = clp,
7624 .rpc_cred = cred,
7625 };
7626 int status;
7627
7628 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007629 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007630 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007631 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007632 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7633 return status;
7634}
7635
7636static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7637 struct rpc_cred *cred)
7638{
7639 unsigned int loop;
7640 int ret;
7641
7642 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7643 ret = _nfs4_proc_destroy_clientid(clp, cred);
7644 switch (ret) {
7645 case -NFS4ERR_DELAY:
7646 case -NFS4ERR_CLIENTID_BUSY:
7647 ssleep(1);
7648 break;
7649 default:
7650 return ret;
7651 }
7652 }
7653 return 0;
7654}
7655
7656int nfs4_destroy_clientid(struct nfs_client *clp)
7657{
7658 struct rpc_cred *cred;
7659 int ret = 0;
7660
7661 if (clp->cl_mvops->minor_version < 1)
7662 goto out;
7663 if (clp->cl_exchange_flags == 0)
7664 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007665 if (clp->cl_preserve_clid)
7666 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007667 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007668 ret = nfs4_proc_destroy_clientid(clp, cred);
7669 if (cred)
7670 put_rpccred(cred);
7671 switch (ret) {
7672 case 0:
7673 case -NFS4ERR_STALE_CLIENTID:
7674 clp->cl_exchange_flags = 0;
7675 }
7676out:
7677 return ret;
7678}
7679
Andy Adamson2050f0c2009-04-01 09:22:30 -04007680struct nfs4_get_lease_time_data {
7681 struct nfs4_get_lease_time_args *args;
7682 struct nfs4_get_lease_time_res *res;
7683 struct nfs_client *clp;
7684};
7685
7686static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7687 void *calldata)
7688{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007689 struct nfs4_get_lease_time_data *data =
7690 (struct nfs4_get_lease_time_data *)calldata;
7691
7692 dprintk("--> %s\n", __func__);
7693 /* just setup sequence, do not trigger session recovery
7694 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007695 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007696 &data->args->la_seq_args,
7697 &data->res->lr_seq_res,
7698 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007699 dprintk("<-- %s\n", __func__);
7700}
7701
7702/*
7703 * Called from nfs4_state_manager thread for session setup, so don't recover
7704 * from sequence operation or clientid errors.
7705 */
7706static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7707{
7708 struct nfs4_get_lease_time_data *data =
7709 (struct nfs4_get_lease_time_data *)calldata;
7710
7711 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007712 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7713 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007714 switch (task->tk_status) {
7715 case -NFS4ERR_DELAY:
7716 case -NFS4ERR_GRACE:
7717 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7718 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7719 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007720 /* fall through */
7721 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007722 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007723 return;
7724 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007725 dprintk("<-- %s\n", __func__);
7726}
7727
Trond Myklebust17280172012-03-11 13:11:00 -04007728static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007729 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7730 .rpc_call_done = nfs4_get_lease_time_done,
7731};
7732
7733int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7734{
7735 struct rpc_task *task;
7736 struct nfs4_get_lease_time_args args;
7737 struct nfs4_get_lease_time_res res = {
7738 .lr_fsinfo = fsinfo,
7739 };
7740 struct nfs4_get_lease_time_data data = {
7741 .args = &args,
7742 .res = &res,
7743 .clp = clp,
7744 };
7745 struct rpc_message msg = {
7746 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7747 .rpc_argp = &args,
7748 .rpc_resp = &res,
7749 };
7750 struct rpc_task_setup task_setup = {
7751 .rpc_client = clp->cl_rpcclient,
7752 .rpc_message = &msg,
7753 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007754 .callback_data = &data,
7755 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007756 };
7757 int status;
7758
Chuck Levera9c92d62013-08-09 12:48:18 -04007759 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007760 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007761 task = rpc_run_task(&task_setup);
7762
7763 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04007764 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007765
Anna Schumakerf6148712017-04-07 14:15:23 -04007766 status = task->tk_status;
7767 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007768 return status;
7769}
7770
Andy Adamsonfc931582009-04-01 09:22:31 -04007771/*
7772 * Initialize the values to be used by the client in CREATE_SESSION
7773 * If nfs4_init_session set the fore channel request and response sizes,
7774 * use them.
7775 *
7776 * Set the back channel max_resp_sz_cached to zero to force the client to
7777 * always set csa_cachethis to FALSE because the current implementation
7778 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7779 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007780static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7781 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007782{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007783 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007784 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007785
Andy Adamson18aad3d2013-06-26 12:21:49 -04007786 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7787 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7788
Andy Adamsonfc931582009-04-01 09:22:31 -04007789 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007790 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7791 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007792 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007793 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007794
7795 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007796 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007797 __func__,
7798 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007799 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007800
7801 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007802 args->bc_attrs.max_rqst_sz = max_bc_payload;
7803 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007804 args->bc_attrs.max_resp_sz_cached = 0;
7805 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007806 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007807
7808 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7809 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7810 __func__,
7811 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7812 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7813 args->bc_attrs.max_reqs);
7814}
7815
Trond Myklebust79969dd2015-02-18 11:30:18 -08007816static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7817 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007818{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007819 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007820 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007821
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007822 if (rcvd->max_resp_sz > sent->max_resp_sz)
7823 return -EINVAL;
7824 /*
7825 * Our requested max_ops is the minimum we need; we're not
7826 * prepared to break up compounds into smaller pieces than that.
7827 * So, no point even trying to continue if the server won't
7828 * cooperate:
7829 */
7830 if (rcvd->max_ops < sent->max_ops)
7831 return -EINVAL;
7832 if (rcvd->max_reqs == 0)
7833 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007834 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7835 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007836 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007837}
7838
Trond Myklebust79969dd2015-02-18 11:30:18 -08007839static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7840 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007841{
7842 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007843 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007844
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007845 if (!(res->flags & SESSION4_BACK_CHAN))
7846 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007847 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7848 return -EINVAL;
7849 if (rcvd->max_resp_sz < sent->max_resp_sz)
7850 return -EINVAL;
7851 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7852 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007853 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007854 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007855 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007856 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007857out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007858 return 0;
7859}
Andy Adamson8d353012009-04-01 09:22:32 -04007860
Andy Adamson8d353012009-04-01 09:22:32 -04007861static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007862 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007863{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007864 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007865
Trond Myklebust79969dd2015-02-18 11:30:18 -08007866 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007867 if (ret)
7868 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007869 return nfs4_verify_back_channel_attrs(args, res);
7870}
7871
7872static void nfs4_update_session(struct nfs4_session *session,
7873 struct nfs41_create_session_res *res)
7874{
7875 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007876 /* Mark client id and session as being confirmed */
7877 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7878 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007879 session->flags = res->flags;
7880 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007881 if (res->flags & SESSION4_BACK_CHAN)
7882 memcpy(&session->bc_attrs, &res->bc_attrs,
7883 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007884}
7885
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007886static int _nfs4_proc_create_session(struct nfs_client *clp,
7887 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007888{
7889 struct nfs4_session *session = clp->cl_session;
7890 struct nfs41_create_session_args args = {
7891 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007892 .clientid = clp->cl_clientid,
7893 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007894 .cb_program = NFS4_CALLBACK,
7895 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007896 struct nfs41_create_session_res res;
7897
Andy Adamsonfc931582009-04-01 09:22:31 -04007898 struct rpc_message msg = {
7899 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7900 .rpc_argp = &args,
7901 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007902 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007903 };
7904 int status;
7905
Chuck Lever6b26cc82016-05-02 14:40:40 -04007906 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007907 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007908
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007909 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007910 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007911
Trond Myklebustb519d402016-09-11 14:50:01 -04007912 switch (status) {
7913 case -NFS4ERR_STALE_CLIENTID:
7914 case -NFS4ERR_DELAY:
7915 case -ETIMEDOUT:
7916 case -EACCES:
7917 case -EAGAIN:
7918 goto out;
7919 };
7920
7921 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007922 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007923 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007924 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007925 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007926 if (status)
7927 goto out;
7928 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007929 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007930out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007931 return status;
7932}
7933
7934/*
7935 * Issues a CREATE_SESSION operation to the server.
7936 * It is the responsibility of the caller to verify the session is
7937 * expired before calling this routine.
7938 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007939int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007940{
7941 int status;
7942 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007943 struct nfs4_session *session = clp->cl_session;
7944
7945 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7946
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007947 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007948 if (status)
7949 goto out;
7950
Andy Adamsonaacd5532011-11-09 13:58:21 -05007951 /* Init or reset the session slot tables */
7952 status = nfs4_setup_session_slot_tables(session);
7953 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007954 if (status)
7955 goto out;
7956
7957 ptr = (unsigned *)&session->sess_id.data[0];
7958 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7959 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007960out:
7961 dprintk("<-- %s\n", __func__);
7962 return status;
7963}
7964
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007965/*
7966 * Issue the over-the-wire RPC DESTROY_SESSION.
7967 * The caller must serialize access to this routine.
7968 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007969int nfs4_proc_destroy_session(struct nfs4_session *session,
7970 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007971{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007972 struct rpc_message msg = {
7973 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7974 .rpc_argp = session,
7975 .rpc_cred = cred,
7976 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007977 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007978
7979 dprintk("--> nfs4_proc_destroy_session\n");
7980
7981 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007982 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7983 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007984
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007985 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007986 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007987
7988 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007989 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007990 "Session has been destroyed regardless...\n", status);
7991
7992 dprintk("<-- nfs4_proc_destroy_session\n");
7993 return status;
7994}
7995
Trond Myklebust7b38c362012-05-23 13:23:31 -04007996/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007997 * Renew the cl_session lease.
7998 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007999struct nfs4_sequence_data {
8000 struct nfs_client *clp;
8001 struct nfs4_sequence_args args;
8002 struct nfs4_sequence_res res;
8003};
8004
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008005static void nfs41_sequence_release(void *data)
8006{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008007 struct nfs4_sequence_data *calldata = data;
8008 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008009
Alexandros Batsakis71358402010-02-05 03:45:05 -08008010 if (atomic_read(&clp->cl_count) > 1)
8011 nfs4_schedule_state_renewal(clp);
8012 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008013 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008014}
8015
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008016static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8017{
8018 switch(task->tk_status) {
8019 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008020 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8021 return -EAGAIN;
8022 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008023 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008024 }
8025 return 0;
8026}
8027
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008028static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008029{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008030 struct nfs4_sequence_data *calldata = data;
8031 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008032
Trond Myklebust14516c32010-07-31 14:29:06 -04008033 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8034 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008035
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008036 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008037 if (task->tk_status < 0) {
8038 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008039 if (atomic_read(&clp->cl_count) == 1)
8040 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008041
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008042 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8043 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008044 return;
8045 }
8046 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008047 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008048out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008049 dprintk("<-- %s\n", __func__);
8050}
8051
8052static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8053{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008054 struct nfs4_sequence_data *calldata = data;
8055 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008056 struct nfs4_sequence_args *args;
8057 struct nfs4_sequence_res *res;
8058
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008059 args = task->tk_msg.rpc_argp;
8060 res = task->tk_msg.rpc_resp;
8061
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008062 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008063}
8064
8065static const struct rpc_call_ops nfs41_sequence_ops = {
8066 .rpc_call_done = nfs41_sequence_call_done,
8067 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008068 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008069};
8070
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008071static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8072 struct rpc_cred *cred,
8073 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008074{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008075 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008076 struct rpc_message msg = {
8077 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8078 .rpc_cred = cred,
8079 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008080 struct rpc_task_setup task_setup_data = {
8081 .rpc_client = clp->cl_rpcclient,
8082 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008083 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008084 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008085 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008086
Alexandros Batsakis71358402010-02-05 03:45:05 -08008087 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008088 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008089 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008090 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008091 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008092 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008093 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008094 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008095 if (is_privileged)
8096 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008097 msg.rpc_argp = &calldata->args;
8098 msg.rpc_resp = &calldata->res;
8099 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008100 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008101
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008102 return rpc_run_task(&task_setup_data);
8103}
8104
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008105static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008106{
8107 struct rpc_task *task;
8108 int ret = 0;
8109
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008110 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008111 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008112 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008113 if (IS_ERR(task))
8114 ret = PTR_ERR(task);
8115 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008116 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008117 dprintk("<-- %s status=%d\n", __func__, ret);
8118 return ret;
8119}
8120
8121static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8122{
8123 struct rpc_task *task;
8124 int ret;
8125
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008126 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008127 if (IS_ERR(task)) {
8128 ret = PTR_ERR(task);
8129 goto out;
8130 }
8131 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008132 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008133 ret = task->tk_status;
8134 rpc_put_task(task);
8135out:
8136 dprintk("<-- %s status=%d\n", __func__, ret);
8137 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008138}
8139
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008140struct nfs4_reclaim_complete_data {
8141 struct nfs_client *clp;
8142 struct nfs41_reclaim_complete_args arg;
8143 struct nfs41_reclaim_complete_res res;
8144};
8145
8146static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8147{
8148 struct nfs4_reclaim_complete_data *calldata = data;
8149
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008150 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008151 &calldata->arg.seq_args,
8152 &calldata->res.seq_res,
8153 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008154}
8155
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008156static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8157{
8158 switch(task->tk_status) {
8159 case 0:
8160 case -NFS4ERR_COMPLETE_ALREADY:
8161 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8162 break;
8163 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008164 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008165 /* fall through */
8166 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008167 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008168 case -NFS4ERR_BADSESSION:
8169 case -NFS4ERR_DEADSESSION:
8170 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8171 nfs4_schedule_session_recovery(clp->cl_session,
8172 task->tk_status);
8173 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008174 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008175 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008176 }
8177 return 0;
8178}
8179
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008180static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8181{
8182 struct nfs4_reclaim_complete_data *calldata = data;
8183 struct nfs_client *clp = calldata->clp;
8184 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8185
8186 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008187 if (!nfs41_sequence_done(task, res))
8188 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008189
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008190 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008191 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8192 rpc_restart_call_prepare(task);
8193 return;
8194 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008195 dprintk("<-- %s\n", __func__);
8196}
8197
8198static void nfs4_free_reclaim_complete_data(void *data)
8199{
8200 struct nfs4_reclaim_complete_data *calldata = data;
8201
8202 kfree(calldata);
8203}
8204
8205static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8206 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8207 .rpc_call_done = nfs4_reclaim_complete_done,
8208 .rpc_release = nfs4_free_reclaim_complete_data,
8209};
8210
8211/*
8212 * Issue a global reclaim complete.
8213 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008214static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8215 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008216{
8217 struct nfs4_reclaim_complete_data *calldata;
8218 struct rpc_task *task;
8219 struct rpc_message msg = {
8220 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008221 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008222 };
8223 struct rpc_task_setup task_setup_data = {
8224 .rpc_client = clp->cl_rpcclient,
8225 .rpc_message = &msg,
8226 .callback_ops = &nfs4_reclaim_complete_call_ops,
8227 .flags = RPC_TASK_ASYNC,
8228 };
8229 int status = -ENOMEM;
8230
8231 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008232 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008233 if (calldata == NULL)
8234 goto out;
8235 calldata->clp = clp;
8236 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008237
Chuck Levera9c92d62013-08-09 12:48:18 -04008238 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008239 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008240 msg.rpc_argp = &calldata->arg;
8241 msg.rpc_resp = &calldata->res;
8242 task_setup_data.callback_data = calldata;
8243 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008244 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008245 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008246 goto out;
8247 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008248 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008249 if (status == 0)
8250 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008251 rpc_put_task(task);
8252out:
8253 dprintk("<-- %s status=%d\n", __func__, status);
8254 return status;
8255}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008256
8257static void
8258nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8259{
8260 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008261 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008262
8263 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008264 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008265 &lgp->res.seq_res, task);
8266 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008267}
8268
8269static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8270{
8271 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008272
8273 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008274 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008275 dprintk("<-- %s\n", __func__);
8276}
8277
8278static int
8279nfs4_layoutget_handle_exception(struct rpc_task *task,
8280 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8281{
Trond Myklebustee314c22012-10-01 17:25:48 -07008282 struct inode *inode = lgp->args.inode;
8283 struct nfs_server *server = NFS_SERVER(inode);
8284 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008285 int nfs4err = task->tk_status;
8286 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008287 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008288
Boaz Harroshed7e5422014-01-22 20:34:54 +02008289 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008290
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008291 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008292 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008293 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008294
8295 /*
8296 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8297 * on the file. set tk_status to -ENODATA to tell upper layer to
8298 * retry go inband.
8299 */
8300 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008301 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008302 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008303 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008304 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8305 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8306 */
8307 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008308 status = -EOVERFLOW;
8309 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008310 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008311 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008312 * (or clients) writing to the same RAID stripe except when
8313 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008314 *
8315 * Treat it like we would RECALLCONFLICT -- we retry for a little
8316 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008317 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008318 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008319 if (lgp->args.minlength == 0) {
8320 status = -EOVERFLOW;
8321 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008322 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008323 status = -EBUSY;
8324 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008325 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008326 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008327 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008328 case -NFS4ERR_DELEG_REVOKED:
8329 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008330 case -NFS4ERR_EXPIRED:
8331 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008332 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008333 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008334 lo = NFS_I(inode)->layout;
8335 /* If the open stateid was bad, then recover it. */
8336 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8337 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008338 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008339 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008340 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008341 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008342 break;
8343 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008344
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008345 /*
8346 * Mark the bad layout state as invalid, then retry
8347 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008348 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008349 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008350 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008351 pnfs_free_lseg_list(&head);
8352 status = -EAGAIN;
8353 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008354 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008355
Trond Myklebust8ac09252017-01-23 22:44:12 -05008356 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008357 err = nfs4_handle_exception(server, nfs4err, exception);
8358 if (!status) {
8359 if (exception->retry)
8360 status = -EAGAIN;
8361 else
8362 status = err;
8363 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008364out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008365 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008366 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008367}
8368
Idan Kedar85541162012-08-02 11:47:10 +03008369static size_t max_response_pages(struct nfs_server *server)
8370{
8371 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8372 return nfs_page_array_len(0, max_resp_sz);
8373}
8374
8375static void nfs4_free_pages(struct page **pages, size_t size)
8376{
8377 int i;
8378
8379 if (!pages)
8380 return;
8381
8382 for (i = 0; i < size; i++) {
8383 if (!pages[i])
8384 break;
8385 __free_page(pages[i]);
8386 }
8387 kfree(pages);
8388}
8389
8390static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8391{
8392 struct page **pages;
8393 int i;
8394
8395 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8396 if (!pages) {
8397 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8398 return NULL;
8399 }
8400
8401 for (i = 0; i < size; i++) {
8402 pages[i] = alloc_page(gfp_flags);
8403 if (!pages[i]) {
8404 dprintk("%s: failed to allocate page\n", __func__);
8405 nfs4_free_pages(pages, size);
8406 return NULL;
8407 }
8408 }
8409
8410 return pages;
8411}
8412
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008413static void nfs4_layoutget_release(void *calldata)
8414{
8415 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008416 struct inode *inode = lgp->args.inode;
8417 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008418 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008419
8420 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008421 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008422 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008423 put_nfs_open_context(lgp->args.ctx);
8424 kfree(calldata);
8425 dprintk("<-- %s\n", __func__);
8426}
8427
8428static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8429 .rpc_call_prepare = nfs4_layoutget_prepare,
8430 .rpc_call_done = nfs4_layoutget_done,
8431 .rpc_release = nfs4_layoutget_release,
8432};
8433
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008434struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008435nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008436{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008437 struct inode *inode = lgp->args.inode;
8438 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008439 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008440 struct rpc_task *task;
8441 struct rpc_message msg = {
8442 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8443 .rpc_argp = &lgp->args,
8444 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008445 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008446 };
8447 struct rpc_task_setup task_setup_data = {
8448 .rpc_client = server->client,
8449 .rpc_message = &msg,
8450 .callback_ops = &nfs4_layoutget_call_ops,
8451 .callback_data = lgp,
8452 .flags = RPC_TASK_ASYNC,
8453 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008454 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008455 struct nfs4_exception exception = {
8456 .inode = inode,
8457 .timeout = *timeout,
8458 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008459 int status = 0;
8460
8461 dprintk("--> %s\n", __func__);
8462
Peng Tao4bd5a9802014-11-17 11:05:17 +08008463 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8464 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8465
Idan Kedar85541162012-08-02 11:47:10 +03008466 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8467 if (!lgp->args.layout.pages) {
8468 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008469 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008470 }
8471 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8472
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008473 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008474 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008475 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008476
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008477 task = rpc_run_task(&task_setup_data);
8478 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008479 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008480 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008481 if (status == 0) {
8482 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8483 *timeout = exception.timeout;
8484 }
8485
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008486 trace_nfs4_layoutget(lgp->args.ctx,
8487 &lgp->args.range,
8488 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008489 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008490 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008491
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008492 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8493 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008494 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008495 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008496 rpc_put_task(task);
8497 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008498 if (status)
8499 return ERR_PTR(status);
8500 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008501}
8502
Benny Halevycbe82602011-05-22 19:52:37 +03008503static void
8504nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8505{
8506 struct nfs4_layoutreturn *lrp = calldata;
8507
8508 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008509 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008510 &lrp->args.seq_args,
8511 &lrp->res.seq_res,
8512 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008513}
8514
8515static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8516{
8517 struct nfs4_layoutreturn *lrp = calldata;
8518 struct nfs_server *server;
8519
8520 dprintk("--> %s\n", __func__);
8521
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008522 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008523 return;
8524
8525 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008526 switch (task->tk_status) {
8527 default:
8528 task->tk_status = 0;
8529 case 0:
8530 break;
8531 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008532 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008533 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008534 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008535 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008536 return;
8537 }
Benny Halevycbe82602011-05-22 19:52:37 +03008538 dprintk("<-- %s\n", __func__);
8539}
8540
8541static void nfs4_layoutreturn_release(void *calldata)
8542{
8543 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008544 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008545
8546 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008547 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008548 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008549 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008550 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8551 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008552 pnfs_put_layout_hdr(lrp->args.layout);
8553 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008554 kfree(calldata);
8555 dprintk("<-- %s\n", __func__);
8556}
8557
8558static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8559 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8560 .rpc_call_done = nfs4_layoutreturn_done,
8561 .rpc_release = nfs4_layoutreturn_release,
8562};
8563
Peng Tao6c166052014-11-17 09:30:40 +08008564int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008565{
8566 struct rpc_task *task;
8567 struct rpc_message msg = {
8568 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8569 .rpc_argp = &lrp->args,
8570 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008571 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008572 };
8573 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008574 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008575 .rpc_message = &msg,
8576 .callback_ops = &nfs4_layoutreturn_call_ops,
8577 .callback_data = lrp,
8578 };
Peng Tao6c166052014-11-17 09:30:40 +08008579 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008580
Andrew Elble99ade3c2015-12-02 09:39:51 -05008581 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8582 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8583 &task_setup_data.rpc_client, &msg);
8584
Benny Halevycbe82602011-05-22 19:52:37 +03008585 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008586 if (!sync) {
8587 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8588 if (!lrp->inode) {
8589 nfs4_layoutreturn_release(lrp);
8590 return -EAGAIN;
8591 }
8592 task_setup_data.flags |= RPC_TASK_ASYNC;
8593 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008594 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008595 task = rpc_run_task(&task_setup_data);
8596 if (IS_ERR(task))
8597 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008598 if (sync)
8599 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008600 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008601 dprintk("<-- %s status=%d\n", __func__, status);
8602 rpc_put_task(task);
8603 return status;
8604}
8605
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008606static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008607_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8608 struct pnfs_device *pdev,
8609 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008610{
8611 struct nfs4_getdeviceinfo_args args = {
8612 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008613 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8614 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008615 };
8616 struct nfs4_getdeviceinfo_res res = {
8617 .pdev = pdev,
8618 };
8619 struct rpc_message msg = {
8620 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8621 .rpc_argp = &args,
8622 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008623 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008624 };
8625 int status;
8626
8627 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008628 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008629 if (res.notification & ~args.notify_types)
8630 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008631 if (res.notification != args.notify_types)
8632 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008633
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008634 dprintk("<-- %s status=%d\n", __func__, status);
8635
8636 return status;
8637}
8638
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008639int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8640 struct pnfs_device *pdev,
8641 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008642{
8643 struct nfs4_exception exception = { };
8644 int err;
8645
8646 do {
8647 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008648 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008649 &exception);
8650 } while (exception.retry);
8651 return err;
8652}
8653EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8654
Andy Adamson863a3c62011-03-23 13:27:54 +00008655static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8656{
8657 struct nfs4_layoutcommit_data *data = calldata;
8658 struct nfs_server *server = NFS_SERVER(data->args.inode);
8659
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008660 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008661 &data->args.seq_args,
8662 &data->res.seq_res,
8663 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008664}
8665
8666static void
8667nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8668{
8669 struct nfs4_layoutcommit_data *data = calldata;
8670 struct nfs_server *server = NFS_SERVER(data->args.inode);
8671
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008672 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008673 return;
8674
8675 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008676 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8677 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8678 case -NFS4ERR_BADLAYOUT: /* no layout */
8679 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008680 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008681 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008682 break;
8683 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008684 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008685 rpc_restart_call_prepare(task);
8686 return;
8687 }
8688 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008689}
8690
8691static void nfs4_layoutcommit_release(void *calldata)
8692{
8693 struct nfs4_layoutcommit_data *data = calldata;
8694
Andy Adamsondb29c082011-07-30 20:52:38 -04008695 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008696 nfs_post_op_update_inode_force_wcc(data->args.inode,
8697 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008698 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008699 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008700 kfree(data);
8701}
8702
8703static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8704 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8705 .rpc_call_done = nfs4_layoutcommit_done,
8706 .rpc_release = nfs4_layoutcommit_release,
8707};
8708
8709int
Andy Adamsonef311532011-03-12 02:58:10 -05008710nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008711{
8712 struct rpc_message msg = {
8713 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8714 .rpc_argp = &data->args,
8715 .rpc_resp = &data->res,
8716 .rpc_cred = data->cred,
8717 };
8718 struct rpc_task_setup task_setup_data = {
8719 .task = &data->task,
8720 .rpc_client = NFS_CLIENT(data->args.inode),
8721 .rpc_message = &msg,
8722 .callback_ops = &nfs4_layoutcommit_ops,
8723 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008724 };
8725 struct rpc_task *task;
8726 int status = 0;
8727
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008728 dprintk("NFS: initiating layoutcommit call. sync %d "
8729 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008730 data->args.lastbytewritten,
8731 data->args.inode->i_ino);
8732
Trond Myklebust472e2592015-02-05 16:50:30 -05008733 if (!sync) {
8734 data->inode = nfs_igrab_and_active(data->args.inode);
8735 if (data->inode == NULL) {
8736 nfs4_layoutcommit_release(data);
8737 return -EAGAIN;
8738 }
8739 task_setup_data.flags = RPC_TASK_ASYNC;
8740 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008741 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008742 task = rpc_run_task(&task_setup_data);
8743 if (IS_ERR(task))
8744 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008745 if (sync)
8746 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008747 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008748 dprintk("%s: status %d\n", __func__, status);
8749 rpc_put_task(task);
8750 return status;
8751}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008752
Andy Adamson97431202013-08-08 10:57:56 -04008753/**
8754 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8755 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8756 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008757static int
8758_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008759 struct nfs_fsinfo *info,
8760 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008761{
8762 struct nfs41_secinfo_no_name_args args = {
8763 .style = SECINFO_STYLE_CURRENT_FH,
8764 };
8765 struct nfs4_secinfo_res res = {
8766 .flavors = flavors,
8767 };
8768 struct rpc_message msg = {
8769 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8770 .rpc_argp = &args,
8771 .rpc_resp = &res,
8772 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008773 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008774 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008775 int status;
8776
8777 if (use_integrity) {
8778 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008779 cred = nfs4_get_clid_cred(server->nfs_client);
8780 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008781 }
8782
8783 dprintk("--> %s\n", __func__);
8784 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8785 &res.seq_res, 0);
8786 dprintk("<-- %s status=%d\n", __func__, status);
8787
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008788 if (cred)
8789 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008790
8791 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008792}
8793
8794static int
8795nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8796 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8797{
8798 struct nfs4_exception exception = { };
8799 int err;
8800 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008801 /* first try using integrity protection */
8802 err = -NFS4ERR_WRONGSEC;
8803
8804 /* try to use integrity protection with machine cred */
8805 if (_nfs4_is_integrity_protected(server->nfs_client))
8806 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8807 flavors, true);
8808
8809 /*
8810 * if unable to use integrity protection, or SECINFO with
8811 * integrity protection returns NFS4ERR_WRONGSEC (which is
8812 * disallowed by spec, but exists in deployed servers) use
8813 * the current filesystem's rpc_client and the user cred.
8814 */
8815 if (err == -NFS4ERR_WRONGSEC)
8816 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8817 flavors, false);
8818
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008819 switch (err) {
8820 case 0:
8821 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008822 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008823 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008824 default:
8825 err = nfs4_handle_exception(server, err, &exception);
8826 }
8827 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008828out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008829 return err;
8830}
8831
8832static int
8833nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8834 struct nfs_fsinfo *info)
8835{
8836 int err;
8837 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008838 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008839 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008840 struct nfs4_secinfo4 *secinfo;
8841 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008842
8843 page = alloc_page(GFP_KERNEL);
8844 if (!page) {
8845 err = -ENOMEM;
8846 goto out;
8847 }
8848
8849 flavors = page_address(page);
8850 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8851
8852 /*
8853 * Fall back on "guess and check" method if
8854 * the server doesn't support SECINFO_NO_NAME
8855 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008856 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008857 err = nfs4_find_root_sec(server, fhandle, info);
8858 goto out_freepage;
8859 }
8860 if (err)
8861 goto out_freepage;
8862
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008863 for (i = 0; i < flavors->num_flavors; i++) {
8864 secinfo = &flavors->flavors[i];
8865
8866 switch (secinfo->flavor) {
8867 case RPC_AUTH_NULL:
8868 case RPC_AUTH_UNIX:
8869 case RPC_AUTH_GSS:
8870 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8871 &secinfo->flavor_info);
8872 break;
8873 default:
8874 flavor = RPC_AUTH_MAXFLAVOR;
8875 break;
8876 }
8877
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008878 if (!nfs_auth_info_match(&server->auth_info, flavor))
8879 flavor = RPC_AUTH_MAXFLAVOR;
8880
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008881 if (flavor != RPC_AUTH_MAXFLAVOR) {
8882 err = nfs4_lookup_root_sec(server, fhandle,
8883 info, flavor);
8884 if (!err)
8885 break;
8886 }
8887 }
8888
8889 if (flavor == RPC_AUTH_MAXFLAVOR)
8890 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008891
8892out_freepage:
8893 put_page(page);
8894 if (err == -EACCES)
8895 return -EPERM;
8896out:
8897 return err;
8898}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008899
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008900static int _nfs41_test_stateid(struct nfs_server *server,
8901 nfs4_stateid *stateid,
8902 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008903{
8904 int status;
8905 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008906 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008907 };
8908 struct nfs41_test_stateid_res res;
8909 struct rpc_message msg = {
8910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8911 .rpc_argp = &args,
8912 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008913 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008914 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008915 struct rpc_clnt *rpc_client = server->client;
8916
8917 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8918 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008919
Chuck Lever38527b12012-07-11 16:30:23 -04008920 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008921 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008922 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008923 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008924 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008925 if (status != NFS_OK) {
8926 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008927 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008928 }
8929 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008930 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008931}
8932
Trond Myklebust43912bb2016-09-22 13:38:56 -04008933static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8934 int err, struct nfs4_exception *exception)
8935{
8936 exception->retry = 0;
8937 switch(err) {
8938 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008939 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008940 nfs4_handle_exception(server, err, exception);
8941 break;
8942 case -NFS4ERR_BADSESSION:
8943 case -NFS4ERR_BADSLOT:
8944 case -NFS4ERR_BAD_HIGH_SLOT:
8945 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8946 case -NFS4ERR_DEADSESSION:
8947 nfs4_do_handle_exception(server, err, exception);
8948 }
8949}
8950
Chuck Lever38527b12012-07-11 16:30:23 -04008951/**
8952 * nfs41_test_stateid - perform a TEST_STATEID operation
8953 *
8954 * @server: server / transport on which to perform the operation
8955 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008956 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008957 *
8958 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8959 * Otherwise a negative NFS4ERR value is returned if the operation
8960 * failed or the state ID is not currently valid.
8961 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008962static int nfs41_test_stateid(struct nfs_server *server,
8963 nfs4_stateid *stateid,
8964 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008965{
8966 struct nfs4_exception exception = { };
8967 int err;
8968 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008969 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04008970 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008971 } while (exception.retry);
8972 return err;
8973}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008974
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008975struct nfs_free_stateid_data {
8976 struct nfs_server *server;
8977 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008978 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008979};
8980
8981static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8982{
8983 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008984 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008985 &data->args.seq_args,
8986 &data->res.seq_res,
8987 task);
8988}
8989
8990static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8991{
8992 struct nfs_free_stateid_data *data = calldata;
8993
8994 nfs41_sequence_done(task, &data->res.seq_res);
8995
8996 switch (task->tk_status) {
8997 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008998 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008999 rpc_restart_call_prepare(task);
9000 }
9001}
9002
9003static void nfs41_free_stateid_release(void *calldata)
9004{
9005 kfree(calldata);
9006}
9007
Trond Myklebust17f26b12013-08-21 15:48:42 -04009008static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009009 .rpc_call_prepare = nfs41_free_stateid_prepare,
9010 .rpc_call_done = nfs41_free_stateid_done,
9011 .rpc_release = nfs41_free_stateid_release,
9012};
9013
9014static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009015 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009016 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009017 bool privileged)
9018{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009019 struct rpc_message msg = {
9020 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009021 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009022 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009023 struct rpc_task_setup task_setup = {
9024 .rpc_client = server->client,
9025 .rpc_message = &msg,
9026 .callback_ops = &nfs41_free_stateid_ops,
9027 .flags = RPC_TASK_ASYNC,
9028 };
9029 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009030
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009031 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9032 &task_setup.rpc_client, &msg);
9033
Chuck Lever38527b12012-07-11 16:30:23 -04009034 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009035 data = kmalloc(sizeof(*data), GFP_NOFS);
9036 if (!data)
9037 return ERR_PTR(-ENOMEM);
9038 data->server = server;
9039 nfs4_stateid_copy(&data->args.stateid, stateid);
9040
9041 task_setup.callback_data = data;
9042
9043 msg.rpc_argp = &data->args;
9044 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009045 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009046 if (privileged)
9047 nfs4_set_sequence_privileged(&data->args.seq_args);
9048
9049 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009050}
9051
Chuck Lever38527b12012-07-11 16:30:23 -04009052/**
9053 * nfs41_free_stateid - perform a FREE_STATEID operation
9054 *
9055 * @server: server / transport on which to perform the operation
9056 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009057 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009058 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009059 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009060 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009061 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009062static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009063 const nfs4_stateid *stateid,
9064 struct rpc_cred *cred,
9065 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009066{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009067 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009068
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009069 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009070 if (IS_ERR(task))
9071 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009072 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009073 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009074}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009075
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009076static void
9077nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009078{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009079 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009080
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009081 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009082 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009083}
9084
Trond Myklebust36281ca2012-03-04 18:13:56 -05009085static bool nfs41_match_stateid(const nfs4_stateid *s1,
9086 const nfs4_stateid *s2)
9087{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009088 if (s1->type != s2->type)
9089 return false;
9090
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009091 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009092 return false;
9093
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009094 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009095 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009096
Anna Schumaker045c5512017-01-11 16:59:48 -05009097 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009098}
9099
Andy Adamson557134a2009-04-01 09:21:53 -04009100#endif /* CONFIG_NFS_V4_1 */
9101
Trond Myklebust36281ca2012-03-04 18:13:56 -05009102static bool nfs4_match_stateid(const nfs4_stateid *s1,
9103 const nfs4_stateid *s2)
9104{
Trond Myklebustf597c532012-03-04 18:13:56 -05009105 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009106}
9107
9108
Trond Myklebust17280172012-03-11 13:11:00 -04009109static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009110 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009111 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009112 .recover_open = nfs4_open_reclaim,
9113 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009114 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009115 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009116};
9117
Andy Adamson591d71c2009-04-01 09:22:47 -04009118#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009119static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009120 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9121 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9122 .recover_open = nfs4_open_reclaim,
9123 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009124 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009125 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009126 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009127};
9128#endif /* CONFIG_NFS_V4_1 */
9129
Trond Myklebust17280172012-03-11 13:11:00 -04009130static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009131 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009132 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009133 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009134 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009135 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009136};
9137
Andy Adamson591d71c2009-04-01 09:22:47 -04009138#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009139static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009140 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9141 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009142 .recover_open = nfs41_open_expired,
9143 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009144 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009145};
9146#endif /* CONFIG_NFS_V4_1 */
9147
Trond Myklebust17280172012-03-11 13:11:00 -04009148static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009149 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009150 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009151 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009152};
9153
9154#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009155static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009156 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009157 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009158 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009159};
9160#endif
9161
Chuck Leverec011fe2013-10-17 14:12:39 -04009162static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009163 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009164 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009165};
9166
9167#if defined(CONFIG_NFS_V4_1)
9168static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009169 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009170 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009171};
9172#endif /* CONFIG_NFS_V4_1 */
9173
Trond Myklebust97dc1352010-06-16 09:52:26 -04009174static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9175 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009176 .init_caps = NFS_CAP_READDIRPLUS
9177 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009178 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009179 .init_client = nfs40_init_client,
9180 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009181 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009182 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009183 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009184 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009185 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009186 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009187 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9188 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9189 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009190 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009191};
9192
9193#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009194static struct nfs_seqid *
9195nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9196{
9197 return NULL;
9198}
9199
Trond Myklebust97dc1352010-06-16 09:52:26 -04009200static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9201 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009202 .init_caps = NFS_CAP_READDIRPLUS
9203 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009204 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009205 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009206 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009207 .init_client = nfs41_init_client,
9208 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009209 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009210 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009211 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009212 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009213 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009214 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009215 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009216 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9217 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9218 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009219 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009220};
9221#endif
9222
Steve Dickson42c2c422013-05-22 12:50:38 -04009223#if defined(CONFIG_NFS_V4_2)
9224static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9225 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009226 .init_caps = NFS_CAP_READDIRPLUS
9227 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009228 | NFS_CAP_POSIX_LOCK
9229 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009230 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009231 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009232 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009233 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009234 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009235 | NFS_CAP_LAYOUTSTATS
9236 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009237 .init_client = nfs41_init_client,
9238 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009239 .match_stateid = nfs41_match_stateid,
9240 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009241 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009242 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009243 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009244 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009245 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009246 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9247 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9248 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009249 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009250};
9251#endif
9252
Trond Myklebust97dc1352010-06-16 09:52:26 -04009253const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9254 [0] = &nfs_v4_0_minor_ops,
9255#if defined(CONFIG_NFS_V4_1)
9256 [1] = &nfs_v4_1_minor_ops,
9257#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009258#if defined(CONFIG_NFS_V4_2)
9259 [2] = &nfs_v4_2_minor_ops,
9260#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009261};
9262
Trond Myklebust13997822016-07-24 17:10:52 -04009263static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009264{
9265 ssize_t error, error2;
9266
9267 error = generic_listxattr(dentry, list, size);
9268 if (error < 0)
9269 return error;
9270 if (list) {
9271 list += error;
9272 size -= error;
9273 }
9274
9275 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9276 if (error2 < 0)
9277 return error2;
9278 return error + error2;
9279}
9280
Trond Myklebust17f26b12013-08-21 15:48:42 -04009281static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009282 .create = nfs_create,
9283 .lookup = nfs_lookup,
9284 .atomic_open = nfs_atomic_open,
9285 .link = nfs_link,
9286 .unlink = nfs_unlink,
9287 .symlink = nfs_symlink,
9288 .mkdir = nfs_mkdir,
9289 .rmdir = nfs_rmdir,
9290 .mknod = nfs_mknod,
9291 .rename = nfs_rename,
9292 .permission = nfs_permission,
9293 .getattr = nfs_getattr,
9294 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009295 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009296};
9297
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009298static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009299 .permission = nfs_permission,
9300 .getattr = nfs_getattr,
9301 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009302 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009303};
9304
David Howells509de812006-08-22 20:06:11 -04009305const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009306 .version = 4, /* protocol version */
9307 .dentry_ops = &nfs4_dentry_operations,
9308 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009309 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009310 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009311 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009312 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009313 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009314 .getattr = nfs4_proc_getattr,
9315 .setattr = nfs4_proc_setattr,
9316 .lookup = nfs4_proc_lookup,
9317 .access = nfs4_proc_access,
9318 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009319 .create = nfs4_proc_create,
9320 .remove = nfs4_proc_remove,
9321 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009322 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009323 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009324 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009325 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009326 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009327 .link = nfs4_proc_link,
9328 .symlink = nfs4_proc_symlink,
9329 .mkdir = nfs4_proc_mkdir,
9330 .rmdir = nfs4_proc_remove,
9331 .readdir = nfs4_proc_readdir,
9332 .mknod = nfs4_proc_mknod,
9333 .statfs = nfs4_proc_statfs,
9334 .fsinfo = nfs4_proc_fsinfo,
9335 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009336 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009337 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009338 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009339 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009340 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009341 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009342 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009343 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009344 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009345 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009346 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009347 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009348 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009349 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009350 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009351 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009352 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009353 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009354 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009355 .create_server = nfs4_create_server,
9356 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009357};
9358
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009359static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009360 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009361 .list = nfs4_xattr_list_nfs4_acl,
9362 .get = nfs4_xattr_get_nfs4_acl,
9363 .set = nfs4_xattr_set_nfs4_acl,
9364};
9365
9366const struct xattr_handler *nfs4_xattr_handlers[] = {
9367 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009368#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9369 &nfs4_xattr_nfs4_label_handler,
9370#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009371 NULL
9372};
9373
Linus Torvalds1da177e2005-04-16 15:20:36 -07009374/*
9375 * Local variables:
9376 * c-basic-offset: 8
9377 * End:
9378 */